ControllerHelper.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "stdafx.h"
  2. #include "ControllerHelper.h"
  3. #include <strsafe.h>
  4. #include <sstream>
  5. namespace OTSController {
  6. /// <summary>
  7. /// Gets the name of the folder.
  8. /// </summary>
  9. /// <param name="fullPath">The full path.</param>
  10. /// <returns>the folder name</returns>
  11. CString CControllerHelper::GetFolderName(CString fullPath)
  12. {
  13. //int nLen = min(MAX_PATH, fullPath.GetLength());
  14. TCHAR sPath[MAX_PATH];
  15. _tcscpy_s(sPath, MAX_PATH, fullPath);
  16. //CString dirName = (LPWSTR)fullPath;
  17. PathRemoveFileSpec(sPath);
  18. return sPath;
  19. }
  20. CString CControllerHelper::GetFileNameWithoutExtension(CString fullPath)
  21. {
  22. TCHAR sPath[MAX_PATH + 4];
  23. _tcscpy_s(sPath, MAX_PATH, fullPath);
  24. PathRemoveExtension(sPath);
  25. return sPath;
  26. }
  27. /// <summary>
  28. /// Gets the name of the file.
  29. /// </summary>
  30. /// <param name="fullPath">The full path.</param>
  31. /// <returns>the file name</returns>
  32. CString CControllerHelper::GetFileName(LPCTSTR fullPath)
  33. {
  34. return PathFindFileName(fullPath);
  35. }
  36. /// <summary>
  37. /// Get file extension
  38. /// </summary>
  39. /// <param name="fullPath">The full path.</param>
  40. /// <returns>
  41. /// Return the file extension
  42. /// </returns>
  43. CString CControllerHelper::GetFileExtension(LPCTSTR fullPath)
  44. {
  45. return PathFindExtension(fullPath);
  46. }
  47. /// <summary>
  48. /// Get system error string
  49. /// </summary>
  50. /// <param name="err">The err.</param>
  51. /// <returns>
  52. /// Return the system error string
  53. /// </returns>
  54. LPCTSTR CControllerHelper::GetSystemErrorString(DWORD err)
  55. {
  56. LPTSTR sErr;
  57. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  58. FORMAT_MESSAGE_FROM_SYSTEM,
  59. NULL,
  60. err,
  61. 0,
  62. (LPTSTR)&sErr,
  63. 0,
  64. NULL);
  65. return sErr;
  66. }
  67. // Strings to double.
  68. BOOL CControllerHelper::StringToDouble(LPCTSTR a_sValue, double& a_dValue)
  69. {
  70. if (!IsDoubleString(a_sValue))
  71. {
  72. LogErrorTrace(__FILE__, __LINE__, _T("StringToDouble: value string (%s) is not digit string."), a_sValue);
  73. return FALSE;
  74. }
  75. a_dValue = _ttof(a_sValue);
  76. return TRUE;
  77. }
  78. // Determines whether is double string.
  79. BOOL CControllerHelper::IsDoubleString(LPCTSTR a_sValue)
  80. {
  81. CString strDouble = a_sValue;
  82. strDouble.Trim();
  83. if (strDouble.IsEmpty())
  84. {
  85. LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string is an empty string."));
  86. return FALSE;
  87. }
  88. int nStart = 0;
  89. // if there is negative value
  90. if (strDouble[nStart] == _T('-'))
  91. {
  92. ++nStart;
  93. }
  94. // cycle through string and check each character if it is a digit
  95. BOOL bDot = FALSE;
  96. for (; nStart < strDouble.GetLength(); ++nStart)
  97. {
  98. if (!isdigit_t(strDouble[nStart]))
  99. {
  100. if (!bDot && strDouble[nStart] == _T('.'))
  101. {
  102. bDot = TRUE;
  103. }
  104. else
  105. {
  106. LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string (%s) is not a double string."), strDouble);
  107. return FALSE;
  108. }
  109. }
  110. }
  111. return TRUE;
  112. }
  113. int CControllerHelper::WCharToChar(const wchar_t* a_psSource, char* a_psDest)
  114. {
  115. size_t iRet = 0;
  116. size_t nLen = wcslen(a_psSource) * 2 + 2;
  117. wcstombs_s(&iRet, a_psDest, nLen, a_psSource, nLen);
  118. return (int)iRet;
  119. }
  120. int CControllerHelper::CharToWChar(const char* a_psSource, wchar_t* a_psDest)
  121. {
  122. size_t iRet = 0;
  123. size_t nLen = strlen(a_psSource) + 1;
  124. mbstowcs_s(&iRet, a_psDest, nLen, a_psSource, nLen);
  125. return (int)iRet;
  126. }
  127. CString CControllerHelper::CharToString(const char* a_psSource)
  128. {
  129. size_t nLen = strlen(a_psSource) + 1;
  130. wchar_t* psDest = new wchar_t[nLen];
  131. CString sRet(_T(""));
  132. if (CharToWChar(a_psSource, psDest) > 0)
  133. {
  134. sRet = psDest;
  135. }
  136. delete[] psDest;
  137. return sRet;
  138. }
  139. DWORD CControllerHelper::ConvStreamToByteArr(IStream *stream, BYTE **byte)
  140. {
  141. DWORD dwSize = 0;
  142. LARGE_INTEGER move = { 0 };
  143. STATSTG stats = { 0 };
  144. stream->Stat(&stats, 0);
  145. dwSize = (DWORD)stats.cbSize.QuadPart;
  146. *byte = new BYTE[dwSize];
  147. stream->Seek(move, STREAM_SEEK_SET, NULL);
  148. stream->Read((void*)*byte, dwSize, NULL);
  149. return dwSize;
  150. }
  151. std::vector<std::string> CControllerHelper::SplitSTDString(std::string& a_sSource, char a_cTok)
  152. {
  153. std::vector<std::string> tokens;
  154. std::stringstream ss(a_sSource);
  155. std::string tok;
  156. while (getline(ss, tok, a_cTok))
  157. {
  158. tokens.push_back(tok);
  159. }
  160. return tokens;
  161. }
  162. std::vector<CString> CControllerHelper::SplitString(const CString& a_sSource, LPCTSTR a_sTok)
  163. {
  164. std::vector<CString> vSplitString;
  165. int nPosLast = 0;
  166. auto nPos = a_sSource.Find(a_sTok, nPosLast);
  167. while (nPos >= nPosLast)
  168. {
  169. if (nPos == nPosLast)
  170. {
  171. vSplitString.push_back(_T(""));
  172. nPosLast++;
  173. }
  174. else
  175. {
  176. CString sSplitValue = a_sSource.Mid(nPosLast, nPos - nPosLast);
  177. sSplitValue.Trim();
  178. vSplitString.push_back(sSplitValue);
  179. nPosLast = nPos + 1;
  180. }
  181. nPos = a_sSource.Find(a_sTok, nPosLast);
  182. }
  183. // push the last one
  184. CString sSplitValue = a_sSource.Right(a_sSource.GetLength() - nPosLast);
  185. sSplitValue.Trim();
  186. vSplitString.push_back(sSplitValue);
  187. return vSplitString;
  188. }
  189. void CControllerHelper::EnsureStringLengthNoMoreThan(CString& ioString, int inMaxLength)
  190. {
  191. auto len = ioString.GetLength();
  192. if (len > inMaxLength)
  193. {
  194. ioString.Truncate(inMaxLength);
  195. }
  196. }
  197. void CControllerHelper::TrimSTDString(std::string& a_sString, char a_cTrimChar /*= ' '*/)
  198. {
  199. size_t first = a_sString.find_first_not_of(a_cTrimChar);
  200. size_t last = a_sString.find_last_not_of(a_cTrimChar);
  201. if (first != std::string::npos || last != std::string::npos)
  202. {
  203. auto sTrimString = a_sString.substr(first, (last - first + 1));
  204. a_sString = sTrimString;
  205. }
  206. }
  207. }