// OTSHelper.cpp : implementation file // #pragma once #include "stdafx.h" #include "OTSHelper.h" //#include "OTSModel.h" #include "HardDriveSerialNumber.h" #include "COTSUtilityDllFunExport.h" #include #include //using namespace NSLogTools; // COTSHelper namespace OTSTools { const CString OTS_TEXT_FILE_COMMENT = _T("//"); COTSHelper::COTSHelper() { } COTSHelper::~COTSHelper() { } // COTSHelper member functions // gets the name of the folder CString COTSHelper::GetFolderName(CString a_strPathName) { TCHAR sPath[MAX_PATH]; _tcscpy_s(sPath, MAX_PATH, a_strPathName); ::PathRemoveFileSpec(sPath); return sPath; } // get file name without extension CString COTSHelper::GetFileNameWithoutExtension(CString a_strPathName) { TCHAR sPath[MAX_PATH + 4]; _tcscpy_s(sPath, MAX_PATH, a_strPathName); ::PathRemoveExtension(sPath); return sPath; } // Gets the name of the file. CString COTSHelper::GetFileName(LPCTSTR a_strPathName) { return ::PathFindFileName(a_strPathName); } // Get file extension CString COTSHelper::GetFileExtension(LPCTSTR a_strPathName) { return ::PathFindExtension(a_strPathName); } // Get system error string LPCTSTR COTSHelper::GetSystemErrorString(DWORD a_nErr) { LPTSTR strErr; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, a_nErr, 0, (LPTSTR)&strErr, 0, NULL); return strErr; } // Strings to int. BOOL COTSHelper::StringToInt(LPCTSTR a_sValue, int& a_nValue) { if (!IsDigitString(a_sValue)) { LogErrorTrace(__FILE__, __LINE__, _T("StringToInt: value string (%s) is not digit string."), a_sValue); return FALSE; } a_nValue = _ttoi(a_sValue); return TRUE; } // Strings to double. BOOL COTSHelper::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 digit string. BOOL COTSHelper::IsDigitString(LPCTSTR a_sValue) { CString strInt = a_sValue; strInt.Trim(); if (strInt.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string is an empty string.")); return FALSE; } int nStart = 0; if (strInt[nStart] == _T('-')) { ++nStart; } // cycle through string and check each character if it is a digit for (; nStart < strInt.GetLength(); ++nStart) { if (!isdigit_t(strInt[nStart])) { LogErrorTrace(__FILE__, __LINE__, _T("IsDigitString: value string (%s) is not a digit string."), strInt); return FALSE; } } return TRUE; } // Determines whether is double string. BOOL COTSHelper::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; } // is CTRL key pressed BOOL COTSHelper::IsCtrlKeyPressed() { return (::GetKeyState(VK_CONTROL) < 0); } // is shift key pressed BOOL COTSHelper::IsShiftKeyPressed() { return (::GetKeyState(VK_SHIFT) < 0); } // Saves the bitmap to file. // return TRUE if successful, FALSE otherwise. BOOL COTSHelper::SaveBitmapToFile(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileName) { // input file check ASSERT(a_pBitmap); if (!a_pBitmap) { LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_pBitmap."); return FALSE; } // file name check CString strPathName = a_pFileName; strPathName.Trim(); if (strPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: file name can't be empty string."); return FALSE; } // file extension CString strFileExt = GetFileExtension(a_pFileName); CString strClisdFormat(_T("image/")); if (strFileExt.IsEmpty()) { strClisdFormat += _T("bmp"); } else { strClisdFormat += strFileExt.Right(strFileExt.GetLength() - 1); } // Get encoder CLSID CLSID CLSIDImage; WCHAR* psClisdFormat = (WCHAR*)strClisdFormat.GetBuffer(strClisdFormat.GetLength()); if (COTSHelper::GetEncoderClsid(psClisdFormat, &CLSIDImage) < 0) { LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Couldn't find CLSID for bitmap image: %s"), (LPCTSTR)strClisdFormat); ASSERT(FALSE); return FALSE; } // save WCHAR* psFileName = (WCHAR*)strPathName.GetBuffer(strPathName.GetLength()); Gdiplus::Status ret = a_pBitmap->Save(psFileName, &CLSIDImage); if (ret != Gdiplus::Ok) { LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Save bitmap image failed, return %d."), (int)ret); ASSERT(FALSE); return FALSE; } // Ok, return TRUE return TRUE; } // Saves the bitmap to stream. // return TRUE if successful, FALSE otherwise. BOOL COTSHelper::SaveBitmapToStream(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileType, IStream *a_stream) { ASSERT(a_pBitmap); if (!a_pBitmap) { LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_pBitmap"); return FALSE; } ASSERT(a_stream); if (!a_stream) { LogErrorTrace(__FILE__, __LINE__, "SaveBitmapToStream: invalid file a_stream"); return FALSE; } // file extension CString strFileExt = a_pFileType; CString strClisdFormat(_T("image/")); if (strFileExt.IsEmpty()) { strClisdFormat += _T("bmp"); } else { strClisdFormat += strFileExt.Right(strFileExt.GetLength()); } // Get encoder CLSID CLSID CLSIDImage; WCHAR* psClisdFormat = (WCHAR*)strClisdFormat.GetBuffer(strClisdFormat.GetLength()); if (COTSHelper::GetEncoderClsid(psClisdFormat, &CLSIDImage) < 0) { LogErrorTrace(__FILE__, __LINE__, ("SaveBitmapToStream: Couldn't find CLSID for bitmap image: %s"), (LPCTSTR)strClisdFormat); ASSERT(FALSE); return FALSE; } // save Gdiplus::Status ret = a_pBitmap->Save(a_stream, &CLSIDImage); if (ret != Gdiplus::Ok) { LogErrorTrace(__FILE__, __LINE__, _T("SaveBitmapToStream: Save bitmap image failed, return %d."), (int)ret); ASSERT(FALSE); return FALSE; } // Ok, return TRUE return TRUE; } // Get encoder CLSID // following code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx // return -1 if fails int COTSHelper::GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { // number of image encoders UINT num = 0; // size of the image encoder array in bytes UINT size = 0; Gdiplus::GetImageEncodersSize(&num, &size); // fails if size of the image encoder array is 0 if (size == 0) { LogErrorTrace(__FILE__, __LINE__, _T("GetEncoderClsid: Gdiplus::GetImageEncodersSize size of the image encoder array is 0.")); return -1; } // The ImageCodecInfo class provides the necessary storage members and methods to retrieve all pertinent information // about the installed image encoders and decoders (called coders) Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL; pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size)); // fails if no enough memory to hold the ImageCodecInfo class if (pImageCodecInfo == NULL) { LogErrorTrace(__FILE__, __LINE__, _T("GetEncoderClsid: failed to malloc for the image. dize %d"), size); return -1; } // gets an array of ImageCodecInfo objects Gdiplus::GetImageEncoders(num, size, pImageCodecInfo); for (UINT j = 0; j < num; ++j) { if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) { *pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; // Success } } // fails to get encoder CLSID free(pImageCodecInfo); return -1; // Failure } // Finds the index of the string list. int COTSHelper::FindStringListIndex(CStringList& a_listStrList, LPCTSTR a_strItem, BOOL a_bNoCase /*= true*/) { POSITION pos = a_listStrList.GetHeadPosition(); int index = 0; while (pos) { auto sValue = a_listStrList.GetNext(pos); if (a_bNoCase) { if (sValue.CompareNoCase(a_strItem) == 0) { return index; } } else { if (sValue.Compare(a_strItem) == 0) { return index; } } index++; } // can't find the string, return -1 return -1; } // Finds the index of the string list. int COTSHelper::FindStringListIndex(std::vector& a_stringList, LPCTSTR a_sItem, BOOL a_bNoCase /*= true*/) { int index = 0; for (auto sValue : a_stringList) { if (a_bNoCase) { if (sValue.CompareNoCase(a_sItem) == 0) { return index; } } else { if (sValue.Compare(a_sItem) == 0) { return index; } } index++; } // can't find the string, return -1 return -1; } // open file BOOL COTSHelper::ShellOpenFile(LPCTSTR a_strPathName) { return ShellExecuteCommand(a_strPathName); } BOOL COTSHelper::ShellExecuteCommand(LPCTSTR a_strPathName, LPCTSTR a_strParam /*= NULL*/) { // pathname can't be an empty string CString strPathName = a_strPathName; ASSERT(strPathName); if (strPathName.IsEmpty()) { LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: a_strPathName is an empty string.")); return FALSE; } CString strParam = a_strParam; if (!strParam.IsEmpty()) { LogTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: open %s, param %s."), strPathName, strParam); } else { LogTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: open %s."), strPathName); } // "ShellExecute" Performs an operation on a specified file // open the file HINSTANCE result = ShellExecute(NULL, _T("open"), strPathName, a_strParam, NULL, SW_SHOWNORMAL); #pragma warning(disable: 4311) #pragma warning(disable: 4302) int nResult = reinterpret_cast(result); #pragma warning(default: 4311) #pragma warning(default: 4302) if (nResult <= 32) { // error, open file failed switch (nResult) { case ERROR_FILE_NOT_FOUND: // The specified file was not found. case ERROR_PATH_NOT_FOUND: // The specified path was not found. LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: can't find the path/file %s."), strPathName ); break; default: // The operating system denied access to the specified file. LogErrorTrace(__FILE__, __LINE__, _T("ShellExecuteCommand: the operating system denied access to the file %s."), strPathName); } // return FALSE ASSERT(FALSE); return FALSE; } // ok, return TRUE return TRUE; } // get machine id CString COTSHelper::GetMachineId() { // machine id string CString strMachineId; MasterHardDiskSerial hardDiskSerial; // get computer hardware id (hard disk id) if (hardDiskSerial.getHardDriveComputerID() != 0) { // get computer hardware id strMachineId = hardDiskSerial.GetHardDriveComputerIdAsString(); } else { // failed to get hardware id, using default one LogErrorTrace(__FILE__, __LINE__, _T("GetMachineId: could not identify the computer id.")); strMachineId = hardDiskSerial.GetDefaultComputerIdString(); } // return machine id string return strMachineId; } // send email BOOL COTSHelper::SendEmail(LPCTSTR a_strEmailAddress, LPCTSTR a_strTitle, LPCTSTR a_strBody) { // form email string CString strEmail; strEmail.Format(_T("mailto:%s?Subject=%s&body=%s"), a_strEmailAddress, a_strTitle, a_strBody); // send email HINSTANCE result = ShellExecute(NULL, _T("open"), strEmail, NULL, NULL, SW_SHOW); // From MSDN Documentation, ShellExecute returns >32 on success, or an error code <= 32 otherwise #pragma warning(disable: 4311) #pragma warning(disable: 4302) int nResult = reinterpret_cast(result); #pragma warning(default: 4311) #pragma warning(default: 4302) return nResult > 32; } // string conventions int COTSHelper::CharToWChar(const char* a_psSource, wchar_t* a_psTarget) { size_t iRet = 0; size_t nLen = strlen(a_psSource) + 1; mbstowcs_s(&iRet, a_psTarget, nLen, a_psSource, nLen); return (int)iRet; } int COTSHelper::WCharToChar(const wchar_t* a_psSource, char* a_psTarget) { size_t iRet = 0; size_t nLen = wcslen(a_psSource) * 2 + 2; wcstombs_s(&iRet, a_psTarget, nLen, a_psSource, nLen); return (int)iRet; } CString COTSHelper::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 COTSHelper::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; } // time strings // return "-" if the date time is invalid CString COTSHelper::GetDateTimeString(const COleDateTime& a_DateTime) { auto nStatus = a_DateTime.GetStatus(); if (nStatus == COleDateTime::valid && a_DateTime != 0) { return a_DateTime.Format(_T("%H:%M:%S %d-%m-%y")); } return _T("-"); } CString COTSHelper::GetTimeSpanString(const COleDateTimeSpan& a_TimeSpan, BOOL a_bFixLenth /*= FALSE*/) { CString strRet; if (a_bFixLenth) { strRet.Format(_T("%dh:%dm:%ds"), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds()); } if (a_TimeSpan.GetDays() > 0) { strRet.Format(_T("%dd %dh:%dm:%ds"), a_TimeSpan.GetDays(), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds()); } else if (a_TimeSpan.GetHours() > 0) { strRet.Format(_T("%dh:%dm:%ds"), a_TimeSpan.GetHours(), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds()); } else if (a_TimeSpan.GetMinutes() > 0) { strRet.Format(_T("%dm:%ds"), a_TimeSpan.GetMinutes(), a_TimeSpan.GetSeconds()); } else { strRet.Format(_T("%ds"), a_TimeSpan.GetSeconds()); } return strRet; } COleDateTime COTSHelper::GetDateTimeFromString(const CString & a_DateTime) { COleDateTime ole_time; ole_time.ParseDateTime(a_DateTime); return ole_time; } COleDateTimeSpan COTSHelper::GetTimeSpanFromString(const CString & a_TimeSpan, BOOL a_bFixLenth) { COleDateTimeSpan span; int days=0, hours=0, minutes=0, seconds=0; if (a_TimeSpan.Find(" ") != 0) { std::vector tString = SplitString(a_TimeSpan, " "); days = std::stoi( tString[0].GetBuffer()); std::vector tString1 = SplitString(tString[1], ":"); hours = std::stoi(tString1[0].GetBuffer()); minutes = std::stoi(tString1[1].GetBuffer()); seconds = std::stoi(tString1[2].GetBuffer()); } else { std::vector tString1 = SplitString(a_TimeSpan, ":"); hours = std::stoi(tString1[0].GetBuffer()); minutes = std::stoi(tString1[1].GetBuffer()); seconds = std::stoi(tString1[2].GetBuffer()); } span.SetDateTimeSpan(days, hours, minutes, seconds); return span; } // waiting void COTSHelper::Waiting(long a_nMilliseconds) { #pragma warning(disable: 28159) DWORD nStart = GetTickCount(); DWORD nEnd = nStart; do { nEnd = GetTickCount(); } while (nEnd >= nStart && nEnd <= (nStart + a_nMilliseconds + 1)); #pragma warning(default: 28159) } // Get file version // File Version should be format .., like 1.2.3 DWORD COTSHelper::GetVersionFromString(LPCTSTR a_sVersion) { int nMajorVersion = 0; int nMinorVersion = 0; int nBuildVersion = 0; DWORD nVersion = 0; if( GetMajorVersionFromString(a_sVersion, nMajorVersion) && GetMinorVersionFromString(a_sVersion, nMinorVersion) && GetBuildVersionFromString(a_sVersion, nBuildVersion)) { nVersion = nMajorVersion * 10000 + nMinorVersion * 100 + nBuildVersion; } return nVersion; } BOOL COTSHelper::GetMajorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion) { // version string CString strVersion(a_sVersion); // get major file version string position int nPosFirst = strVersion.Find(_T('.')); if (nPosFirst <= 0) { // failed to find major file version LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to find the major file version string: %s"), strVersion); return FALSE; } // get major file version string CString strMajorVersion = strVersion.Left(nPosFirst); // the major file version string can't be empty strMajorVersion.Trim(); if (strMajorVersion.IsEmpty()) { // failed to find major file version LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to find the major file version string: %s"), strVersion); return FALSE; } // convert the major version string to major version number if (!COTSHelper::StringToInt(strMajorVersion, a_nVersion)) { // failed to convert the major file version string to number LogErrorTrace(__FILE__, __LINE__, _T("GetMajorVersionFromString: failed to convert the major file version string to number: %s (%s)."), strMajorVersion, strVersion); return FALSE; } // ok, return TRUE return TRUE; } BOOL COTSHelper::GetMinorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion) { // version string CString strVersion(a_sVersion); // get major file version string position first int nPosFirst = strVersion.Find(_T('.')); if (nPosFirst < 0) { // failed to find major file version string LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find major version string: %s."), strVersion); return FALSE; } // get minor file version string position int nPosSecont = strVersion.Find(_T('.'), nPosFirst + 1); int nStrLength = nPosSecont - nPosFirst - 1; if (nStrLength <= 0) { // failed to find minor file version string LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find minor file version string: %s."), strVersion); return FALSE; } // get minor file version string CString strMinorVersion = strVersion.Mid(nPosFirst + 1, nStrLength); // the minor file version string can't be empty strMinorVersion.Trim(); if (strMinorVersion.IsEmpty()) { // failed to find minor file version string LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find minor file version string: %s."), strVersion); return FALSE; } // convert the minor version string to minor version number if (!COTSHelper::StringToInt(strMinorVersion, a_nVersion)) { // failed to convert the major file version string to number LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to convert the minor file version string to number: %s (%s)."), strMinorVersion, strVersion); return FALSE; } // ok, return TRUE return TRUE; } BOOL COTSHelper::GetBuildVersionFromString(LPCTSTR a_sVersion, int& a_nBuild) { // version string CString strVersion(a_sVersion); // get major file version string position first int nPosFirst = strVersion.Find(_T('.')); if (nPosFirst < 0) { // failed to find major file version string LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find major version string: %s."), strVersion); return FALSE; } // get minor file version string position int nPosSecond = strVersion.Find(_T('.'), nPosFirst + 1); if (nPosSecond < 0) { // failed to find minor file version string LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find minor file version string: %s."), strVersion); return FALSE; } // get build string position int nStrLength = strVersion.GetLength() - nPosSecond - 1; if (nStrLength <= 0) { // failed to find build string LogErrorTrace(__FILE__, __LINE__, _T("GetBuildVersionFromString: failed to find build string: %s."), strVersion); return FALSE; } // get build string CString strBuild = strVersion.Right(nStrLength); // build string can't be empty strBuild.Trim(); if (strBuild.IsEmpty()) { // failed to convert the build string to number LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to find the build string: %s."), strVersion); return FALSE; } // convert the build string to build number if (!COTSHelper::StringToInt(strBuild, a_nBuild)) { // failed to convert the build string to number LogErrorTrace(__FILE__, __LINE__, _T("GetMinorVersionFromString: failed to convert the build string to number: %s (%s)."), strBuild, strVersion); return FALSE; } // ok, return TRUE return TRUE; } // get file modify string CString COTSHelper::GetFileWriteTime(LPCTSTR a_strPathName) { // file modify string CString strFileModifyString = _T(""); // get file information structure WIN32_FIND_DATA FileStruct; HANDLE hfile; hfile = FindFirstFile(a_strPathName, &FileStruct); // check if found the file ok if (hfile == INVALID_HANDLE_VALUE) { // failed to find the file LogErrorTrace(__FILE__, __LINE__, _T("GetFileWriteTime: failed to find the file: %s."), a_strPathName); return strFileModifyString; } // last modify time FILETIME ft = FileStruct.ftLastWriteTime; // get file modify string CTime time(ft); strFileModifyString.Format(_T("%d/%d/%d %d:%d"), time.GetYear(), time.GetMonth(), time.GetDay(), time.GetHour(), time.GetMinute()); // return file modify string return strFileModifyString; } // load text file std::vector COTSHelper::LoadTextFileToSTDList(CString a_strPathName, int a_nLine/*= -1*/) { // string list std::vector listStr; // load try { // open the file FILE *fStream; errno_t err = _tfopen_s(&fStream, a_strPathName, _T("rt,ccs=UNICODE")); if (err != 0) return listStr; // failed..CString sRead; CStdioFile file(fStream); // read the file CString strLine; int nLine = 0; while (file.ReadString(strLine) && nLine != a_nLine) { // get a line // remove comments int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT); if (nCommentPos != -1) { // remove comments strLine = strLine.Left(nCommentPos); } // process the line strLine.Trim(); // jump over empty lines if (strLine) { continue; } // add the string into string list std::string strSTDLine((LPCTSTR)strLine); listStr.push_back(strSTDLine); // got a line ++nLine; } } catch (CFileException* pe) { pe->Delete(); return listStr; } // return string list return listStr; } std::vector COTSHelper::LoadTextFileToCStingList(CString a_strPathName, int a_nLine /*= -1*/) { // string list std::vector listStr; // load try { // open the file CStdioFile file; file.Open(a_strPathName, CFile::modeRead | CFile::shareDenyWrite); // read the file CString strLine; int nLine = 0; while (file.ReadString(strLine) && nLine != a_nLine) { // get a line // remove comments int nCommentPos = strLine.Find(OTS_TEXT_FILE_COMMENT); if (nCommentPos != -1) { // remove comments strLine = strLine.Left(nCommentPos); } // process the line strLine.Trim(); // jump over empty lines if (strLine.IsEmpty()) { continue; } listStr.push_back(strLine); } file.Close(); } catch (CFileException* pe) { pe->Delete(); return listStr; } // return string list return listStr; } // split a string // std::string& a_sSource -- source string // char a_cTok a_cSep -- separator std::vector COTSHelper::SplitSTDString(std::string& a_sSource, char a_cSep) { // string list std::vector listStr; std::stringstream strSource(a_sSource); std::string str; // seperate a string from the source string while (getline(strSource, str, a_cSep)) { // get the string ok, add the string into the string list listStr.push_back(str); } // return string list return listStr; } // const CString& a_sSource // LPCTSTR a_sSep -- separator std::vector COTSHelper::SplitString(const CString& a_strSource, LPCTSTR a_strSep) { // string list std::vector listString; // source string CString strSource = a_strSource; // find the first separator int nPosLast = 0; auto nPos = strSource.Find(a_strSep, nPosLast); // found the separator? while (nPos >= nPosLast) { // there is no string between two seperator if nPos == nPosLast if (nPos == nPosLast) { listString.push_back(_T("")); nPosLast++; } else { // get the string between two separator CString strValue = strSource.Mid(nPosLast, nPos - nPosLast); strValue.Trim(); // add the string into the string list listString.push_back(strValue); nPosLast = nPos + 1; } // try to find the next separator nPos = strSource.Find(a_strSep, nPosLast); } // push the last one into the string list CString strLastValue = strSource.Right(strSource.GetLength() - nPosLast); strLastValue.Trim(); listString.push_back(strLastValue); // return the string list return listString; } // cut of the string to make sure that it will be never over the length void COTSHelper::EnsureStringLengthNoMoreThan(CString& a_str, int a_nMaxLength) { auto len = a_str.GetLength(); if (len > a_nMaxLength) { a_str.Truncate(a_nMaxLength); } } // trim std string void COTSHelper::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; } } // get file name list in a folder BOOL COTSHelper::GetFileNameList(CString a_strFolderName, CString a_strFileType,std::vector& a_listFileName) { // get file name a_strFolderName += "*"; a_strFolderName += a_strFileType; try { //find first file WIN32_FIND_DATA FindFileData; HANDLE file = FindFirstFile(a_strFolderName.GetBuffer(), &FindFileData); //find other file if (file != INVALID_HANDLE_VALUE) { a_listFileName.push_back(FindFileData.cFileName); BOOL bState = FALSE; bState = FindNextFile(file, &FindFileData); while (bState) { a_listFileName.push_back(FindFileData.cFileName); bState = FindNextFile(file, &FindFileData); } } } catch (CFileException* pe) { pe->Delete(); return FALSE; } return TRUE; } std::string COTSHelper::GetSystemTime() { SYSTEMTIME m_time; GetLocalTime(&m_time); char szDateTime[100] = { 0 }; sprintf(szDateTime, "%02d-%02d-%02d %02d:%02d:%02d", m_time.wYear, m_time.wMonth, m_time.wDay, m_time.wHour, m_time.wMinute, m_time.wSecond); std::string time(szDateTime); return time; } }