12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- // OTSHelper.cpp : implementation file
- //
- #pragma once
- #include "stdafx.h"
- #include "OTSHelper.h"
- //#include "OTSModel.h"
- #include "HardDriveSerialNumber.h"
- #include "COTSUtilityDllFunExport.h"
- #include <strsafe.h>
- #include <sstream>
- //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<CString>& 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<int>(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<int>(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 <CString > tString = SplitString(a_TimeSpan, " ");
- days = std::stoi( tString[0].GetBuffer());
- std::vector<CString> 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<CString> 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 <Major version>.<Minor version>.<Build version>, 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<std::string> COTSHelper::LoadTextFileToSTDList(CString a_strPathName, int a_nLine/*= -1*/)
- {
- // string list
- std::vector<std::string> 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<CString> COTSHelper::LoadTextFileToCStingList(CString a_strPathName, int a_nLine /*= -1*/)
- {
- // string list
- std::vector<CString> 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<std::string> COTSHelper::SplitSTDString(std::string& a_sSource, char a_cSep)
- {
- // string list
- std::vector<std::string> 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<CString> COTSHelper::SplitString(const CString& a_strSource, LPCTSTR a_strSep)
- {
- // string list
- std::vector<CString> 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<CString>& 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;
- }
-
- }
|