OTSHelper.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. // OTSHelper.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "OTSHelper.h"
  5. #include "OTSModel.h"
  6. #include "HardDriveSerialNumber.h"
  7. #include "COTSUtilityDllFunExport.h"
  8. #include <strsafe.h>
  9. #include <sstream>
  10. // COTSHelper
  11. namespace OTSMODEL {
  12. COTSHelper::COTSHelper()
  13. {
  14. }
  15. COTSHelper::~COTSHelper()
  16. {
  17. }
  18. // COTSHelper member functions
  19. // gets the name of the folder
  20. CString COTSHelper::GetFolderName(CString a_strPathName)
  21. {
  22. TCHAR sPath[MAX_PATH];
  23. _tcscpy_s(sPath, MAX_PATH, a_strPathName);
  24. ::PathRemoveFileSpec(sPath);
  25. return sPath;
  26. }
  27. // get file name without extension
  28. CString COTSHelper::GetFileNameWithoutExtension(CString a_strPathName)
  29. {
  30. TCHAR sPath[MAX_PATH + 4];
  31. _tcscpy_s(sPath, MAX_PATH, a_strPathName);
  32. ::PathRemoveExtension(sPath);
  33. return sPath;
  34. }
  35. // Gets the name of the file.
  36. CString COTSHelper::GetFileName(LPCTSTR a_strPathName)
  37. {
  38. return ::PathFindFileName(a_strPathName);
  39. }
  40. // Get file extension
  41. CString COTSHelper::GetFileExtension(LPCTSTR a_strPathName)
  42. {
  43. return ::PathFindExtension(a_strPathName);
  44. }
  45. // Get system error string
  46. LPCTSTR COTSHelper::GetSystemErrorString(DWORD a_nErr)
  47. {
  48. LPTSTR strErr;
  49. ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  50. FORMAT_MESSAGE_FROM_SYSTEM,
  51. NULL,
  52. a_nErr,
  53. 0,
  54. (LPTSTR)&strErr,
  55. 0,
  56. NULL);
  57. return strErr;
  58. }
  59. // Strings to int.
  60. BOOL COTSHelper::StringToInt(LPCTSTR a_sValue, int& a_nValue)
  61. {
  62. if (!IsDigitString(a_sValue))
  63. {
  64. LogErrorTrace(__FILE__, __LINE__, _T("StringToInt: value string (%s) is not digit string."), a_sValue);
  65. return FALSE;
  66. }
  67. a_nValue = _ttoi(a_sValue);
  68. return TRUE;
  69. }
  70. // Strings to double.
  71. BOOL COTSHelper::StringToDouble(LPCTSTR a_sValue, double& a_dValue)
  72. {
  73. if (!IsDoubleString(a_sValue))
  74. {
  75. LogErrorTrace(__FILE__, __LINE__, _T("StringToDouble: value string (%s) is not digit string."), a_sValue);
  76. return FALSE;
  77. }
  78. a_dValue = _ttof(a_sValue);
  79. return TRUE;
  80. }
  81. // Determines whether is digit string.
  82. BOOL COTSHelper::IsDigitString(LPCTSTR a_sValue)
  83. {
  84. CString strInt = a_sValue;
  85. strInt.Trim();
  86. if (strInt.IsEmpty())
  87. {
  88. LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string is an empty string."));
  89. return FALSE;
  90. }
  91. int nStart = 0;
  92. if (strInt[nStart] == _T('-'))
  93. {
  94. ++nStart;
  95. }
  96. // cycle through string and check each character if it is a digit
  97. for (; nStart < strInt.GetLength(); ++nStart)
  98. {
  99. if (!isdigit_t(strInt[nStart]))
  100. {
  101. LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string (%s) is not a digit string."), strInt);
  102. return FALSE;
  103. }
  104. }
  105. return TRUE;
  106. }
  107. // Determines whether is double string.
  108. BOOL COTSHelper::IsDoubleString(LPCTSTR a_sValue)
  109. {
  110. CString strDouble = a_sValue;
  111. strDouble.Trim();
  112. if (strDouble.IsEmpty())
  113. {
  114. LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string is an empty string."));
  115. return FALSE;
  116. }
  117. int nStart = 0;
  118. // if there is negative value
  119. if (strDouble[nStart] == _T('-'))
  120. {
  121. ++nStart;
  122. }
  123. // cycle through string and check each character if it is a digit
  124. BOOL bDot = FALSE;
  125. for (; nStart < strDouble.GetLength(); ++nStart)
  126. {
  127. if (!isdigit_t(strDouble[nStart]))
  128. {
  129. if (!bDot && strDouble[nStart] == _T('.'))
  130. {
  131. bDot = TRUE;
  132. }
  133. else
  134. {
  135. LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string (%s) is not a double string."), strDouble);
  136. return FALSE;
  137. }
  138. }
  139. }
  140. return TRUE;
  141. }
  142. // is CTRL key pressed
  143. BOOL COTSHelper::IsCtrlKeyPressed()
  144. {
  145. return (::GetKeyState(VK_CONTROL) < 0);
  146. }
  147. // is shift key pressed
  148. BOOL COTSHelper::IsShiftKeyPressed()
  149. {
  150. return (::GetKeyState(VK_SHIFT) < 0);
  151. }
  152. // Saves the bitmap to file.
  153. // return TRUE if successful, FALSE otherwise.
  154. BOOL COTSHelper::SaveBitmapToFile(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileName)
  155. {
  156. // input file check
  157. ASSERT(a_pBitmap);
  158. if (!a_pBitmap)
  159. {
  160. LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_pBitmap.");
  161. return FALSE;
  162. }
  163. // file name check
  164. CString strPathName = a_pFileName;
  165. strPathName.Trim();
  166. if (strPathName.IsEmpty())
  167. {
  168. LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: file name can't be empty string.");
  169. return FALSE;
  170. }
  171. // file extension
  172. CString strFileExt = GetFileExtension(a_pFileName);
  173. CString strClisdFormat(_T("image/"));
  174. if (strFileExt.IsEmpty())
  175. {
  176. strClisdFormat += _T("bmp");
  177. }
  178. else
  179. {
  180. strClisdFormat += strFileExt.Right(strFileExt.GetLength() - 1);
  181. }
  182. // Get encoder CLSID
  183. CLSID CLSIDImage;
  184. WCHAR* psClisdFormat = (WCHAR*)strClisdFormat.GetBuffer(strClisdFormat.GetLength());
  185. if (COTSHelper::GetEncoderClsid(psClisdFormat, &CLSIDImage) < 0)
  186. {
  187. LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Couldn't find CLSID for bitmap image: %s"), (LPCTSTR)strClisdFormat);
  188. ASSERT(FALSE);
  189. return FALSE;
  190. }
  191. // save
  192. WCHAR* psFileName = (WCHAR*)strPathName.GetBuffer(strPathName.GetLength());
  193. Gdiplus::Status ret = a_pBitmap->Save(psFileName, &CLSIDImage);
  194. if (ret != Gdiplus::Ok)
  195. {
  196. LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Save bitmap image failed, return %d."), (int)ret);
  197. ASSERT(FALSE);
  198. return FALSE;
  199. }
  200. // Ok, return TRUE
  201. return TRUE;
  202. }
  203. // Saves the bitmap to stream.
  204. // return TRUE if successful, FALSE otherwise.
  205. BOOL COTSHelper::SaveBitmapToStream(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileType, IStream *a_stream)
  206. {
  207. ASSERT(a_pBitmap);
  208. if (!a_pBitmap)
  209. {
  210. LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_pBitmap");
  211. return FALSE;
  212. }
  213. ASSERT(a_stream);
  214. if (!a_stream)
  215. {
  216. LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_stream");
  217. return FALSE;
  218. }
  219. // file extension
  220. CString strFileExt = a_pFileType;
  221. CString strClisdFormat(_T("image/"));
  222. if (strFileExt.IsEmpty())
  223. {
  224. strClisdFormat += _T("bmp");
  225. }
  226. else
  227. {
  228. strClisdFormat += strFileExt.Right(strFileExt.GetLength());
  229. }
  230. // Get encoder CLSID
  231. CLSID CLSIDImage;
  232. WCHAR* psClisdFormat = (WCHAR*)strClisdFormat.GetBuffer(strClisdFormat.GetLength());
  233. if (COTSHelper::GetEncoderClsid(psClisdFormat, &CLSIDImage) < 0)
  234. {
  235. LogErrorTrace(__FILE__, __LINE__, ("SaveBitmapToStream: Couldn't find CLSID for bitmap image: %s"), (LPCTSTR)strClisdFormat);
  236. ASSERT(FALSE);
  237. return FALSE;
  238. }
  239. // save
  240. Gdiplus::Status ret = a_pBitmap->Save(a_stream, &CLSIDImage);
  241. if (ret != Gdiplus::Ok)
  242. {
  243. LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Save bitmap image failed, return %d."), (int)ret);
  244. ASSERT(FALSE);
  245. return FALSE;
  246. }
  247. // Ok, return TRUE
  248. return TRUE;
  249. }
  250. // Get encoder CLSID
  251. // following code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx
  252. // return -1 if fails
  253. int COTSHelper::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  254. {
  255. // number of image encoders
  256. UINT num = 0;
  257. // size of the image encoder array in bytes
  258. UINT size = 0;
  259. Gdiplus::GetImageEncodersSize(&num, &size);
  260. // fails if size of the image encoder array is 0
  261. if (size == 0)
  262. {
  263. LogErrorTrace(__FILE__, __LINE__, _T("GetEncoderClsid: Gdiplus::GetImageEncodersSize size of the image encoder array is 0."));
  264. return -1;
  265. }
  266. // The ImageCodecInfo class provides the necessary storage members and methods to retrieve all pertinent information
  267. // about the installed image encoders and decoders (called coders)
  268. Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
  269. pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
  270. // fails if no enough memory to hold the ImageCodecInfo class
  271. if (pImageCodecInfo == NULL)
  272. {
  273. LogErrorTrace(__FILE__, __LINE__, _T("GetEncoderClsid: failed to malloc for the image. dize %d"), size);
  274. return -1;
  275. }
  276. // gets an array of ImageCodecInfo objects
  277. Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
  278. for (UINT j = 0; j < num; ++j)
  279. {
  280. if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
  281. {
  282. *pClsid = pImageCodecInfo[j].Clsid;
  283. free(pImageCodecInfo);
  284. return j; // Success
  285. }
  286. }
  287. // fails to get encoder CLSID
  288. free(pImageCodecInfo);
  289. return -1; // Failure
  290. }
  291. // Finds the index of the string list.
  292. int COTSHelper::FindStringListIndex(CStringList& a_listStrList, LPCTSTR a_strItem, BOOL a_bNoCase /*= true*/)
  293. {
  294. POSITION pos = a_listStrList.GetHeadPosition();
  295. int index = 0;
  296. while (pos)
  297. {
  298. auto sValue = a_listStrList.GetNext(pos);
  299. if (a_bNoCase)
  300. {
  301. if (sValue.CompareNoCase(a_strItem) == 0)
  302. {
  303. return index;
  304. }
  305. }
  306. else
  307. {
  308. if (sValue.Compare(a_strItem) == 0)
  309. {
  310. return index;
  311. }
  312. }
  313. index++;
  314. }
  315. // can't find the string, return -1
  316. return -1;
  317. }
  318. // Finds the index of the string list.
  319. int COTSHelper::FindStringListIndex(std::vector<CString>& a_stringList, LPCTSTR a_sItem, BOOL a_bNoCase /*= true*/)
  320. {
  321. int index = 0;
  322. for (auto sValue : a_stringList)
  323. {
  324. if (a_bNoCase)
  325. {
  326. if (sValue.CompareNoCase(a_sItem) == 0)
  327. {
  328. return index;
  329. }
  330. }
  331. else
  332. {
  333. if (sValue.Compare(a_sItem) == 0)
  334. {
  335. return index;
  336. }
  337. }
  338. index++;
  339. }
  340. // can't find the string, return -1
  341. return -1;
  342. }
  343. // open file
  344. BOOL COTSHelper::ShellOpenFile(LPCTSTR a_strPathName)
  345. {
  346. return ShellExecuteCommand(a_strPathName);
  347. }
  348. BOOL COTSHelper::ShellExecuteCommand(LPCTSTR a_strPathName, LPCTSTR a_strParam /*= NULL*/)
  349. {
  350. // pathname can't be an empty string
  351. CString strPathName = a_strPathName;
  352. ASSERT(strPathName);
  353. if (strPathName.IsEmpty())
  354. {
  355. LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: a_strPathName is an empty string."));
  356. return FALSE;
  357. }
  358. CString strParam = a_strParam;
  359. if (!strParam.IsEmpty())
  360. {
  361. LogTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: open %s, param %s."), strPathName, strParam);
  362. }
  363. else
  364. {
  365. LogTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: open %s."), strPathName);
  366. }
  367. // "ShellExecute" Performs an operation on a specified file
  368. // open the file
  369. HINSTANCE result = ShellExecute(NULL, _T("open"), strPathName, a_strParam, NULL, SW_SHOWNORMAL);
  370. #pragma warning(disable: 4311)
  371. #pragma warning(disable: 4302)
  372. int nResult = reinterpret_cast<int>(result);
  373. #pragma warning(default: 4311)
  374. #pragma warning(default: 4302)
  375. if (nResult <= 32)
  376. {
  377. // error, open file failed
  378. switch (nResult)
  379. {
  380. case ERROR_FILE_NOT_FOUND:
  381. // The specified file was not found.
  382. case ERROR_PATH_NOT_FOUND:
  383. // The specified path was not found.
  384. LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: can't find the path/file %s."), strPathName );
  385. break;
  386. default:
  387. // The operating system denied access to the specified file.
  388. LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: the operating system denied access to the file %s."), strPathName);
  389. }
  390. // return FALSE
  391. ASSERT(FALSE);
  392. return FALSE;
  393. }
  394. // ok, return TRUE
  395. return TRUE;
  396. }
  397. // get machine id
  398. CString COTSHelper::GetMachineId()
  399. {
  400. // machine id string
  401. CString strMachineId;
  402. MasterHardDiskSerial hardDiskSerial;
  403. // get computer hardware id (hard disk id)
  404. if (hardDiskSerial.getHardDriveComputerID() != 0)
  405. {
  406. // get computer hardware id
  407. strMachineId = hardDiskSerial.GetHardDriveComputerIdAsString();
  408. }
  409. else
  410. {
  411. // failed to get hardware id, using default one
  412. LogErrorTrace(__FILE__, __LINE__, _T("GetMachineId: could not identify the computer id."));
  413. strMachineId = hardDiskSerial.GetDefaultComputerIdString();
  414. }
  415. // return machine id string
  416. return strMachineId;
  417. }
  418. // send email
  419. BOOL COTSHelper::SendEmail(LPCTSTR a_strEmailAddress, LPCTSTR a_strTitle, LPCTSTR a_strBody)
  420. {
  421. // form email string
  422. CString strEmail;
  423. strEmail.Format(_T("mailto:%s?Subject=%s&body=%s"), a_strEmailAddress, a_strTitle, a_strBody);
  424. // send email
  425. HINSTANCE result = ShellExecute(NULL, _T("open"), strEmail, NULL, NULL, SW_SHOW);
  426. // From MSDN Documentation, ShellExecute returns >32 on success, or an error code <= 32 otherwise
  427. #pragma warning(disable: 4311)
  428. #pragma warning(disable: 4302)
  429. int nResult = reinterpret_cast<int>(result);
  430. #pragma warning(default: 4311)
  431. #pragma warning(default: 4302)
  432. return nResult > 32;
  433. }
  434. // string conventions
  435. int COTSHelper::CharToWChar(const char* a_psSource, wchar_t* a_psTarget)
  436. {
  437. size_t iRet = 0;
  438. size_t nLen = strlen(a_psSource) + 1;
  439. mbstowcs_s(&iRet, a_psTarget, nLen, a_psSource, nLen);
  440. return (int)iRet;
  441. }
  442. int COTSHelper::WCharToChar(const wchar_t* a_psSource, char* a_psTarget)
  443. {
  444. size_t iRet = 0;
  445. size_t nLen = wcslen(a_psSource) * 2 + 2;
  446. wcstombs_s(&iRet, a_psTarget, nLen, a_psSource, nLen);
  447. return (int)iRet;
  448. }
  449. CString COTSHelper::CharToString(const char* a_psSource)
  450. {
  451. size_t nLen = strlen(a_psSource) + 1;
  452. wchar_t* psDest = new wchar_t[nLen];
  453. CString sRet(_T(""));
  454. if (CharToWChar(a_psSource, psDest) > 0)
  455. {
  456. sRet = psDest;
  457. }
  458. delete[] psDest;
  459. return sRet;
  460. }
  461. DWORD COTSHelper::ConvStreamToByteArr(IStream *stream, BYTE **byte)
  462. {
  463. DWORD dwSize = 0;
  464. LARGE_INTEGER move = { 0 };
  465. STATSTG stats = { 0 };
  466. stream->Stat(&stats, 0);
  467. dwSize = (DWORD)stats.cbSize.QuadPart;
  468. *byte = new BYTE[dwSize];
  469. stream->Seek(move, STREAM_SEEK_SET, NULL);
  470. stream->Read((void*)*byte, dwSize, NULL);
  471. return dwSize;
  472. }
  473. // time strings
  474. // return "-" if the date time is invalid
  475. CString COTSHelper::GetDateTimeString(const COleDateTime& a_DateTime)
  476. {
  477. auto nStatus = a_DateTime.GetStatus();
  478. if (nStatus == COleDateTime::valid && a_DateTime != 0)
  479. {
  480. return a_DateTime.Format(_T("%H:%M:%S %d-%m-%y"));
  481. }
  482. return _T("-");
  483. }
  484. CString COTSHelper::GetTimeSpanString(const COleDateTimeSpan& a_TimeSpan, BOOL a_bFixLenth /*= FALSE*/)
  485. {
  486. CString strRet;
  487. if (a_bFixLenth)
  488. {
  489. strRet.Format(_T("%dh:%dm:%ds"), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds());
  490. }
  491. if (a_TimeSpan.GetDays() > 0)
  492. {
  493. strRet.Format(_T("%dd %dh:%dm:%ds"), a_TimeSpan.GetDays(), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds());
  494. }
  495. else if (a_TimeSpan.GetHours() > 0)
  496. {
  497. strRet.Format(_T("%dh:%dm:%ds"), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds());
  498. }
  499. else if (a_TimeSpan.GetMinutes() > 0)
  500. {
  501. strRet.Format(_T("%dm:%ds"), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds());
  502. }
  503. else
  504. {
  505. strRet.Format(_T("%ds"), a_TimeSpan.GetSeconds());
  506. }
  507. return strRet;
  508. }
  509. COleDateTime COTSHelper::GetDateTimeFromString(const CString & a_DateTime)
  510. {
  511. COleDateTime ole_time;
  512. ole_time.ParseDateTime(a_DateTime);
  513. return ole_time;
  514. }
  515. COleDateTimeSpan COTSHelper::GetTimeSpanFromString(const CString & a_TimeSpan, BOOL a_bFixLenth)
  516. {
  517. COleDateTimeSpan span;
  518. int days=0, hours=0, minutes=0, seconds=0;
  519. if (a_TimeSpan.Find(" ") != 0)
  520. {
  521. std::vector <CString > tString = SplitString(a_TimeSpan, " ");
  522. days = std::stoi( tString[0].GetBuffer());
  523. std::vector<CString> tString1 = SplitString(tString[1], ":");
  524. hours = std::stoi(tString1[0].GetBuffer());
  525. minutes = std::stoi(tString1[1].GetBuffer());
  526. seconds = std::stoi(tString1[2].GetBuffer());
  527. }
  528. else
  529. {
  530. std::vector<CString> tString1 = SplitString(a_TimeSpan, ":");
  531. hours = std::stoi(tString1[0].GetBuffer());
  532. minutes = std::stoi(tString1[1].GetBuffer());
  533. seconds = std::stoi(tString1[2].GetBuffer());
  534. }
  535. span.SetDateTimeSpan(days, hours, minutes, seconds);
  536. return span;
  537. }
  538. // waiting
  539. void COTSHelper::Waiting(long a_nMilliseconds)
  540. {
  541. #pragma warning(disable: 28159)
  542. DWORD nStart = GetTickCount();
  543. DWORD nEnd = nStart;
  544. do
  545. {
  546. nEnd = GetTickCount();
  547. } while (nEnd >= nStart && nEnd <= (nStart + a_nMilliseconds + 1));
  548. #pragma warning(default: 28159)
  549. }
  550. // Get file version
  551. // File Version should be format <Major version>.<Minor version>.<Build version>, like 1.2.3
  552. DWORD COTSHelper::GetVersionFromString(LPCTSTR a_sVersion)
  553. {
  554. int nMajorVersion = 0;
  555. int nMinorVersion = 0;
  556. int nBuildVersion = 0;
  557. DWORD nVersion = 0;
  558. if( GetMajorVersionFromString(a_sVersion, nMajorVersion) &&
  559. GetMinorVersionFromString(a_sVersion, nMinorVersion) &&
  560. GetBuildVersionFromString(a_sVersion, nBuildVersion))
  561. {
  562. nVersion = nMajorVersion * 10000 + nMinorVersion * 100 + nBuildVersion;
  563. }
  564. return nVersion;
  565. }
  566. BOOL COTSHelper::GetMajorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion)
  567. {
  568. // version string
  569. CString strVersion(a_sVersion);
  570. // get major file version string position
  571. int nPosFirst = strVersion.Find(_T('.'));
  572. if (nPosFirst <= 0)
  573. {
  574. // failed to find major file version
  575. LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to find the major file version string: %s"), strVersion);
  576. return FALSE;
  577. }
  578. // get major file version string
  579. CString strMajorVersion = strVersion.Left(nPosFirst);
  580. // the major file version string can't be empty
  581. strMajorVersion.Trim();
  582. if (strMajorVersion.IsEmpty())
  583. {
  584. // failed to find major file version
  585. LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to find the major file version string: %s"), strVersion);
  586. return FALSE;
  587. }
  588. // convert the major version string to major version number
  589. if (!COTSHelper::StringToInt(strMajorVersion, a_nVersion))
  590. {
  591. // failed to convert the major file version string to number
  592. LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to convert the major file version string to number: %s (%s)."), strMajorVersion, strVersion);
  593. return FALSE;
  594. }
  595. // ok, return TRUE
  596. return TRUE;
  597. }
  598. BOOL COTSHelper::GetMinorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion)
  599. {
  600. // version string
  601. CString strVersion(a_sVersion);
  602. // get major file version string position first
  603. int nPosFirst = strVersion.Find(_T('.'));
  604. if (nPosFirst < 0)
  605. {
  606. // failed to find major file version string
  607. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find major version string: %s."), strVersion);
  608. return FALSE;
  609. }
  610. // get minor file version string position
  611. int nPosSecont = strVersion.Find(_T('.'), nPosFirst + 1);
  612. int nStrLength = nPosSecont - nPosFirst - 1;
  613. if (nStrLength <= 0)
  614. {
  615. // failed to find minor file version string
  616. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find minor file version string: %s."), strVersion);
  617. return FALSE;
  618. }
  619. // get minor file version string
  620. CString strMinorVersion = strVersion.Mid(nPosFirst + 1, nStrLength);
  621. // the minor file version string can't be empty
  622. strMinorVersion.Trim();
  623. if (strMinorVersion.IsEmpty())
  624. {
  625. // failed to find minor file version string
  626. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find minor file version string: %s."), strVersion);
  627. return FALSE;
  628. }
  629. // convert the minor version string to minor version number
  630. if (!COTSHelper::StringToInt(strMinorVersion, a_nVersion))
  631. {
  632. // failed to convert the major file version string to number
  633. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to convert the minor file version string to number: %s (%s)."), strMinorVersion, strVersion);
  634. return FALSE;
  635. }
  636. // ok, return TRUE
  637. return TRUE;
  638. }
  639. BOOL COTSHelper::GetBuildVersionFromString(LPCTSTR a_sVersion, int& a_nBuild)
  640. {
  641. // version string
  642. CString strVersion(a_sVersion);
  643. // get major file version string position first
  644. int nPosFirst = strVersion.Find(_T('.'));
  645. if (nPosFirst < 0)
  646. {
  647. // failed to find major file version string
  648. LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find major version string: %s."), strVersion);
  649. return FALSE;
  650. }
  651. // get minor file version string position
  652. int nPosSecond = strVersion.Find(_T('.'), nPosFirst + 1);
  653. if (nPosSecond < 0)
  654. {
  655. // failed to find minor file version string
  656. LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find minor file version string: %s."), strVersion);
  657. return FALSE;
  658. }
  659. // get build string position
  660. int nStrLength = strVersion.GetLength() - nPosSecond - 1;
  661. if (nStrLength <= 0)
  662. {
  663. // failed to find build string
  664. LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find build string: %s."), strVersion);
  665. return FALSE;
  666. }
  667. // get build string
  668. CString strBuild = strVersion.Right(nStrLength);
  669. // build string can't be empty
  670. strBuild.Trim();
  671. if (strBuild.IsEmpty())
  672. {
  673. // failed to convert the build string to number
  674. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find the build string: %s."), strVersion);
  675. return FALSE;
  676. }
  677. // convert the build string to build number
  678. if (!COTSHelper::StringToInt(strBuild, a_nBuild))
  679. {
  680. // failed to convert the build string to number
  681. LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to convert the build string to number: %s (%s)."), strBuild, strVersion);
  682. return FALSE;
  683. }
  684. // ok, return TRUE
  685. return TRUE;
  686. }
  687. // get file modify string
  688. CString COTSHelper::GetFileWriteTime(LPCTSTR a_strPathName)
  689. {
  690. // file modify string
  691. CString strFileModifyString = _T("");
  692. // get file information structure
  693. WIN32_FIND_DATA FileStruct;
  694. HANDLE hfile;
  695. hfile = FindFirstFile(a_strPathName, &FileStruct);
  696. // check if found the file ok
  697. if (hfile == INVALID_HANDLE_VALUE)
  698. {
  699. // failed to find the file
  700. LogErrorTrace(__FILE__, __LINE__, _T("GetFileWriteTime: failed to find the file: %s."), a_strPathName);
  701. return strFileModifyString;
  702. }
  703. // last modify time
  704. FILETIME ft = FileStruct.ftLastWriteTime;
  705. // get file modify string
  706. CTime time(ft);
  707. strFileModifyString.Format(_T("%d/%d/%d %d:%d"), time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute());
  708. // return file modify string
  709. return strFileModifyString;
  710. }
  711. // load text file
  712. std::vector<std::string> COTSHelper::LoadTextFileToSTDList(CString a_strPathName, int a_nLine/*= -1*/)
  713. {
  714. // string list
  715. std::vector<std::string> listStr;
  716. // load
  717. try
  718. {
  719. // open the file
  720. FILE *fStream;
  721. errno_t err = _tfopen_s(&fStream, a_strPathName, _T("rt,ccs=UNICODE"));
  722. if (err != 0) return listStr; // failed..CString sRead;
  723. CStdioFile file(fStream);
  724. // read the file
  725. CString strLine;
  726. int nLine = 0;
  727. while (file.ReadString(strLine) && nLine != a_nLine)
  728. {
  729. // get a line
  730. // remove comments
  731. int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT);
  732. if (nCommentPos != -1)
  733. {
  734. // remove comments
  735. strLine = strLine.Left(nCommentPos);
  736. }
  737. // process the line
  738. strLine.Trim();
  739. // jump over empty lines
  740. if (strLine)
  741. {
  742. continue;
  743. }
  744. // add the string into string list
  745. std::string strSTDLine((LPCTSTR)strLine);
  746. listStr.push_back(strSTDLine);
  747. // got a line
  748. ++nLine;
  749. }
  750. }
  751. catch (CFileException* pe)
  752. {
  753. pe->Delete();
  754. return listStr;
  755. }
  756. // return string list
  757. return listStr;
  758. }
  759. std::vector<CString> COTSHelper::LoadTextFileToCStingList(CString a_strPathName, int a_nLine /*= -1*/)
  760. {
  761. // string list
  762. std::vector<CString> listStr;
  763. // load
  764. try
  765. {
  766. // open the file
  767. CStdioFile file;
  768. file.Open(a_strPathName, CFile::modeRead | CFile::shareDenyWrite);
  769. // read the file
  770. CString strLine;
  771. int nLine = 0;
  772. while (file.ReadString(strLine) && nLine != a_nLine)
  773. {
  774. // get a line
  775. // remove comments
  776. int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT);
  777. if (nCommentPos != -1)
  778. {
  779. // remove comments
  780. strLine = strLine.Left(nCommentPos);
  781. }
  782. // process the line
  783. strLine.Trim();
  784. // jump over empty lines
  785. if (strLine.IsEmpty())
  786. {
  787. continue;
  788. }
  789. listStr.push_back(strLine);
  790. }
  791. file.Close();
  792. }
  793. catch (CFileException* pe)
  794. {
  795. pe->Delete();
  796. return listStr;
  797. }
  798. // return string list
  799. return listStr;
  800. }
  801. // split a string
  802. // std::string& a_sSource -- source string
  803. // char a_cTok a_cSep -- separator
  804. std::vector<std::string> COTSHelper::SplitSTDString(std::string& a_sSource, char a_cSep)
  805. {
  806. // string list
  807. std::vector<std::string> listStr;
  808. std::stringstream strSource(a_sSource);
  809. std::string str;
  810. // seperate a string from the source string
  811. while (getline(strSource, str, a_cSep))
  812. {
  813. // get the string ok, add the string into the string list
  814. listStr.push_back(str);
  815. }
  816. // return string list
  817. return listStr;
  818. }
  819. // const CString& a_sSource
  820. // LPCTSTR a_sSep -- separator
  821. std::vector<CString> COTSHelper::SplitString(const CString& a_strSource, LPCTSTR a_strSep)
  822. {
  823. // string list
  824. std::vector<CString> listString;
  825. // source string
  826. CString strSource = a_strSource;
  827. // find the first separator
  828. int nPosLast = 0;
  829. auto nPos = strSource.Find(a_strSep, nPosLast);
  830. // found the separator?
  831. while (nPos >= nPosLast)
  832. {
  833. // there is no string between two seperator if nPos == nPosLast
  834. if (nPos == nPosLast)
  835. {
  836. listString.push_back(_T(""));
  837. nPosLast++;
  838. }
  839. else
  840. {
  841. // get the string between two separator
  842. CString strValue = strSource.Mid(nPosLast, nPos - nPosLast);
  843. strValue.Trim();
  844. // add the string into the string list
  845. listString.push_back(strValue);
  846. nPosLast = nPos + 1;
  847. }
  848. // try to find the next separator
  849. nPos = strSource.Find(a_strSep, nPosLast);
  850. }
  851. // push the last one into the string list
  852. CString strLastValue = strSource.Right(strSource.GetLength() - nPosLast);
  853. strLastValue.Trim();
  854. listString.push_back(strLastValue);
  855. // return the string list
  856. return listString;
  857. }
  858. // cut of the string to make sure that it will be never over the length
  859. void COTSHelper::EnsureStringLengthNoMoreThan(CString& a_str, int a_nMaxLength)
  860. {
  861. auto len = a_str.GetLength();
  862. if (len > a_nMaxLength)
  863. {
  864. a_str.Truncate(a_nMaxLength);
  865. }
  866. }
  867. // trim std string
  868. void COTSHelper::TrimSTDString(std::string& a_sString, char a_cTrimChar /*= ' '*/)
  869. {
  870. size_t first = a_sString.find_first_not_of(a_cTrimChar);
  871. size_t last = a_sString.find_last_not_of(a_cTrimChar);
  872. if (first != std::string::npos || last != std::string::npos)
  873. {
  874. auto sTrimString = a_sString.substr(first, (last - first + 1));
  875. a_sString = sTrimString;
  876. }
  877. }
  878. // get file name list in a folder
  879. BOOL COTSHelper::GetFileNameList(CString a_strFolderName, CString a_strFileType,std::vector<CString>& a_listFileName)
  880. {
  881. // get file name
  882. a_strFolderName += "*";
  883. a_strFolderName += a_strFileType;
  884. try
  885. {
  886. //find first file
  887. WIN32_FIND_DATA FindFileData;
  888. HANDLE file = FindFirstFile(a_strFolderName.GetBuffer(), &FindFileData);
  889. //find other file
  890. if (file != INVALID_HANDLE_VALUE)
  891. {
  892. a_listFileName.push_back(FindFileData.cFileName);
  893. BOOL bState = FALSE;
  894. bState = FindNextFile(file, &FindFileData);
  895. while (bState)
  896. {
  897. a_listFileName.push_back(FindFileData.cFileName);
  898. bState = FindNextFile(file, &FindFileData);
  899. }
  900. }
  901. }
  902. catch (CFileException* pe)
  903. {
  904. pe->Delete();
  905. return FALSE;
  906. }
  907. return TRUE;
  908. }
  909. std::string COTSHelper::GetSystemTime()
  910. {
  911. SYSTEMTIME m_time;
  912. GetLocalTime(&m_time);
  913. char szDateTime[100] = { 0 };
  914. sprintf_s(szDateTime, "%02d-%02d-%02d %02d:%02d:%02d", m_time.wYear, m_time.wMonth,
  915. m_time.wDay, m_time.wHour, m_time.wMinute, m_time.wSecond);
  916. std::string time(szDateTime);
  917. return time;
  918. }
  919. }