#pragma once #include "GdiplusNew.h" #include using namespace Gdiplus; // COTSHelper command target namespace OTSTools { #ifdef UNICODE #define isdigit_t iswdigit #else #define isdigit_t isdigit #endif class ScopeGuard { public: explicit ScopeGuard(std::function onExitScope) : onExitScope_(onExitScope), dismissed_(false) { } ~ScopeGuard() { if (!dismissed_) { onExitScope_(); } } void Dismiss() { dismissed_ = true; } private: std::function onExitScope_; bool dismissed_; private: // noncopyable ScopeGuard(ScopeGuard const&); ScopeGuard& operator=(ScopeGuard const&); }; #define SCOPEGUARD_LINENAME_CAT(name, line) name##line #define SCOPEGUARD_LINENAME(name, line) SCOPEGUARD_LINENAME_CAT(name, line) #define ON_SCOPE_EXIT(callback) ScopeGuard SCOPEGUARD_LINENAME(EXIT, __LINE__)(callback) class __declspec(dllexport) COTSHelper { protected: COTSHelper(); ~COTSHelper(); public: // gets the name of the folder static CString GetFolderName(CString a_strPathName); // get file name without extension static CString GetFileNameWithoutExtension(CString a_strPathName); // Gets the name of the file static CString GetFileName(LPCTSTR a_strPathName); // get file extension static CString GetFileExtension(LPCTSTR a_strPathName); // get system error string static LPCTSTR GetSystemErrorString(DWORD a_nErr); // Strings to int. static BOOL StringToInt(LPCTSTR a_sValue, int& a_nValue); // Strings to double static BOOL StringToDouble(LPCTSTR a_sValue, double& a_nValue); // is digit string static BOOL IsDigitString(LPCTSTR a_sValue); // is double string static BOOL IsDoubleString(LPCTSTR a_sValue); // is CTRL key pressed static BOOL IsCtrlKeyPressed(); // is shift key pressed static BOOL IsShiftKeyPressed(); // Saves the bitmap to file. static BOOL SaveBitmapToFile(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileName); // Saves the bitmap to file. static BOOL SaveBitmapToStream(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileType, IStream *a_stream); // Get encoder CLSID // following code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid); // Finds the index of the string list. static int FindStringListIndex(CStringList& a_listStr, LPCTSTR a_sItem, BOOL a_bNoCase = TRUE); static int FindStringListIndex(std::vector & a_listStr, LPCTSTR a_sItem, BOOL a_bNoCase = TRUE); // open file static BOOL ShellOpenFile(LPCTSTR a_strPathName); static BOOL ShellExecuteCommand(LPCTSTR a_strCommandFile, LPCTSTR a_sCommandParam = NULL); // get machine id static CString GetMachineId(); // send email static BOOL SendEmail(LPCTSTR a_strEmailAddress, LPCTSTR a_sTitle, LPCTSTR a_sBody); // string conversions static int CharToWChar(const char* a_psSource, wchar_t* a_psTarget); static int WCharToChar(const wchar_t* a_psSource, char* a_psTarget); static CString CharToString(const char* a_psSource); static DWORD ConvStreamToByteArr(IStream *a_stream, BYTE **a_byte); // time strings static CString GetDateTimeString(const COleDateTime& a_DateTime); static CString GetTimeSpanString(const COleDateTimeSpan& a_TimeSpan, BOOL a_bFixLenth = FALSE); static COleDateTime GetDateTimeFromString(const CString& a_DateTime); static COleDateTimeSpan GetTimeSpanFromString(const CString& a_TimeSpan, BOOL a_bFixLenth = FALSE); // waiting static void Waiting(long a_nMilliseconds); // version string static DWORD GetVersionFromString(LPCTSTR a_sVersion); static BOOL GetMajorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion); static BOOL GetMinorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion); static BOOL GetBuildVersionFromString(LPCTSTR a_sVersion, int& a_nBuild); // get file modify string static CString GetFileWriteTime(LPCTSTR a_strFilePath); // load text file static std::vector LoadTextFileToSTDList(CString a_strPathName, int a_nLine = -1); static std::vector LoadTextFileToCStingList(CString a_strPathName, int a_nLine = -1); // split string static std::vector SplitSTDString(std::string& a_sSource, char a_cTok); static std::vector SplitString(const CString& a_sSource, LPCTSTR a_sTok); // cut of the string to make sure that it will be never over the length static void EnsureStringLengthNoMoreThan(CString& a_str, int a_nMaxLength); // trim std string static void TrimSTDString(std::string& a_strString, char a_cTrimChar = ' '); // get file name list in a folder static BOOL GetFileNameList(CString a_strFolderName, CString a_strFileType,std::vector& a_listFileName); std::string GetSystemTime(); }; }