OTSHelper.cpp 27 KB

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