Util.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <iomanip>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include "Log.h"
  7. using namespace std;
  8. template<class F>
  9. struct Final_action
  10. {
  11. F clean;
  12. Final_action(F f) :clean(f)
  13. {
  14. }
  15. ~Final_action()
  16. {
  17. clean();
  18. }
  19. };
  20. template<class F>
  21. Final_action<F> finally(F f)
  22. {
  23. return Final_action<F>(f);
  24. }
  25. //int * p = new int[7];
  26. //auto act1 = finally([&]
  27. //{
  28. // delete[]p;
  29. // p = NULL;
  30. //}
  31. //);
  32. bool IsLeap(int year);
  33. bool IsWeekEnd(int year, int month, int day);
  34. char* PBQ_TrimLeftA(char *pTarget);
  35. char* PBQ_TrimRightA(char *pTarget);
  36. char* PBQ_TrimLeftMA(char *pTarget, char* pChars);
  37. char* PBQ_TrimRightMA(char *pTarget, char* pChars);
  38. void Full2Half(wchar_t* str);
  39. bool split(const string& src, string delimit,vector<string> &v, string null_subst="");
  40. bool split(const wstring& src, wstring delimit, vector<wstring> &v, wstring null_subst=L"");
  41. vector<string> splitEx(const string& src, string separate_character);
  42. int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len);
  43. void Gb2312_2_Unicode(unsigned short* dst, const char* src);
  44. void Unicode_2_UTF8(char* dst, unsigned short* src);
  45. string ConvertUTF8toGB2312(const char *pData, size_t size);
  46. char* ConvertGB2312toUTF8(const char *pData);
  47. wstring UTF8ToUnicode(const char* szU8);
  48. inline bool IsChinese(unsigned char c) { return ((c>=0xa1)&&(c<=0xfe)); }
  49. int CalcWord(const char *ps, const size_t length);
  50. void Int64DateToStr(const __int64& llDate, string& out);
  51. void Int64DateToYMDStr(const __int64& llDate, string& out);
  52. void Int64DateToHMStr(const __int64& llDate, string& out);
  53. unsigned char ToHex(unsigned char x);
  54. unsigned char FromHex(unsigned char x);
  55. string UrlEncode(const std::string& str);
  56. string UrlDecode(const std::string& str);
  57. void DeleteDirectory(const CString strPath);
  58. void RecursiveDirectory(CString cstrDir); // 递归创建目录
  59. void toUpper(char *szDestination, const char *szSource);
  60. CString GetExcelDriverInfo();
  61. __int64 StringToInt64(const char *szSourse);
  62. string GetRequestByKey(const string& strKey,const string& strReuest); // 解析URL中的参数
  63. void GetKeysByUrl(const string & strUrl, vector<string> & vctKey);
  64. __int64 GetCharNumber(char* szText, size_t n); // 获取字符串所有的数据
  65. bool RegexStdMatch(string src, string regular); // std 正则表达式校验
  66. bool RegexStdMatch(wstring src, wstring regular);
  67. string replace_all(string& str, const string& old_value, const string& new_value);
  68. template<typename T>
  69. string tostring(T t, int bit = 4) //括号内可以是字符串(ascii)、整数、浮点数、bool等
  70. {
  71. std::string result;
  72. stringstream ss;
  73. ss << fixed << setprecision(bit) << t;
  74. ss >> result;
  75. CStringA strTemp = result.c_str();
  76. int nIndex = strTemp.Find(".");
  77. if (nIndex != -1)
  78. {
  79. strTemp.TrimRight("0");
  80. if (nIndex == strTemp.GetLength() - 1)
  81. {
  82. strTemp.TrimRight(".");
  83. }
  84. }
  85. if (strTemp == "-0")
  86. strTemp = "0";
  87. return string(strTemp);
  88. }
  89. CStringW CStrA2W(const CStringA &cstrSrcA);
  90. CStringA CStrW2A(const CStringW &cstrSrcW);
  91. string strW2A(const wstring &wstr);
  92. wstring strA2W(const string &wstr);
  93. string strWtoUTF8(const wstring &src) ;
  94. wstring strUTF8toW(const string& src) ;
  95. void sync_system_time(ULONGLONG time);