BaseUtility.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include <iomanip>
  3. #include <sstream>
  4. #include <vector>
  5. using namespace std;
  6. #ifndef _UNICODE
  7. #define tstring std::string
  8. #else
  9. #define tstring std::wstring
  10. #endif
  11. #ifndef _UNICODE
  12. #define tstringstream std::stringstream
  13. #else
  14. #define tstringstream std::wstringstream
  15. #endif
  16. template<class T = char>
  17. class autoptr
  18. {
  19. public:
  20. autoptr(T *buf) : m_buf(buf) {}
  21. ~autoptr() {
  22. if (m_buf)
  23. {
  24. delete m_buf;
  25. m_buf = NULL;
  26. }
  27. }
  28. private:
  29. T *m_buf;
  30. };
  31. template<class T = char>
  32. class autoptr_arr
  33. {
  34. public:
  35. autoptr_arr(T *buf) : m_buf(buf) {}
  36. ~autoptr_arr() {
  37. delete[]m_buf;
  38. }
  39. private:
  40. T *m_buf;
  41. };
  42. bool IsLeap(int year);
  43. bool IsWeekEnd(int year, int month, int day);
  44. bool Nextday(int& year, int& month, int& day);
  45. void Full2Half(wchar_t* str);
  46. bool split(const tstring& src, tstring delimit,vector<tstring> &v, tstring null_subst=_T(""));
  47. bool split(const string& src, string delimit, vector<string> &v, string null_subst = "");
  48. vector<tstring> splitEx(const tstring& src, tstring separate_character);
  49. vector<string> splitEx(const string& src, string separate_character);
  50. int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len);
  51. void Gb2312_2_Unicode(unsigned short* dst, const char* src);
  52. void Unicode_2_UTF8(char* dst, unsigned short* src);
  53. string ConvertUTF8toGB2312(const char *pData, size_t size);
  54. string ConvertGB2312toUTF8(const char *pData);
  55. bool TransToPriceFormat(const tstring& strSrc, tstring& strDes, double& dDes);
  56. wstring GB2312ToUnicode(const char *pData);
  57. wstring UTF8ToUnicode(const char* szU8);
  58. void UTF8toANSI(std::string &strUTF8);
  59. string UnicodeToUtf8(const wchar_t* unicode);
  60. string UnicodeToGB2312(TCHAR* pData);
  61. string UnicodeToGB2312(const tstring& strSrc);
  62. string UnicodeToGB2312(const CString& strSrc);
  63. tstring GB2312ToTstring(string str);
  64. string TstringToGB2312(tstring tstr);
  65. string TstringToUTF8(tstring tstr);
  66. tstring UTF8ToTstring(string str);
  67. inline bool IsChinese(unsigned char c) { return ((c>=0xa1)&&(c<=0xfe)); }
  68. int CalcWord(const char *ps, const size_t length);
  69. void Int64DateToStr(const __int64& llDate, string& out);
  70. void Int64DateToYMDStr(const __int64& llDate, string& out);
  71. void Int64DateToHMStr(const __int64& llDate, string& out);
  72. void GetSubStr(TCHAR *szDestination, const TCHAR *szSource, const int nWidth, HDC hDC);
  73. bool IsNumAndPoint(const CString& strText);
  74. unsigned char ToHex(unsigned char x);
  75. unsigned char FromHex(unsigned char x);
  76. string UrlEncode(const string& str);
  77. string UrlDecode(const string& str);
  78. void DeleteDirectory(const CString &strPath);
  79. void FindDirFile(const CString &strPath, vector<std::tuple<CString, CString>>& vctFilePath);
  80. void toUpper(char *szDestination, const char *szSource);
  81. void toUpper(TCHAR *szDestination, const TCHAR *szSource);
  82. void FormatPrice(char *szDes, const double dSource); // 格式化价格 小数部分至少两位
  83. bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark);
  84. bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark = NULL
  85. , const char *szDanger = NULL, const char *szOCO = NULL, const char *szPACK = NULL);
  86. string replace(const char *szContext, char oldChar, const char *szNew);
  87. __int64 StringToInt64(const TCHAR *szSourse);
  88. void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src);
  89. void SafeCopyData(TCHAR dest[], int destSize, tstring str);
  90. tstring GetRequestByKey(const tstring& strKey,const tstring& strReuest); // 解析URL中的参数
  91. void GetKeysByUrl(const tstring & strUrl, vector<tstring> & vctKey);
  92. __int64 GetCharNumber(TCHAR* szText, size_t n); // 获取字符串所有的数据
  93. CString AddNumberSeparate(CString strSrc);
  94. bool RegexStdMatch(string src, string regular); // std 正则表达式校验
  95. bool RegexStdMatch(tstring src, tstring regular);
  96. tstring replace_all(tstring& str, const tstring& old_value, const tstring& new_value);
  97. template<typename T>
  98. tstring tostring(T t, int bit = 4)
  99. {
  100. tstring result;
  101. wstringstream ss;
  102. ss << fixed << setprecision(bit) << t;
  103. ss >> result;
  104. CString strTemp = result.c_str();
  105. int nIndex = strTemp.Find(_T("."));
  106. if (nIndex != -1)
  107. {
  108. strTemp.TrimRight(_T("0"));
  109. if (nIndex == strTemp.GetLength() - 1)
  110. {
  111. strTemp.TrimRight(_T("."));
  112. }
  113. }
  114. if (strTemp == _T("-0"))
  115. strTemp = _T("0");
  116. return tstring(strTemp);
  117. }
  118. tstring ANSIToUnicode(const string& str);
  119. CString FormatNumberString(CString strData);
  120. #define CLOSE_IMAGE(pImage) if (pImage)\
  121. {\
  122. delete pImage;\
  123. pImage = NULL;\
  124. }\
  125. #define CLOSE_ICON(hIcon) if (hIcon)\
  126. {\
  127. ::DestroyIcon(hIcon);\
  128. hIcon = NULL; \
  129. }\
  130. //格式化手机号码
  131. #define FormatPhoneNumber(strPhone)\
  132. if (strPhone.GetLength() > 5)\
  133. {\
  134. strPhone = strPhone.Mid(0, 3) + "******" + strPhone.Mid(strPhone.GetLength() - 2, 2);\
  135. }
  136. string FormatInt32Value2(int nValue);
  137. tstring FormatInt32Value(int nValue);
  138. tstring FormatInt64Value(__int64 nValue);
  139. tstring FormatFloatValue(double fValue);
  140. string FormatFloatValue2(double fValue);
  141. CString GetAppPath();
  142. string GetImageEncodeString(string strPath);
  143. string GenerateGUID();