-
Notifications
You must be signed in to change notification settings - Fork 13
/
comhelper.cpp
75 lines (59 loc) · 1.49 KB
/
comhelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <comhelper.h>
#include <fstream>
#include <streambuf>
void codeConvert(char * from,char * to,char * src,char * dst,size_t dstSize){
size_t sl,dl;
sl = strlen(src);
dl = dstSize;
char * pIn = src;
char * pOut = dst;
memset(dst,'\0',dstSize);
iconv_t conv = iconv_open(to,from);
iconv(conv, &pIn, &sl, &pOut, &dl);
iconv_close(conv);
}
void gbk2utf8(char * src,char * dst,size_t dstSize){
char * gbk = (char*)"GBK";
char * utf8 = (char*)"UTF-8";
codeConvert(gbk,utf8,src,dst,dstSize);
}
bool fileExists (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
std::string fileReadAll(const std::string& name){
std::ifstream f(name);
std::string content((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
return content;
}
bool stringStartsWith(std::string str,std::string key){
if (str.find(key) == 0){
return true;
}else{
return false;
}
}
CommandOption::CommandOption(int argc,char * argv[]){
this->argc = argc;
this->argv = argv;
}
bool CommandOption::exists(const std::string & option){
char ** begin = argv;
char ** end = argv + argc;
return std::find(begin, end, option) != end;
return 0;
}
char * CommandOption::get(const std::string & option){
char ** begin = argv;
char ** end = argv + argc;
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end) {
return *itr;
}
return NULL;
}