BaseUtility.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(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 toUpper(char *szDestination, const char *szSource);
  80. void toUpper(TCHAR *szDestination, const TCHAR *szSource);
  81. void FormatPrice(char *szDes, const double dSource); // 格式化价格 小数部分至少两位
  82. bool CombinationVolRemark(string &strDes, const char *szVol, const char *szRemark);
  83. bool CombinationQuoteData(string &strDes, const char *szVol, const char *szRemark = NULL
  84. , const char *szDanger = NULL, const char *szOCO = NULL, const char *szPACK = NULL);
  85. string replace(const char *szContext, char oldChar, const char *szNew);
  86. __int64 StringToInt64(const TCHAR *szSourse);
  87. void SafeCopyData(TCHAR dest[], int destSize, TCHAR* src);
  88. void SafeCopyData(TCHAR dest[], int destSize, tstring str);
  89. tstring GetRequestByKey(const tstring& strKey,const tstring& strReuest); // 解析URL中的参数
  90. void GetKeysByUrl(const tstring & strUrl, vector<tstring> & vctKey);
  91. __int64 GetCharNumber(TCHAR* szText, size_t n); // 获取字符串所有的数据
  92. CString AddNumberSeparate(CString strSrc);
  93. bool RegexStdMatch(string src, string regular); // std 正则表达式校验
  94. bool RegexStdMatch(tstring src, tstring regular);
  95. tstring replace_all(tstring& str, const tstring& old_value, const tstring& new_value);
  96. template<typename T>
  97. tstring tostring(T t, int bit = 4)
  98. {
  99. tstring result;
  100. wstringstream ss;
  101. ss << fixed << setprecision(bit) << t;
  102. ss >> result;
  103. CString strTemp = result.c_str();
  104. int nIndex = strTemp.Find(_T("."));
  105. if (nIndex != -1)
  106. {
  107. strTemp.TrimRight(_T("0"));
  108. if (nIndex == strTemp.GetLength() - 1)
  109. {
  110. strTemp.TrimRight(_T("."));
  111. }
  112. }
  113. if (strTemp == _T("-0"))
  114. strTemp = _T("0");
  115. return tstring(strTemp);
  116. }
  117. tstring ANSIToUnicode(const string& str);
  118. CString FormatNumberString(CString strData);
  119. #define CLOSE_IMAGE(pImage) if (pImage)\
  120. {\
  121. delete pImage;\
  122. pImage = NULL;\
  123. }\
  124. #define CLOSE_ICON(hIcon) if (hIcon)\
  125. {\
  126. ::DestroyIcon(hIcon);\
  127. hIcon = NULL; \
  128. }\
  129. //格式化手机号码
  130. #define FormatPhoneNumber(strPhone)\
  131. if (strPhone.GetLength() > 5)\
  132. {\
  133. strPhone = strPhone.Mid(0, 3) + "******" + strPhone.Mid(strPhone.GetLength() - 2, 2);\
  134. }
  135. string FormatInt32Value2(int nValue);
  136. tstring FormatInt32Value(int nValue);
  137. tstring FormatInt64Value(__int64 nValue);
  138. tstring FormatFloatValue(double fValue);
  139. string FormatFloatValue2(double fValue);
  140. CString GetAppPath();