123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #pragma once
- #include "GdiplusNew.h"
- #include <functional>
- 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<void()> onExitScope)
- : onExitScope_(onExitScope), dismissed_(false)
- { }
- ~ScopeGuard()
- {
- if (!dismissed_)
- {
- onExitScope_();
- }
- }
- void Dismiss()
- {
- dismissed_ = true;
- }
- private:
- std::function<void()> 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<CString> & 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<std::string> LoadTextFileToSTDList(CString a_strPathName, int a_nLine = -1);
- static std::vector<CString> LoadTextFileToCStingList(CString a_strPathName, int a_nLine = -1);
- // split string
- static std::vector<std::string> SplitSTDString(std::string& a_sSource, char a_cTok);
- static std::vector<CString> 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<CString>& a_listFileName);
- std::string GetSystemTime();
- };
-
- }
|