-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvar.cpp
More file actions
34 lines (32 loc) · 702 Bytes
/
var.cpp
File metadata and controls
34 lines (32 loc) · 702 Bytes
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
#include "var.h"
bool startsWith(string& s,const char* pat){
if(strlen(pat)>s.length())
return false;
int index=0;
while(*pat){
if(s[index++]!=*pat)
return false;
++pat;
}
return true;
}
int toInt(const string& s){
return atoi(s.c_str());
}
string execShellCmd(const char* cmd,int* res_code){
string res;
const int BUF_SIZE = 128;
static char buf[BUF_SIZE];
FILE* ptr;
if((ptr=popen(cmd,"r"))!=NULL){
while(fgets(buf,BUF_SIZE,ptr)!=NULL){
res+=buf;
}
pclose(ptr);
*res_code = 0;
} else {
perror("can't open pipe!");
*res_code = -1;
}
return res;
}