comm.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "comm.h"
  2. #include <cstdarg>
  3. #define MAX_PATH_SIZE 256
  4. void GetNewForm(const char* form, char* newForm) {
  5. int indexNum = 0;
  6. int acount = 0;
  7. newForm[0] = '{';
  8. for (int i = 0; i < strlen(form); i++)
  9. {
  10. //if((i-1 >= 0 && form[i]=='\\') || (i-1 < 0))
  11. if (form[i] == '%') {
  12. if (acount != 0)
  13. {
  14. newForm[++indexNum] = '"';
  15. if (acount % 2 != 0) {
  16. newForm[++indexNum] = ':';
  17. }
  18. else {
  19. newForm[++indexNum] = ',';
  20. }
  21. }
  22. newForm[++indexNum] = '"';
  23. acount++;
  24. }
  25. if (form[i] == ' ') continue;
  26. newForm[++indexNum] = form[i];
  27. }
  28. newForm[++indexNum] = '"';
  29. newForm[++indexNum] = '}';
  30. }
  31. string GetOutPutStr(const char* form, ...) {
  32. char* newForm = new char[strlen(form) * 3 + 5];
  33. memset(newForm, 0, strlen(form) * 3 + 5);
  34. if (strlen(form) < 2) {
  35. strcpy(newForm, "{\"\"}");
  36. }
  37. else {
  38. GetNewForm(form, newForm);
  39. }
  40. va_list vaList;
  41. va_start(vaList, form);
  42. #ifdef RVC_OS_WIN
  43. int acount = _vscprintf(newForm, vaList);
  44. #else
  45. int acount = vsnprintf(0, 0, newForm, vaList);
  46. va_end(vaList);
  47. va_start(vaList, form);
  48. #endif
  49. char* buf = new char[acount + 1];
  50. memset(buf, 0, acount + 1);
  51. vsprintf(buf, newForm, vaList);
  52. va_end(vaList);
  53. string ret;
  54. ret.assign(buf);
  55. delete buf;
  56. delete newForm;
  57. return ret;
  58. }
  59. int no_begin_with(const char strA[], const char strB[]) {
  60. if (strA == NULL) return 1;
  61. if (strB == NULL) return 1;
  62. if (strlen(strA) == 0 || strlen(strB) == 0 || strlen(strA) < strlen(strB)) return 1;
  63. const char* pA = strA;
  64. const char* pB = strB;
  65. while (*pA != '\0' && *pB != '\0') {
  66. if (*pA != *pB) return 1;
  67. pA++;
  68. pB++;
  69. }
  70. return 0;
  71. }
  72. int str2int(const string str, int& ret) {
  73. if (str.size() == 0) return 1;
  74. ret = 0;
  75. int symbol = 0;
  76. for (int i = 0; i < str.size(); i++) {
  77. if (i == 0 && str[i] == '-') {
  78. symbol = 1;
  79. continue;
  80. }
  81. if (i == 0 && str[i] == '+') continue;
  82. if (i == 0) {
  83. while (str[i] == '0' && i < str.size()) {
  84. ++i;
  85. }
  86. if (i == str.size()) return 0;
  87. }
  88. if (str[i] < '0' || str[i] >'9') return 2;
  89. ret += (str[i] - '0') * pow(10, str.size() - i - 1);
  90. }
  91. if (symbol) ret -= 2 * ret;
  92. return 0;
  93. }
  94. int str2int(const string str) {
  95. int ret;
  96. str2int(str, ret);
  97. return ret;
  98. }
  99. vector<string> split_str(string strA, string strB) {
  100. vector<string> ret;
  101. //cout << "ret:" << ret.size() << endl;
  102. if (strA.size() == 0 || strB.size() == 0) {
  103. return ret;
  104. }
  105. char* chA = new char[strA.size()];
  106. char* chB = new char[strB.size()];
  107. memset(chA, 0, sizeof(chA));
  108. memset(chB, 0, sizeof(chB));
  109. strcpy(chA, strA.c_str());
  110. strcpy(chB, strB.c_str());
  111. char* tmp = strtok(chA, chB);
  112. while (tmp) {
  113. //cout << "tmp:" << tmp << endl;
  114. ret.push_back(tmp);
  115. tmp = strtok(NULL, chB);
  116. }
  117. delete[] chA;
  118. delete[] chB;
  119. return ret;
  120. }
  121. int ip2byte(const string str, BYTE ip[]) {
  122. vector<string> ret = split_str(str, ".");
  123. vector<string>::iterator it = ret.begin();
  124. int index = 0;
  125. while (it != ret.end()) {
  126. int tmp = 0;
  127. if (str2int(*it, tmp)) return 1;
  128. else {
  129. if (tmp > 255) return 2;
  130. else {
  131. ip[index] = tmp;
  132. index++;
  133. }
  134. }
  135. it++;
  136. }
  137. return 0;
  138. }
  139. unsigned char Ch2Hex(char ch)
  140. {
  141. static const char* hex = "0123456789ABCDEF";
  142. for (unsigned char i = 0; i != 16; ++i)
  143. if (ch == hex[i])
  144. return i;
  145. return 0;
  146. }
  147. char* Hex2Str(const char* src, int& dstLen)
  148. {
  149. int i = 0;
  150. int cnt = 0;
  151. int len = strlen(src);
  152. unsigned char* d = new unsigned char[len];
  153. memset(d, 0, len);
  154. while (*src)
  155. {
  156. if (i & 1)
  157. {
  158. d[cnt++] |= Ch2Hex(*src);
  159. }
  160. else
  161. {
  162. d[cnt] = Ch2Hex(*src) << 4;
  163. }
  164. src++;
  165. i++;
  166. }
  167. dstLen = cnt;
  168. return (char*)d;
  169. }
  170. char* Str2Hex(const char* src, int srcLen)
  171. {
  172. string ret;
  173. static const char* hex = "0123456789ABCDEF";
  174. for (int i = 0; i != srcLen; ++i)
  175. {
  176. ret.push_back(hex[(src[i] >> 4) & 0xf]);
  177. ret.push_back(hex[src[i] & 0xf]);
  178. }
  179. char* tmp = new char[ret.length() + 1];
  180. memset(tmp, 0, ret.length() + 1);
  181. memcpy(tmp, ret.c_str(), ret.length());
  182. return tmp;
  183. }
  184. #ifdef __linux__
  185. int getIPFromLinux(char* ip) {
  186. //if (strlen(ip) < 15) return 1;
  187. struct ifaddrs* ifAddrStruct = NULL;
  188. void* tmpAddrPtr = NULL;
  189. getifaddrs(&ifAddrStruct);
  190. while (ifAddrStruct != NULL) {
  191. if (ifAddrStruct->ifa_addr->sa_family == AF_INET) {
  192. // is a valid IP4 Address
  193. tmpAddrPtr = &((struct sockaddr_in*)ifAddrStruct->ifa_addr)->sin_addr;
  194. char addressBuffer[INET_ADDRSTRLEN];
  195. inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
  196. //printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  197. if (!no_begin_with(addressBuffer, "99") || !no_begin_with(addressBuffer, "10")) {
  198. memset(ip, 0, sizeof(ip));
  199. strcpy(ip, addressBuffer);
  200. }
  201. else if (strlen(ip) == 0 && no_begin_with(addressBuffer, "127.0")) {
  202. memset(ip, 0, sizeof(ip));
  203. strcpy(ip, addressBuffer);
  204. }
  205. }
  206. /*
  207. else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  208. // is a valid IP6 Address
  209. tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
  210. char addressBuffer[INET6_ADDRSTRLEN];
  211. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  212. printf("%s IP Address %s\n", ifAddrStruct->ifa_name, addressBuffer);
  213. }
  214. */
  215. ifAddrStruct = ifAddrStruct->ifa_next;
  216. }
  217. //printf("ip=%s\n", ip);
  218. return 0;
  219. }
  220. //invalid function
  221. bool get_cpu_id_by_asm(std::string& cpu_id)
  222. {
  223. return(true);
  224. }
  225. void parse_cpu_id(const char* file_name, const char* match_words, std::string& cpu_id)
  226. {
  227. cpu_id.c_str();
  228. std::ifstream ifs(file_name, std::ios::binary);
  229. if (!ifs.is_open())
  230. {
  231. return;
  232. }
  233. char line[4096] = { 0 };
  234. while (!ifs.eof())
  235. {
  236. ifs.getline(line, sizeof(line));
  237. if (!ifs.good())
  238. {
  239. break;
  240. }
  241. const char* cpu = strstr(line, match_words);
  242. if (NULL == cpu)
  243. {
  244. continue;
  245. }
  246. cpu += strlen(match_words);
  247. while ('\0' != cpu[0])
  248. {
  249. if (' ' != cpu[0])
  250. {
  251. cpu_id.push_back(cpu[0]);
  252. }
  253. ++cpu;
  254. }
  255. if (!cpu_id.empty())
  256. {
  257. break;
  258. }
  259. }
  260. ifs.close();
  261. }
  262. bool get_cpu_id_by_system(std::string& cpu_id, const string save_path)
  263. {
  264. //cpu_id.c_str();
  265. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  266. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  267. strcpy(dmidecode_result, save_path.c_str());
  268. strcat(dmidecode_result, "/.dmidecode_result.txt");
  269. char command[512] = { 0 };
  270. snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
  271. if (0 == system(command))
  272. {
  273. parse_cpu_id(dmidecode_result, "ID:", cpu_id);
  274. }
  275. unlink(dmidecode_result);
  276. return(!cpu_id.empty());
  277. }
  278. void parse_board_serial(const char* file_name, const char* match_words, std::string& board_serial)
  279. {
  280. board_serial.c_str();
  281. std::ifstream ifs(file_name, std::ios::binary);
  282. if (!ifs.is_open())
  283. {
  284. return;
  285. }
  286. char line[4096] = { 0 };
  287. while (!ifs.eof())
  288. {
  289. ifs.getline(line, sizeof(line));
  290. if (!ifs.good())
  291. {
  292. break;
  293. }
  294. const char* board = strstr(line, match_words);
  295. if (NULL == board)
  296. {
  297. continue;
  298. }
  299. board += strlen(match_words);
  300. while ('\0' != board[0])
  301. {
  302. if (' ' != board[0])
  303. {
  304. board_serial.push_back(board[0]);
  305. }
  306. ++board;
  307. }
  308. if ("None" == board_serial)
  309. {
  310. board_serial.clear();
  311. continue;
  312. }
  313. if (!board_serial.empty())
  314. {
  315. break;
  316. }
  317. }
  318. ifs.close();
  319. }
  320. bool get_board_serial_by_system(std::string& board_serial,const string save_path)
  321. {
  322. //board_serial.c_str();
  323. if (save_path.size() + strlen("/.dmidecode_result.txt") > MAX_PATH_SIZE) return false;
  324. char dmidecode_result[MAX_PATH_SIZE] = { 0 };
  325. strcpy(dmidecode_result, save_path.c_str());
  326. strcat(dmidecode_result, "/.dmidecode_result.txt");
  327. char command[512] = { 0 };
  328. snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
  329. if (0 == system(command))
  330. {
  331. parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
  332. }
  333. else {
  334. return false;
  335. }
  336. unlink(dmidecode_result);
  337. return true;
  338. }
  339. bool parse_disk_serial(const char* line, int line_size, const char* match_words, std::string& serial_no)
  340. {
  341. const char* serial_s = strstr(line, match_words);
  342. if (NULL == serial_s)
  343. {
  344. return(false);
  345. }
  346. serial_s += strlen(match_words);
  347. while (isspace(serial_s[0]))
  348. {
  349. ++serial_s;
  350. }
  351. const char* serial_e = line + line_size;
  352. const char* comma = strchr(serial_s, ',');
  353. if (NULL != comma)
  354. {
  355. serial_e = comma;
  356. }
  357. while (serial_e > serial_s && isspace(serial_e[-1]))
  358. {
  359. --serial_e;
  360. }
  361. if (serial_e <= serial_s)
  362. {
  363. return(false);
  364. }
  365. std::string(serial_s, serial_e).swap(serial_no);
  366. return(true);
  367. }
  368. void get_disk_serial(const char* file_name, const char* match_words, std::vector<string>& serial_no)
  369. {
  370. std::ifstream ifs(file_name, std::ios::binary);
  371. if (!ifs.is_open())
  372. {
  373. return;
  374. }
  375. char line[4096] = { 0 };
  376. while (!ifs.eof())
  377. {
  378. ifs.getline(line, sizeof(line));
  379. if (!ifs.good())
  380. {
  381. break;
  382. }
  383. if (0 == ifs.gcount())
  384. {
  385. continue;
  386. }
  387. string disk_serial;
  388. if (parse_disk_serial(line, ifs.gcount() - 1, match_words, disk_serial))
  389. {
  390. //break;
  391. serial_no.push_back(disk_serial);
  392. }
  393. }
  394. ifs.close();
  395. }
  396. bool get_disk_serial_by_system(std::vector<string>& serial_no,const string save_path)
  397. {
  398. if (save_path.size() + strlen("/.lshw_result.txt") > MAX_PATH_SIZE) return false;
  399. char lshw_result[MAX_PATH_SIZE] = { 0 };
  400. strcpy(lshw_result, save_path.c_str());
  401. strcat(lshw_result, "/.lshw_result.txt");
  402. char command[512] = { 0 };
  403. snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
  404. if (0 == system(command))
  405. {
  406. get_disk_serial(lshw_result, "serial:", serial_no);
  407. }
  408. else {
  409. return false;
  410. }
  411. unlink(lshw_result);
  412. return(true);
  413. }
  414. #include <unistd.h>
  415. bool file_is_exist(string filePath) {
  416. return (access(filePath.c_str(), F_OK) == 0);
  417. }
  418. bool dir_is_exist(string dirPath) {
  419. return (access(dirPath.c_str(), F_OK) == 0);
  420. }
  421. //创建成功返回0,创建失败返回1,已经存在返回2
  422. int file_create(string filePath) {
  423. if (!file_is_exist(filePath)) {
  424. FILE* fp;
  425. if (fp = fopen(filePath.c_str(), "w")) {
  426. fclose(fp);
  427. return 0;
  428. }
  429. else {
  430. return 1;
  431. }
  432. }
  433. return 2;
  434. }
  435. #include <sys/stat.h>
  436. //创建成功返回0
  437. int dir_create(string dirPath) {
  438. if (!dir_is_exist(dirPath)) {
  439. return mkdir(dirPath.c_str(), S_IRWXU);
  440. }
  441. return 0;
  442. }
  443. //删除成功返回0
  444. int remove_file(string filePath) {
  445. return remove(filePath.c_str());
  446. }
  447. #include <dirent.h>
  448. int remove_dir(string dirPath) {
  449. DIR* dir;
  450. struct dirent* ptr;
  451. dir = opendir(dirPath.c_str());
  452. if (dir == NULL) return 1;
  453. while ((ptr = readdir(dir)) != NULL) {
  454. if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0) continue;
  455. char path[1024] = { 0 };
  456. strcpy(path, dirPath.c_str());
  457. strcat(path, "/");
  458. strcat(path, ptr->d_name);
  459. if (ptr->d_type == DT_DIR) {
  460. remove_dir(path);
  461. }
  462. else {
  463. if (remove_file(path) != 0) return 1;
  464. }
  465. }
  466. closedir(dir);
  467. return rmdir(dirPath.c_str());
  468. }
  469. bool set_system_time(TIME* _time)
  470. {
  471. struct tm p;
  472. p.tm_year = _time->year - 1900;
  473. p.tm_mon = _time->month - 1;
  474. p.tm_mday = _time->day;
  475. p.tm_hour = _time->hour;
  476. p.tm_min = _time->minute;
  477. p.tm_sec = _time->second;
  478. //cout << p.tm_year << " " << p.tm_mon << " " << p.tm_mday << " " << p.tm_hour << " " << p.tm_min << " " << p.tm_sec << endl;
  479. time_t sec = mktime(&p);
  480. //cout << sec << endl;
  481. struct timeval tv;
  482. tv.tv_sec = sec;
  483. //cout << tv.tv_sec << endl;
  484. if (settimeofday(&tv, NULL) < 0)
  485. {
  486. return false;
  487. }
  488. return true;
  489. }
  490. bool set_system_time_by_sec(int sec)
  491. {
  492. struct timeval tv;
  493. gettimeofday(&tv, NULL);
  494. tv.tv_sec += sec;
  495. //cout << tv.tv_sec << endl;
  496. if (settimeofday(&tv, NULL) < 0)
  497. {
  498. return false;
  499. }
  500. return true;
  501. }
  502. TIME* get_system_time()
  503. {
  504. TIME* t = new TIME();
  505. time_t timep;
  506. time(&timep);
  507. struct tm* p;
  508. p = localtime(&timep);
  509. t->year = 1900 + p->tm_year;
  510. t->month = 1 + p->tm_mon;
  511. t->day = p->tm_mday;
  512. t->hour = p->tm_hour;
  513. t->minute = p->tm_min;
  514. t->second = p->tm_sec;
  515. return t;
  516. }
  517. string time2str(const TIME* tim) {
  518. if (tim == NULL) return "";
  519. string ret;
  520. char timeStr[16] = { 0 };
  521. sprintf(timeStr, "%04d%02d%02d%02d%02d%02d", tim->year,
  522. tim->month, tim->day, tim->hour, tim->minute, tim->second);
  523. ret = timeStr;
  524. return ret;
  525. }
  526. TIME* str2time(string str) {
  527. if (str.length() < 14) return NULL;
  528. TIME* ret = new TIME();
  529. ret->year = str2int(str.substr(0, 4));
  530. ret->month = str2int(str.substr(4, 2));
  531. ret->day = str2int(str.substr(6, 2));
  532. ret->hour = str2int(str.substr(8, 2));
  533. ret->minute = str2int(str.substr(10, 2));
  534. ret->second = str2int(str.substr(12, 2));
  535. return ret;
  536. }
  537. #endif // __linux__