Util.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. vector<string> splitEx(const string& src, string separate_character);
  40. int GB2312_2_UTF8(char* buf, int buf_len, const char* src, int src_len);
  41. void Gb2312_2_Unicode(unsigned short* dst, const char* src);
  42. void Unicode_2_UTF8(char* dst, unsigned short* src);
  43. string ConvertUTF8toGB2312(const char *pData, size_t size);
  44. wstring UTF8ToUnicode(const char* szU8);
  45. string UnicodeToUTF8(const wchar_t* unicode);
  46. CString GetExePath();
  47. int CalcWord(const char *ps, const size_t length);
  48. void Int64DateToStr(const __int64& llDate, string& out);
  49. void Int64DateToYMDStr(const __int64& llDate, string& out);
  50. void Int64DateToHMStr(const __int64& llDate, string& out);
  51. unsigned char ToHex(unsigned char x);
  52. unsigned char FromHex(unsigned char x);
  53. string UrlEncode(const std::string& str);
  54. string UrlDecode(const std::string& str);
  55. void RecursiveDirectory(CString cstrDir); // 递归创建目录
  56. void toUpper(char *szDestination, const char *szSource);
  57. CString GetExcelDriverInfo();
  58. __int64 StringToInt64(const char *szSourse);
  59. string GetRequestByKey(const string& strKey,const string& strReuest); // 解析URL中的参数
  60. void GetKeysByUrl(const string & strUrl, vector<string> & vctKey);
  61. __int64 GetCharNumber(char* szText, size_t n); // 获取字符串所有的数据
  62. bool RegexStdMatch(string src, string regular); // std 正则表达式校验
  63. bool RegexStdMatch(wstring src, wstring regular);
  64. string replace_all(string& str, const string& old_value, const string& new_value);
  65. template<typename T>
  66. string tostring(T t, int bit = 4) //括号内可以是字符串(ascii)、整数、浮点数、bool等
  67. {
  68. std::string result;
  69. stringstream ss;
  70. ss << fixed << setprecision(bit) << t;
  71. ss >> result;
  72. CStringA strTemp = result.c_str();
  73. int nIndex = strTemp.Find(".");
  74. if (nIndex != -1)
  75. {
  76. strTemp.TrimRight("0");
  77. if (nIndex == strTemp.GetLength() - 1)
  78. {
  79. strTemp.TrimRight(".");
  80. }
  81. }
  82. if (strTemp == "-0")
  83. strTemp = "0";
  84. return string(strTemp);
  85. }
  86. CStringW CStrA2W(const CStringA &cstrSrcA);
  87. CStringA CStrW2A(const CStringW &cstrSrcW);
  88. string strW2A(const wstring &wstr);
  89. wstring strA2W(const string &wstr);
  90. string strWtoUTF8(const wstring &src) ;
  91. wstring strUTF8toW(const string& src) ;
  92. void sync_system_time(ULONGLONG time);