|
|
@@ -67,20 +67,6 @@ string GetOutPutStr(const char* form, ...) {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-int no_begin_with(const char strA[], const char strB[]) {
|
|
|
- if (strA == NULL) return 1;
|
|
|
- if (strB == NULL) return 1;
|
|
|
- if (strlen(strA) == 0 || strlen(strB) == 0 || strlen(strA) < strlen(strB)) return 1;
|
|
|
- const char* pA = strA;
|
|
|
- const char* pB = strB;
|
|
|
- while (*pA != '\0' && *pB != '\0') {
|
|
|
- if (*pA != *pB) return 1;
|
|
|
- pA++;
|
|
|
- pB++;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
int str2int(const string str, int& ret) {
|
|
|
if (str.size() == 0) return 1;
|
|
|
ret = 0;
|
|
|
@@ -111,49 +97,6 @@ int str2int(const string str) {
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
-vector<string> split_str(string strA, string strB) {
|
|
|
- vector<string> ret;
|
|
|
- //cout << "ret:" << ret.size() << endl;
|
|
|
- if (strA.size() == 0 || strB.size() == 0) {
|
|
|
- return ret;
|
|
|
- }
|
|
|
- char* chA = new char[strA.size()];
|
|
|
- char* chB = new char[strB.size()];
|
|
|
- memset(chA, 0, sizeof(chA));
|
|
|
- memset(chB, 0, sizeof(chB));
|
|
|
- strcpy(chA, strA.c_str());
|
|
|
- strcpy(chB, strB.c_str());
|
|
|
- char* tmp = strtok(chA, chB);
|
|
|
- while (tmp) {
|
|
|
- //cout << "tmp:" << tmp << endl;
|
|
|
- ret.push_back(tmp);
|
|
|
- tmp = strtok(NULL, chB);
|
|
|
-
|
|
|
- }
|
|
|
- delete[] chA;
|
|
|
- delete[] chB;
|
|
|
- return ret;
|
|
|
-}
|
|
|
-
|
|
|
-int ip2byte(const string str, BYTE ip[]) {
|
|
|
- vector<string> ret = split_str(str, ".");
|
|
|
- vector<string>::iterator it = ret.begin();
|
|
|
- int index = 0;
|
|
|
- while (it != ret.end()) {
|
|
|
- int tmp = 0;
|
|
|
- if (str2int(*it, tmp)) return 1;
|
|
|
- else {
|
|
|
- if (tmp > 255) return 2;
|
|
|
- else {
|
|
|
- ip[index] = tmp;
|
|
|
- index++;
|
|
|
- }
|
|
|
- }
|
|
|
- it++;
|
|
|
- }
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
unsigned char Ch2Hex(char ch)
|
|
|
{
|
|
|
static const char* hex = "0123456789ABCDEF";
|
|
|
@@ -205,45 +148,6 @@ char* Str2Hex(const char* src, int srcLen)
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
|
- int getIPFromLinux(char* ip) {
|
|
|
- //if (strlen(ip) < 15) return 1;
|
|
|
- struct ifaddrs* ifAddrStruct = NULL;
|
|
|
- void* tmpAddrPtr = NULL;
|
|
|
- getifaddrs(&ifAddrStruct);
|
|
|
-
|
|
|
- while (ifAddrStruct != NULL) {
|
|
|
- if (ifAddrStruct->ifa_addr->sa_family == AF_INET) {
|
|
|
- // is a valid IP4 Address
|
|
|
- tmpAddrPtr = &((struct sockaddr_in*)ifAddrStruct->ifa_addr)->sin_addr;
|
|
|
- char addressBuffer[INET_ADDRSTRLEN];
|
|
|
- inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
|
|
|
- //printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
|
|
|
- //99开头行内办公网,10开头行内业务网。规范出自《招商银行总行网络规范汇编(2017年版).pdf》
|
|
|
- if (!no_begin_with(addressBuffer, "99") || !no_begin_with(addressBuffer, "10")) {
|
|
|
- memset(ip, 0, sizeof(ip));
|
|
|
- strcpy(ip, addressBuffer);
|
|
|
- }
|
|
|
- else if (strlen(ip) == 0 && no_begin_with(addressBuffer, "127.0")) {
|
|
|
- memset(ip, 0, sizeof(ip));
|
|
|
- strcpy(ip, addressBuffer);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- /*
|
|
|
- else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6
|
|
|
- // is a valid IP6 Address
|
|
|
- tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
|
|
|
- char addressBuffer[INET6_ADDRSTRLEN];
|
|
|
- inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
|
|
|
- printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
|
|
|
- }
|
|
|
- */
|
|
|
- ifAddrStruct = ifAddrStruct->ifa_next;
|
|
|
- }
|
|
|
- //printf("ip=%s\n", ip);
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
|
|
|
{
|
|
|
cpu_id.c_str();
|
|
|
@@ -493,27 +397,11 @@ char* Str2Hex(const char* src, int srcLen)
|
|
|
}
|
|
|
|
|
|
#include <unistd.h>
|
|
|
- bool file_is_exist(string filePath) {
|
|
|
- return (access(filePath.c_str(), F_OK) == 0);
|
|
|
- }
|
|
|
|
|
|
bool dir_is_exist(string dirPath) {
|
|
|
return (access(dirPath.c_str(), F_OK) == 0);
|
|
|
}
|
|
|
- //创建成功返回0,创建失败返回1,已经存在返回2
|
|
|
- int file_create(string filePath) {
|
|
|
- if (!file_is_exist(filePath)) {
|
|
|
- FILE* fp;
|
|
|
- if (fp = fopen(filePath.c_str(), "w")) {
|
|
|
- fclose(fp);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- else {
|
|
|
- return 1;
|
|
|
- }
|
|
|
- }
|
|
|
- return 2;
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
//创建成功返回0
|
|
|
@@ -524,58 +412,7 @@ char* Str2Hex(const char* src, int srcLen)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- //删除成功返回0
|
|
|
- int remove_file(string filePath) {
|
|
|
- return remove(filePath.c_str());
|
|
|
- }
|
|
|
-
|
|
|
#include <dirent.h>
|
|
|
- int remove_dir(string dirPath) {
|
|
|
- DIR* dir;
|
|
|
- struct dirent* ptr;
|
|
|
- dir = opendir(dirPath.c_str());
|
|
|
- if (dir == NULL) return 1;
|
|
|
- while ((ptr = readdir(dir)) != NULL) {
|
|
|
- if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue;
|
|
|
- char path[1024] = { 0 };
|
|
|
- strcpy(path, dirPath.c_str());
|
|
|
- strcat(path, "/");
|
|
|
- strcat(path, ptr->d_name);
|
|
|
- if (ptr->d_type == DT_DIR) {
|
|
|
- remove_dir(path);
|
|
|
- }
|
|
|
- else {
|
|
|
- if (remove_file(path) != 0) return 1;
|
|
|
- }
|
|
|
- }
|
|
|
- closedir(dir);
|
|
|
- return rmdir(dirPath.c_str());
|
|
|
- }
|
|
|
-
|
|
|
- bool set_system_time(TIME* _time)
|
|
|
- {
|
|
|
- struct tm p;
|
|
|
- p.tm_year = _time->year - 1900;
|
|
|
- p.tm_mon = _time->month - 1;
|
|
|
- p.tm_mday = _time->day;
|
|
|
- p.tm_hour = _time->hour;
|
|
|
- p.tm_min = _time->minute;
|
|
|
- p.tm_sec = _time->second;
|
|
|
- //cout << p.tm_year << " " << p.tm_mon << " " << p.tm_mday << " " << p.tm_hour << " " << p.tm_min << " " << p.tm_sec << endl;
|
|
|
-
|
|
|
- time_t sec = mktime(&p);
|
|
|
- //cout << sec << endl;
|
|
|
-
|
|
|
- struct timeval tv;
|
|
|
- tv.tv_sec = sec;
|
|
|
- //cout << tv.tv_sec << endl;
|
|
|
-
|
|
|
- if (settimeofday(&tv, NULL) < 0)
|
|
|
- {
|
|
|
- return false;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
|
|
|
bool set_system_time_by_sec(int sec)
|
|
|
{
|
|
|
@@ -594,45 +431,4 @@ char* Str2Hex(const char* src, int srcLen)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- TIME* get_system_time()
|
|
|
- {
|
|
|
- TIME* t = new TIME();
|
|
|
-
|
|
|
- time_t timep;
|
|
|
- time(&timep);
|
|
|
-
|
|
|
- struct tm* p;
|
|
|
- p = localtime(&timep);
|
|
|
-
|
|
|
- t->year = 1900 + p->tm_year;
|
|
|
- t->month = 1 + p->tm_mon;
|
|
|
- t->day = p->tm_mday;
|
|
|
- t->hour = p->tm_hour;
|
|
|
- t->minute = p->tm_min;
|
|
|
- t->second = p->tm_sec;
|
|
|
-
|
|
|
- return t;
|
|
|
- }
|
|
|
-
|
|
|
- string time2str(const TIME* tim) {
|
|
|
- if (tim == NULL) return "";
|
|
|
- string ret;
|
|
|
- char timeStr[16] = { 0 };
|
|
|
- sprintf(timeStr, "%04d%02d%02d%02d%02d%02d", tim->year,
|
|
|
- tim->month, tim->day, tim->hour, tim->minute, tim->second);
|
|
|
- ret = timeStr;
|
|
|
- return ret;
|
|
|
- }
|
|
|
-
|
|
|
- TIME* str2time(string str) {
|
|
|
- if (str.length() < 14) return NULL;
|
|
|
- TIME* ret = new TIME();
|
|
|
- ret->year = str2int(str.substr(0, 4));
|
|
|
- ret->month = str2int(str.substr(4, 2));
|
|
|
- ret->day = str2int(str.substr(6, 2));
|
|
|
- ret->hour = str2int(str.substr(8, 2));
|
|
|
- ret->minute = str2int(str.substr(10, 2));
|
|
|
- ret->second = str2int(str.substr(12, 2));
|
|
|
- return ret;
|
|
|
- }
|
|
|
#endif // __linux__
|