OTSHelper.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include "GdiplusNew.h"
  3. #include <functional>
  4. using namespace Gdiplus;
  5. // COTSHelper command target
  6. namespace OTSTools {
  7. #ifdef UNICODE
  8. #define isdigit_t iswdigit
  9. #else
  10. #define isdigit_t isdigit
  11. #endif
  12. class ScopeGuard
  13. {
  14. public:
  15. explicit ScopeGuard(std::function<void()> onExitScope)
  16. : onExitScope_(onExitScope), dismissed_(false)
  17. { }
  18. ~ScopeGuard()
  19. {
  20. if (!dismissed_)
  21. {
  22. onExitScope_();
  23. }
  24. }
  25. void Dismiss()
  26. {
  27. dismissed_ = true;
  28. }
  29. private:
  30. std::function<void()> onExitScope_;
  31. bool dismissed_;
  32. private: // noncopyable
  33. ScopeGuard(ScopeGuard const&);
  34. ScopeGuard& operator=(ScopeGuard const&);
  35. };
  36. #define SCOPEGUARD_LINENAME_CAT(name, line) name##line
  37. #define SCOPEGUARD_LINENAME(name, line) SCOPEGUARD_LINENAME_CAT(name, line)
  38. #define ON_SCOPE_EXIT(callback) ScopeGuard SCOPEGUARD_LINENAME(EXIT, __LINE__)(callback)
  39. class __declspec(dllexport) COTSHelper
  40. {
  41. protected:
  42. COTSHelper();
  43. ~COTSHelper();
  44. public:
  45. // gets the name of the folder
  46. static CString GetFolderName(CString a_strPathName);
  47. // get file name without extension
  48. static CString GetFileNameWithoutExtension(CString a_strPathName);
  49. // Gets the name of the file
  50. static CString GetFileName(LPCTSTR a_strPathName);
  51. // get file extension
  52. static CString GetFileExtension(LPCTSTR a_strPathName);
  53. // get system error string
  54. static LPCTSTR GetSystemErrorString(DWORD a_nErr);
  55. // Strings to int.
  56. static BOOL StringToInt(LPCTSTR a_sValue, int& a_nValue);
  57. // Strings to double
  58. static BOOL StringToDouble(LPCTSTR a_sValue, double& a_nValue);
  59. // is digit string
  60. static BOOL IsDigitString(LPCTSTR a_sValue);
  61. // is double string
  62. static BOOL IsDoubleString(LPCTSTR a_sValue);
  63. // is CTRL key pressed
  64. static BOOL IsCtrlKeyPressed();
  65. // is shift key pressed
  66. static BOOL IsShiftKeyPressed();
  67. // Saves the bitmap to file.
  68. static BOOL SaveBitmapToFile(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileName);
  69. // Saves the bitmap to file.
  70. static BOOL SaveBitmapToStream(Gdiplus::Bitmap* a_pBitmap, LPCTSTR a_pFileType, IStream *a_stream);
  71. // Get encoder CLSID
  72. // following code from http://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx
  73. static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
  74. // Finds the index of the string list.
  75. static int FindStringListIndex(CStringList& a_listStr, LPCTSTR a_sItem, BOOL a_bNoCase = TRUE);
  76. static int FindStringListIndex(std::vector<CString> & a_listStr, LPCTSTR a_sItem, BOOL a_bNoCase = TRUE);
  77. // open file
  78. static BOOL ShellOpenFile(LPCTSTR a_strPathName);
  79. static BOOL ShellExecuteCommand(LPCTSTR a_strCommandFile, LPCTSTR a_sCommandParam = NULL);
  80. // get machine id
  81. static CString GetMachineId();
  82. // send email
  83. static BOOL SendEmail(LPCTSTR a_strEmailAddress, LPCTSTR a_sTitle, LPCTSTR a_sBody);
  84. // string conversions
  85. static int CharToWChar(const char* a_psSource, wchar_t* a_psTarget);
  86. static int WCharToChar(const wchar_t* a_psSource, char* a_psTarget);
  87. static CString CharToString(const char* a_psSource);
  88. static DWORD ConvStreamToByteArr(IStream *a_stream, BYTE **a_byte);
  89. // time strings
  90. static CString GetDateTimeString(const COleDateTime& a_DateTime);
  91. static CString GetTimeSpanString(const COleDateTimeSpan& a_TimeSpan, BOOL a_bFixLenth = FALSE);
  92. static COleDateTime GetDateTimeFromString(const CString& a_DateTime);
  93. static COleDateTimeSpan GetTimeSpanFromString(const CString& a_TimeSpan, BOOL a_bFixLenth = FALSE);
  94. // waiting
  95. static void Waiting(long a_nMilliseconds);
  96. // version string
  97. static DWORD GetVersionFromString(LPCTSTR a_sVersion);
  98. static BOOL GetMajorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion);
  99. static BOOL GetMinorVersionFromString(LPCTSTR a_sVersion, int& a_nVersion);
  100. static BOOL GetBuildVersionFromString(LPCTSTR a_sVersion, int& a_nBuild);
  101. // get file modify string
  102. static CString GetFileWriteTime(LPCTSTR a_strFilePath);
  103. // load text file
  104. static std::vector<std::string> LoadTextFileToSTDList(CString a_strPathName, int a_nLine = -1);
  105. static std::vector<CString> LoadTextFileToCStingList(CString a_strPathName, int a_nLine = -1);
  106. // split string
  107. static std::vector<std::string> SplitSTDString(std::string& a_sSource, char a_cTok);
  108. static std::vector<CString> SplitString(const CString& a_sSource, LPCTSTR a_sTok);
  109. // cut of the string to make sure that it will be never over the length
  110. static void EnsureStringLengthNoMoreThan(CString& a_str, int a_nMaxLength);
  111. // trim std string
  112. static void TrimSTDString(std::string& a_strString, char a_cTrimChar = ' ');
  113. // get file name list in a folder
  114. static BOOL GetFileNameList(CString a_strFolderName, CString a_strFileType,std::vector<CString>& a_listFileName);
  115. std::string GetSystemTime();
  116. };
  117. }