123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #include "stdafx.h"
- #include "ControllerHelper.h"
- #include <strsafe.h>
- #include <sstream>
- namespace OTSController {
- /// <summary>
- /// Gets the name of the folder.
- /// </summary>
- /// <param name="fullPath">The full path.</param>
- /// <returns>the folder name</returns>
- CString CControllerHelper::GetFolderName(CString fullPath)
- {
- //int nLen = min(MAX_PATH, fullPath.GetLength());
- TCHAR sPath[MAX_PATH];
- _tcscpy_s(sPath, MAX_PATH, fullPath);
- //CString dirName = (LPWSTR)fullPath;
- PathRemoveFileSpec(sPath);
- return sPath;
- }
- CString CControllerHelper::GetFileNameWithoutExtension(CString fullPath)
- {
- TCHAR sPath[MAX_PATH + 4];
- _tcscpy_s(sPath, MAX_PATH, fullPath);
- PathRemoveExtension(sPath);
- return sPath;
- }
- /// <summary>
- /// Gets the name of the file.
- /// </summary>
- /// <param name="fullPath">The full path.</param>
- /// <returns>the file name</returns>
- CString CControllerHelper::GetFileName(LPCTSTR fullPath)
- {
- return PathFindFileName(fullPath);
- }
- /// <summary>
- /// Get file extension
- /// </summary>
- /// <param name="fullPath">The full path.</param>
- /// <returns>
- /// Return the file extension
- /// </returns>
- CString CControllerHelper::GetFileExtension(LPCTSTR fullPath)
- {
- return PathFindExtension(fullPath);
- }
- /// <summary>
- /// Get system error string
- /// </summary>
- /// <param name="err">The err.</param>
- /// <returns>
- /// Return the system error string
- /// </returns>
- LPCTSTR CControllerHelper::GetSystemErrorString(DWORD err)
- {
- LPTSTR sErr;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- err,
- 0,
- (LPTSTR)&sErr,
- 0,
- NULL);
- return sErr;
- }
-
- // Strings to double.
- BOOL CControllerHelper::StringToDouble(LPCTSTR a_sValue, double& a_dValue)
- {
- if (!IsDoubleString(a_sValue))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("StringToDouble: value string (%s) is not digit string."), a_sValue);
- return FALSE;
- }
- a_dValue = _ttof(a_sValue);
- return TRUE;
- }
- // Determines whether is double string.
- BOOL CControllerHelper::IsDoubleString(LPCTSTR a_sValue)
- {
- CString strDouble = a_sValue;
- strDouble.Trim();
- if (strDouble.IsEmpty())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string is an empty string."));
- return FALSE;
- }
- int nStart = 0;
- // if there is negative value
- if (strDouble[nStart] == _T('-'))
- {
- ++nStart;
- }
- // cycle through string and check each character if it is a digit
- BOOL bDot = FALSE;
- for (; nStart < strDouble.GetLength(); ++nStart)
- {
- if (!isdigit_t(strDouble[nStart]))
- {
- if (!bDot && strDouble[nStart] == _T('.'))
- {
- bDot = TRUE;
- }
- else
- {
- LogErrorTrace(__FILE__, __LINE__, _T("IsDoubleString: value string (%s) is not a double string."), strDouble);
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- int CControllerHelper::WCharToChar(const wchar_t* a_psSource, char* a_psDest)
- {
- size_t iRet = 0;
- size_t nLen = wcslen(a_psSource) * 2 + 2;
- wcstombs_s(&iRet, a_psDest, nLen, a_psSource, nLen);
- return (int)iRet;
- }
- int CControllerHelper::CharToWChar(const char* a_psSource, wchar_t* a_psDest)
- {
- size_t iRet = 0;
- size_t nLen = strlen(a_psSource) + 1;
- mbstowcs_s(&iRet, a_psDest, nLen, a_psSource, nLen);
- return (int)iRet;
- }
- CString CControllerHelper::CharToString(const char* a_psSource)
- {
- size_t nLen = strlen(a_psSource) + 1;
- wchar_t* psDest = new wchar_t[nLen];
- CString sRet(_T(""));
- if (CharToWChar(a_psSource, psDest) > 0)
- {
- sRet = psDest;
- }
- delete[] psDest;
- return sRet;
- }
- DWORD CControllerHelper::ConvStreamToByteArr(IStream *stream, BYTE **byte)
- {
- DWORD dwSize = 0;
- LARGE_INTEGER move = { 0 };
- STATSTG stats = { 0 };
- stream->Stat(&stats, 0);
- dwSize = (DWORD)stats.cbSize.QuadPart;
- *byte = new BYTE[dwSize];
- stream->Seek(move, STREAM_SEEK_SET, NULL);
- stream->Read((void*)*byte, dwSize, NULL);
- return dwSize;
- }
- std::vector<std::string> CControllerHelper::SplitSTDString(std::string& a_sSource, char a_cTok)
- {
- std::vector<std::string> tokens;
- std::stringstream ss(a_sSource);
- std::string tok;
- while (getline(ss, tok, a_cTok))
- {
- tokens.push_back(tok);
- }
- return tokens;
- }
- std::vector<CString> CControllerHelper::SplitString(const CString& a_sSource, LPCTSTR a_sTok)
- {
- std::vector<CString> vSplitString;
- int nPosLast = 0;
- auto nPos = a_sSource.Find(a_sTok, nPosLast);
- while (nPos >= nPosLast)
- {
- if (nPos == nPosLast)
- {
- vSplitString.push_back(_T(""));
- nPosLast++;
- }
- else
- {
- CString sSplitValue = a_sSource.Mid(nPosLast, nPos - nPosLast);
- sSplitValue.Trim();
- vSplitString.push_back(sSplitValue);
- nPosLast = nPos + 1;
- }
- nPos = a_sSource.Find(a_sTok, nPosLast);
- }
- // push the last one
- CString sSplitValue = a_sSource.Right(a_sSource.GetLength() - nPosLast);
- sSplitValue.Trim();
- vSplitString.push_back(sSplitValue);
- return vSplitString;
- }
- void CControllerHelper::EnsureStringLengthNoMoreThan(CString& ioString, int inMaxLength)
- {
- auto len = ioString.GetLength();
- if (len > inMaxLength)
- {
- ioString.Truncate(inMaxLength);
- }
- }
- void CControllerHelper::TrimSTDString(std::string& a_sString, char a_cTrimChar /*= ' '*/)
- {
- size_t first = a_sString.find_first_not_of(a_cTrimChar);
- size_t last = a_sString.find_last_not_of(a_cTrimChar);
- if (first != std::string::npos || last != std::string::npos)
- {
- auto sTrimString = a_sString.substr(first, (last - first + 1));
- a_sString = sTrimString;
- }
- }
- }
|