#include "stdafx.h" #include "ControllerHelper.h" #include #include namespace OTSController { /// /// Gets the name of the folder. /// /// The full path. /// the folder name 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; } /// /// Gets the name of the file. /// /// The full path. /// the file name CString CControllerHelper::GetFileName(LPCTSTR fullPath) { return PathFindFileName(fullPath); } /// /// Get file extension /// /// The full path. /// /// Return the file extension /// CString CControllerHelper::GetFileExtension(LPCTSTR fullPath) { return PathFindExtension(fullPath); } /// /// Get system error string /// /// The err. /// /// Return the system error string /// 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 CControllerHelper::SplitSTDString(std::string& a_sSource, char a_cTok) { std::vector tokens; std::stringstream ss(a_sSource); std::string tok; while (getline(ss, tok, a_cTok)) { tokens.push_back(tok); } return tokens; } std::vector CControllerHelper::SplitString(const CString& a_sSource, LPCTSTR a_sTok) { std::vector 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; } } }