common_api.cpp 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. #include "stdafx.h"
  2. #include <time.h>
  3. #include "common_api.h"
  4. common_api::common_api(void)
  5. {
  6. m_NotifyEvent = CreateMutexA(NULL, FALSE, NULL);
  7. if (m_NotifyEvent == NULL)
  8. {
  9. printf("CreateMutex error: %d\n", GetLastError());
  10. return;
  11. }
  12. }
  13. common_api::~common_api(void)
  14. {
  15. CloseHandle(m_NotifyEvent);
  16. }
  17. common_api g_common1_for_init;
  18. bool string_2_guid(const char *pstr, GUID &stGuid)
  19. {
  20. string temp;
  21. vector<string> thelist;
  22. if (SplitTo84422222222String(pstr, thelist) == false)
  23. {
  24. return false;
  25. }
  26. temp = "0x";
  27. temp += thelist[0].c_str();
  28. StrToIntT(temp.c_str(), &stGuid.Data1);
  29. //sscanf_s(thelist[0].c_str(), "%08x", &stGuid.Data1);
  30. temp = "0x";
  31. temp += thelist[1].c_str();
  32. StrToIntT(temp.c_str(), &stGuid.Data2);
  33. //sscanf_s(thelist[1].c_str(), "%04x", &stGuid.Data2);
  34. temp = "0x";
  35. temp += thelist[2].c_str();
  36. StrToIntT(temp.c_str(), &stGuid.Data3);
  37. //sscanf_s(thelist[2].c_str(), "%04x", &stGuid.Data3);
  38. for (size_t i = 3; i < 11; i++)
  39. {
  40. //sscanf_s(thelist[i].c_str(), "%02x", &stGuid.Data4[i - 3],2);
  41. temp = "0x";
  42. temp += thelist[i].c_str();
  43. StrToIntT(temp.c_str(), &stGuid.Data4[i - 3]);
  44. }
  45. return true;
  46. }
  47. bool guid_2_string(GUID &stGuid, string &str)
  48. {
  49. char szBuff[MAX_PATH] = { 0 };
  50. sprintf_s(szBuff,"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", stGuid.Data1, stGuid.Data2, stGuid.Data3,
  51. stGuid.Data4[0], stGuid.Data4[1], stGuid.Data4[2], stGuid.Data4[3],
  52. stGuid.Data4[4], stGuid.Data4[5], stGuid.Data4[6], stGuid.Data4[7]);
  53. str = szBuff;
  54. return true;
  55. }
  56. bool AddEnvPath(const char *pPath)
  57. {
  58. DWORD PathLen = 32768;
  59. char *pszPath = new char[PathLen];
  60. DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
  61. if (Len > PathLen)
  62. {
  63. delete[]pszPath;
  64. return false;
  65. }
  66. std::string::size_type pos = 0;
  67. string EnvPath = pszPath;
  68. delete[]pszPath;
  69. //EnvPath.find(pPath, 0);
  70. if ((pos = EnvPath.find(pPath, pos)) == std::string::npos)
  71. {
  72. string NewPath = EnvPath;
  73. NewPath += ";";
  74. NewPath += pPath;
  75. if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
  76. {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. bool DelEnvPath(const char* pPath)
  83. {
  84. DWORD PathLen = 32768;
  85. char* pszPath = new char[PathLen];
  86. DWORD Len = GetEnvironmentVariable("Path", pszPath, PathLen);
  87. if (Len > PathLen)
  88. {
  89. delete[]pszPath;
  90. return false;
  91. }
  92. std::string::size_type pos = 0;
  93. string EnvPath = pszPath;
  94. delete[]pszPath;
  95. //EnvPath.find(pPath, 0);
  96. if ((pos = EnvPath.find(pPath, pos)) > 0)
  97. {
  98. string NewPath = EnvPath;
  99. //NewPath += ";";
  100. //NewPath += pPath;
  101. NewPath.erase(pos, pos + strlen(pPath) + 1);
  102. if (SetEnvironmentVariable("Path", NewPath.c_str()) == FALSE)
  103. {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. __time64_t GetCurrentRealTimeOfStk()
  110. {
  111. __time64_t now;
  112. _time64(&now);
  113. return now;
  114. }
  115. UINT64 GetDay(UINT64 TheTime)
  116. {
  117. struct tm tCur;
  118. _gmtime64_s(&tCur, (__time64_t*)&TheTime);
  119. tCur.tm_sec = 0;
  120. tCur.tm_min = 0;
  121. tCur.tm_hour = 0;
  122. __time64_t CurDay = _mkgmtime64(&tCur);
  123. return (UINT64)CurDay;
  124. }
  125. int CompareTimeByDay(UINT64 Prev, UINT64 Cur)
  126. {
  127. struct tm tPrev,tCur;
  128. _gmtime64_s(&tPrev, (__time64_t*)&Prev);
  129. tPrev.tm_sec = 0;
  130. tPrev.tm_min = 0;
  131. tPrev.tm_hour = 0;
  132. __time64_t PrevDay = _mkgmtime64(&tPrev);
  133. _gmtime64_s(&tCur, (__time64_t*)&Cur);
  134. tCur.tm_sec = 0;
  135. tCur.tm_min = 0;
  136. tCur.tm_hour = 0;
  137. __time64_t CurDay = _mkgmtime64(&tPrev);
  138. if (CurDay == PrevDay)
  139. {
  140. return 0;
  141. }
  142. if (CurDay > PrevDay)
  143. {
  144. return 1;
  145. }
  146. return -1;
  147. }
  148. bool getBusIdFromFilePath(const char *pFilePath,string &BusId)
  149. {
  150. string Path = pFilePath;
  151. size_t readcount = 0;
  152. size_t firstHit = Path.find_first_of('/');
  153. if(firstHit == string::npos || firstHit != 0)
  154. {
  155. return false;
  156. }
  157. size_t SecondHit = Path.find_first_of('/',1);
  158. if(SecondHit == string::npos)
  159. {
  160. BusId = Path.substr(1,Path.size()-1);
  161. }
  162. else
  163. {
  164. BusId = Path.substr(1,SecondHit - 1);
  165. }
  166. if(BusId.size() == 0)
  167. {
  168. return false;
  169. }
  170. return true;
  171. }
  172. void RawToHex(const char *pRaw, DWORD RawLen, string &Hex)
  173. {
  174. char szMsg[4] = { 0 };
  175. Hex.reserve(RawLen * 3);
  176. for (DWORD i = 0; i < RawLen; i++)
  177. {
  178. _snprintf_s(szMsg, 3, "%02X ", pRaw[i]);
  179. Hex += szMsg;
  180. }
  181. }
  182. bool MergeDevicePath(vector<string> &nodes,string &Devpath)
  183. {
  184. Devpath = "";
  185. for (size_t i = 0; i < nodes.size(); i++)
  186. {
  187. if (nodes[i].size() > 0)
  188. {
  189. Devpath += "/";
  190. Devpath += nodes[i];
  191. }
  192. }
  193. return (Devpath.size() > 0);
  194. }
  195. bool SplitStringSub(string &Path, char key, vector<string> &nodes)
  196. {
  197. string node;
  198. size_t readcount = 0;
  199. //nodes.clear();
  200. string::size_type firstHit = Path.find_first_of(key);
  201. if (firstHit == string::npos)
  202. {
  203. if (Path.size() > 0)
  204. {
  205. nodes.push_back(Path);
  206. }
  207. return true;
  208. }
  209. //firstHit += 1;
  210. if (firstHit > 0)
  211. {
  212. node = Path.substr(0, firstHit);
  213. if (node.size() > 0)
  214. {
  215. nodes.push_back(node);
  216. }
  217. }
  218. while (firstHit < Path.size())
  219. {
  220. firstHit += 1;
  221. string::size_type SecondHit = Path.find_first_of(key, firstHit);
  222. if (SecondHit == string::npos)
  223. {
  224. node = Path.substr(firstHit, Path.size() - (firstHit));
  225. if (node.size() > 0)
  226. {
  227. nodes.push_back(node);
  228. }
  229. return true;
  230. }
  231. node = Path.substr(firstHit, SecondHit - (firstHit));
  232. if (node.size() > 0)
  233. {
  234. nodes.push_back(node);
  235. }
  236. firstHit = SecondHit;
  237. }
  238. return (nodes.size() > 0);
  239. }
  240. bool SplitString(const char *pString,string args, vector<string> &nodes)
  241. {
  242. vector<string> req;
  243. vector<string> res;
  244. req.push_back(string(pString));
  245. for (size_t i = 0; i < args.size(); i++)
  246. {
  247. //cut all req by key
  248. for (size_t j = 0; j < req.size(); j++)
  249. {
  250. SplitStringSub(req[j], args[i], res);
  251. }
  252. //clear and reset
  253. req.clear();
  254. req = res;
  255. res.clear();
  256. }
  257. nodes = req;
  258. return true;
  259. }
  260. bool SplitTo84422222222String(const char *pString, vector<string> &nodes)
  261. {
  262. string total;
  263. vector<string> thelist;
  264. SplitString(pString, string("{}-"), thelist);
  265. for (size_t i = 0; i < thelist.size(); i++)
  266. {
  267. total += thelist[i];
  268. }
  269. if (total.size() != 32)
  270. {
  271. return false;
  272. }
  273. string temp;
  274. temp = total.substr(0, 8);//8
  275. nodes.push_back(temp);
  276. temp = total.substr(8, 4);//4
  277. nodes.push_back(temp);
  278. temp = total.substr(12, 4);//4
  279. nodes.push_back(temp);
  280. temp = total.substr(16, 2);//2
  281. nodes.push_back(temp);
  282. temp = total.substr(18, 2);//2
  283. nodes.push_back(temp);
  284. temp = total.substr(20, 2);//2
  285. nodes.push_back(temp);
  286. temp = total.substr(22, 2);//2
  287. nodes.push_back(temp);
  288. temp = total.substr(24, 2);//2
  289. nodes.push_back(temp);
  290. temp = total.substr(26, 2);//2
  291. nodes.push_back(temp);
  292. temp = total.substr(28, 2);//2
  293. nodes.push_back(temp);
  294. temp = total.substr(30, 2);//2
  295. nodes.push_back(temp);
  296. return true;
  297. }
  298. string FormatstdString(const char *fmt, ...)
  299. {
  300. std::string strResult = "";
  301. WaitForSingleObject(g_common1_for_init.m_NotifyEvent, INFINITE);
  302. {
  303. if (NULL != fmt)
  304. {
  305. va_list marker = NULL;
  306. va_start(marker, fmt);
  307. size_t nLength = _vscprintf(fmt, marker) + 1;
  308. std::vector<char> vBuffer(nLength, '\0');
  309. int nWritten = vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
  310. if (nWritten > 0)
  311. {
  312. strResult = &vBuffer[0];
  313. }
  314. va_end(marker);
  315. }
  316. ReleaseMutex(g_common1_for_init.m_NotifyEvent);
  317. }
  318. return strResult;
  319. }
  320. bool SplitDevicePath(const char *pDevicePath, vector<string> &nodes)
  321. {
  322. string node;
  323. string Path = pDevicePath;
  324. size_t readcount = 0;
  325. nodes.clear();
  326. string::size_type firstHit = Path.find_first_of('/');
  327. if (firstHit == string::npos || firstHit != 0)
  328. {
  329. return false;
  330. }
  331. while (firstHit < Path.size())
  332. {
  333. firstHit += 1;
  334. string::size_type SecondHit = Path.find_first_of('/', firstHit);
  335. if (SecondHit == string::npos)
  336. {
  337. node = Path.substr(firstHit, Path.size() - firstHit);
  338. if (node.size() > 0)
  339. {
  340. nodes.push_back(node);
  341. }
  342. return true;
  343. }
  344. node = Path.substr(firstHit, SecondHit - (firstHit));
  345. if (node.size() > 0)
  346. {
  347. nodes.push_back(node);
  348. }
  349. firstHit = SecondHit;
  350. }
  351. return (nodes.size() > 0);
  352. }
  353. bool SplitDeviceList(const char* pDevicePath, vector<string>& nodes)
  354. {
  355. string node;
  356. string Path = pDevicePath;
  357. size_t readcount = 0;
  358. nodes.clear();
  359. string::size_type firstHit = Path.find_first_of(';');
  360. if (firstHit == string::npos )
  361. {
  362. nodes.push_back(pDevicePath);
  363. return true;
  364. }
  365. else
  366. {
  367. node = Path.substr(0, (firstHit));
  368. nodes.push_back(node);
  369. }
  370. while (firstHit < Path.size())
  371. {
  372. firstHit += 1;
  373. string::size_type SecondHit = Path.find_first_of(';', firstHit);
  374. if (SecondHit == string::npos)
  375. {
  376. node = Path.substr(firstHit, Path.size() - firstHit);
  377. nodes.push_back(node);
  378. return true;
  379. }
  380. node = Path.substr(firstHit, SecondHit - (firstHit));
  381. nodes.push_back(node);
  382. firstHit = SecondHit;
  383. }
  384. return (nodes.size() > 0);
  385. }
  386. string GetProcessDirectory()
  387. {
  388. string ret = "";
  389. char szFilename[MAX_PATH] = { 0 };
  390. DWORD res = GetModuleFileNameA(0, szFilename, MAX_PATH);
  391. if (res == 0)
  392. {
  393. return ret;
  394. }
  395. string fullpath = szFilename;
  396. string::size_type firstHit = fullpath.find_last_of('\\');
  397. if (firstHit == string::npos || firstHit == 0)
  398. {
  399. return ret;
  400. }
  401. ret = fullpath.substr(0, firstHit);//kick last \
  402. return ret;
  403. }
  404. //fullpath must be like X:\filetitle.x.y.z...,or X:/filetitle.x.y.z...
  405. string GetFileTitle(string &fullpath)
  406. {
  407. string ret = "";
  408. string::size_type firstHit = fullpath.find_last_of('\\');
  409. string::size_type firstHitSec = fullpath.find_last_of('/');
  410. if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
  411. {
  412. return ret;
  413. }
  414. if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
  415. {
  416. ret = fullpath.substr(firstHitSec + 1);
  417. }
  418. else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
  419. {
  420. ret = fullpath.substr(firstHit + 1);
  421. }
  422. else
  423. {
  424. //effective both
  425. if (firstHit > firstHitSec)
  426. {
  427. ret = fullpath.substr(firstHit + 1);//kick last
  428. }
  429. else
  430. {
  431. ret = fullpath.substr(firstHitSec + 1);//kick last
  432. }
  433. }
  434. //got filetitle with ext
  435. if (ret.size() > 0)
  436. {
  437. string::size_type firstHit = ret.find_first_of('.');
  438. ret = ret.substr(0, firstHit);
  439. }
  440. return ret;
  441. }
  442. string GetFileName(string &fullpath)
  443. {
  444. string ret = "";
  445. string::size_type firstHit = fullpath.find_last_of('\\');
  446. string::size_type firstHitSec = fullpath.find_last_of('/');
  447. if ((firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size())) && (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size())))
  448. {
  449. return ret;
  450. }
  451. if (firstHit == string::npos || firstHit == 0 || (firstHit + 1 == fullpath.size()))
  452. {
  453. ret = fullpath.substr(firstHitSec + 1);
  454. }
  455. else if (firstHitSec == string::npos || firstHitSec == 0 || (firstHitSec + 1 == fullpath.size()))
  456. {
  457. ret = fullpath.substr(firstHit + 1);
  458. }
  459. else
  460. {
  461. //effective both
  462. if (firstHit > firstHitSec)
  463. {
  464. ret = fullpath.substr(firstHit + 1);//kick last
  465. }
  466. else
  467. {
  468. ret = fullpath.substr(firstHitSec + 1);//kick last
  469. }
  470. }
  471. //got filetitle with ext
  472. //if (ret.size() > 0)
  473. //{
  474. // string::size_type ExtHit = ret.find_last_of('.');
  475. // if (ExtHit == 0 || ExtHit == string::npos || ((ExtHit + 1) == fullpath.size()))
  476. // {
  477. // ret = "";
  478. // }
  479. // else
  480. // {
  481. // ret = ret.substr(ExtHit + 1);
  482. // }
  483. //}
  484. return ret;
  485. }
  486. string ReplaceFileTitle(string FilePath, string NewTitle)
  487. {
  488. string::size_type ExtHit = FilePath.find_last_of('.');
  489. if (ExtHit != string::npos)
  490. {
  491. FilePath.replace(ExtHit + 1, string::npos, NewTitle);
  492. }
  493. return FilePath;
  494. }
  495. bool CreateFileDirectory(string &FullFilePath)
  496. {
  497. string path = GetFileDirectory(FullFilePath);
  498. string workpath = GetProcessDirectory() + string("\\");
  499. transform(path.begin(), path.end(), path.begin(), tolower);
  500. transform(workpath.begin(), workpath.end(), workpath.begin(), tolower);
  501. vector<string> folders;
  502. string subpath = ReplaceSubString(path, workpath, string(""));
  503. SplitString(subpath.c_str(), string("/\\"), folders);
  504. for (size_t i = 0; i < folders.size(); i++)
  505. {
  506. workpath += folders[i] + string("\\");
  507. DWORD attr = GetFileAttributes(workpath.c_str());
  508. if (attr == (DWORD)-1)
  509. {
  510. if (CreateDirectory(workpath.c_str(), 0) == 0)
  511. {
  512. return false;
  513. }
  514. }
  515. if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
  516. {
  517. return false;
  518. }
  519. }
  520. return true;
  521. }
  522. string GetFileDirectory(string &fullpath)
  523. {
  524. string ret = "";
  525. string::size_type firstHit = fullpath.find_last_of('\\');
  526. string::size_type firstHitSec = fullpath.find_last_of('/');
  527. if ((firstHit == string::npos || firstHit == 0) && (firstHitSec == string::npos || firstHitSec == 0))
  528. {
  529. return ret;
  530. }
  531. if (firstHit == string::npos || firstHit == 0)
  532. {
  533. ret = fullpath.substr(0, firstHitSec);//kick last
  534. }
  535. else if (firstHitSec == string::npos || firstHitSec == 0)
  536. {
  537. ret = fullpath.substr(0, firstHit);//kick last
  538. }
  539. else
  540. {
  541. //effective both
  542. if (firstHit > firstHitSec)
  543. {
  544. ret = fullpath.substr(0, firstHit);//kick last
  545. }
  546. else
  547. {
  548. ret = fullpath.substr(0, firstHitSec);//kick last
  549. }
  550. }
  551. return ret;
  552. }
  553. string ReplaceSubString(string org, string &keystr, string &replacestr)
  554. {
  555. std::string::size_type pos = 0;
  556. std::string::size_type keylen = keystr.size();
  557. std::string::size_type replen = replacestr.size();
  558. while ((pos = org.find(keystr, pos)) != std::string::npos)
  559. {
  560. org.replace(pos, keylen, replacestr);
  561. pos += replen;
  562. }
  563. return org;
  564. }
  565. bool FindSortedSubFiles(string rootFile, vector<string> &filelist)
  566. {
  567. WIN32_FIND_DATA fd;
  568. map<string,FILETIME> sortmap;
  569. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  570. if (rootFile.size() == 0)
  571. {
  572. return false;
  573. }
  574. HANDLE hFile;
  575. BOOL bRet = TRUE;
  576. string rootDir = GetFileDirectory(rootFile);
  577. if (rootDir[rootDir.size() - 1] != '\\')
  578. {
  579. rootDir += "\\";
  580. }
  581. string subpath = rootFile;
  582. subpath += ".*";//log file looks like xxx.log.20161107_185132
  583. hFile = FindFirstFile(subpath.c_str(), &fd);
  584. while (hFile != INVALID_HANDLE_VALUE && bRet)
  585. {
  586. if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
  587. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  588. {
  589. //ignore dir
  590. }
  591. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  592. {
  593. //ignore it
  594. }
  595. else
  596. {
  597. //full file name
  598. subpath = rootDir;
  599. subpath += fd.cFileName;
  600. sortmap[subpath] = fd.ftLastWriteTime;
  601. }
  602. bRet = FindNextFile(hFile, &fd);
  603. }
  604. FindClose(hFile);
  605. //sort it
  606. while (sortmap.size() > 0)
  607. {
  608. ULARGE_INTEGER fTime1 = { 0 };
  609. map<string, FILETIME>::iterator itermax;
  610. map<string, FILETIME>::iterator iter = sortmap.begin();
  611. while (iter != sortmap.end())
  612. {
  613. ULARGE_INTEGER *pfTime2 = (ULARGE_INTEGER *)&(iter->second);
  614. if (fTime1.QuadPart < (*pfTime2).QuadPart)
  615. {
  616. itermax = iter;
  617. }
  618. ++iter;
  619. }
  620. //got bigest one
  621. filelist.push_back(itermax->first);
  622. //del iter
  623. sortmap.erase(itermax);
  624. }
  625. return (filelist.size() > 0);
  626. }
  627. bool FindSubVersionDirs(string rootDir, map<string,vector<string>>& dirlist)
  628. {
  629. WIN32_FIND_DATA fd;
  630. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  631. if (rootDir.size() == 0)
  632. {
  633. return false;
  634. }
  635. HANDLE hFile;
  636. BOOL bRet = TRUE;
  637. if (rootDir[rootDir.size() - 1] != '\\')
  638. {
  639. rootDir += "\\";
  640. }
  641. string subpath = rootDir;
  642. //define the format of the basepath
  643. subpath += "*";
  644. hFile = FindFirstFile(subpath.c_str(), &fd);
  645. while (hFile != INVALID_HANDLE_VALUE && bRet)
  646. {
  647. if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
  648. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  649. {
  650. string TheDir = rootDir;
  651. TheDir += fd.cFileName;
  652. dirlist[fd.cFileName].push_back(TheDir);
  653. }
  654. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  655. {
  656. //ignore it
  657. }
  658. else
  659. {
  660. //ignore it
  661. }
  662. bRet = FindNextFile(hFile, &fd);
  663. }
  664. FindClose(hFile);
  665. return (dirlist.size() > 0);
  666. }
  667. bool FindSubFiles(string rootDir, vector<string> &filelist,bool Recursive, string strWildcard)
  668. {
  669. WIN32_FIND_DATA fd;
  670. ZeroMemory(&fd, sizeof(WIN32_FIND_DATA));
  671. if (rootDir.size() == 0)
  672. {
  673. return false;
  674. }
  675. HANDLE hFile;
  676. BOOL bRet = TRUE;
  677. if (rootDir[rootDir.size() - 1] != '\\')
  678. {
  679. rootDir += "\\";
  680. }
  681. string subpath = rootDir;
  682. //define the format of the basepath
  683. subpath += strWildcard;
  684. hFile = FindFirstFile(subpath.c_str(), &fd);
  685. while (hFile != INVALID_HANDLE_VALUE && bRet)
  686. {
  687. if ((fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) &&
  688. strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
  689. {
  690. if (Recursive)
  691. {
  692. subpath = rootDir;
  693. subpath += fd.cFileName;
  694. FindSubFiles(subpath, filelist);
  695. }
  696. }
  697. else if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
  698. {
  699. //ignore it
  700. }
  701. else
  702. {
  703. //full file name
  704. subpath = rootDir;
  705. subpath += fd.cFileName;
  706. filelist.push_back(subpath);
  707. }
  708. bRet = FindNextFile(hFile, &fd);
  709. }
  710. FindClose(hFile);
  711. return (filelist.size() > 0);
  712. }
  713. string & makeLowerStr(string & src)
  714. {
  715. size_t len = src.size();
  716. for (size_t i = 0; i < len; i++)
  717. {
  718. src.at(i) = tolower(src.at(i));
  719. }
  720. return src;
  721. }
  722. std::vector<export_functions> GetDllFunctionInfo(const char* moduleName)
  723. {
  724. HMODULE hModule = LoadLibraryA(moduleName);
  725. if (hModule == nullptr)
  726. return {};
  727. const IMAGE_DOS_HEADER* pDosHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(hModule);
  728. const IMAGE_NT_HEADERS* pNtHeader = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<BYTE*>(hModule) + pDosHeader->e_lfanew);
  729. const IMAGE_EXPORT_DIRECTORY* pExportDirectory = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(reinterpret_cast<BYTE*>(
  730. hModule) + pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
  731. const DWORD* pAddressOfFunctions = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->
  732. AddressOfFunctions);
  733. const DWORD* pAddressOfNames = reinterpret_cast<DWORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNames);
  734. const WORD* pAddressOfNameOrdinals = reinterpret_cast<WORD*>(reinterpret_cast<BYTE*>(hModule) + pExportDirectory->AddressOfNameOrdinals);
  735. // 返回格式 [[函数名, 函数地址, 函数序号], ...]
  736. std::vector<export_functions> result;
  737. for (DWORD i = 0; i < pExportDirectory->NumberOfNames; i++)
  738. {
  739. const char* pName = reinterpret_cast<char*>(reinterpret_cast<BYTE*>(hModule) + pAddressOfNames[i]);
  740. const DWORD dwAddress = pAddressOfFunctions[pAddressOfNameOrdinals[i]];
  741. const DWORD dwOrdinal = pAddressOfNameOrdinals[i] + pExportDirectory->Base;
  742. // 根据自身需求,选择是否直接函数内打印
  743. // std::cout << "Function Name: " << pName << std::endl;
  744. // std::cout << "Function Address: " << dwAddress << std::endl;
  745. // std::cout << "Function Ordinal: " << dwOrdinal << std::endl;
  746. // std::cout << std::endl;
  747. result.push_back(std::make_tuple( pName, dwAddress, dwOrdinal) );
  748. }
  749. return result;
  750. }
  751. void TransferModuleJosnConfig2DriverConfig(ResDataObject& config)
  752. {
  753. for (int x = 0; x < config.size(); x++)
  754. {
  755. //如果有Value
  756. if (config[x].GetKeyCount("Value") > 0)
  757. {
  758. //FINFO("TRY COVERT [{$}] VALUE {$}", config.GetKey(x), config[x]["Value"].size() > 0 ? config[x]["Value"].encode() : (const char*)config[x]["Value"]);
  759. if (config[x]["Value"].size() <= 0)
  760. {
  761. string va = (const char*)config[x]["Value"];
  762. config[x] = va.c_str();
  763. }
  764. else
  765. {
  766. ResDataObject rest = config[x]["Value"];
  767. config[x] = rest;
  768. //FINFO("convert object [{$}], object {$}", config.GetKey(x), rest.encode());
  769. }
  770. }
  771. //FINFO("After Convert {$}", config.encode());
  772. }
  773. }
  774. std::string DatetimeToString(time_t timeVal)
  775. {
  776. // 将time_t转换为本地时间的tm结构体
  777. struct tm* localTime = localtime(&timeVal);
  778. if (localTime == nullptr)
  779. {
  780. // 处理转换失败的情况,返回空字符串或错误标识
  781. return "";
  782. }
  783. // 缓冲区,足够容纳格式化后的时间字符串(包含终止符)
  784. char timeStr[32] = { 0 };
  785. // 格式化时间字符串:年-月-日 时:分:秒
  786. // %Y:四位年份,%m:两位月份,%d:两位日期
  787. // %H:24小时制小时,%M:分钟,%S:秒
  788. strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localTime);
  789. return std::string(timeStr);
  790. }