helpfunc.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //#include "stdafx.h"
  2. #include <tchar.h>
  3. #include "helpfunc.h"
  4. #include <algorithm>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <shlwapi.h>
  9. #include "globalVar.h"
  10. #include "opEnv.h"
  11. //#define USE_BOOST_STACK_TRACE
  12. #ifdef USE_BOOST_STACK_TRACE
  13. #include <boost/stacktrace.hpp>
  14. #endif
  15. std::wstring _s2wstring(const std::string& s) {
  16. size_t nlen = s.length();
  17. wchar_t* m_char;
  18. int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), nlen, NULL, 0);
  19. m_char = new wchar_t[len + 1];
  20. MultiByteToWideChar(CP_ACP, 0, s.data(), nlen, m_char, len);
  21. m_char[len] = '\0';
  22. std::wstring ws(m_char);
  23. delete[] m_char;
  24. return ws;
  25. }
  26. std::string _ws2string(const std::wstring& ws) {
  27. // std::string strLocale = setlocale(LC_ALL, "");
  28. // const wchar_t* wchSrc = ws.c_str();
  29. // size_t nDestSize = wcstombs(NULL, wchSrc, 0) + 1;
  30. // char *chDest = new char[nDestSize];
  31. // memset(chDest, 0, nDestSize);
  32. // wcstombs(chDest, wchSrc, nDestSize);
  33. // std::string strResult = chDest;
  34. // delete[]chDest;
  35. // setlocale(LC_ALL, strLocale.c_str());
  36. //return strResult;
  37. int nlen = ws.length();
  38. char* m_char;
  39. int len = WideCharToMultiByte(CP_ACP, 0, ws.data(), nlen, NULL, 0, NULL, NULL);
  40. m_char = new char[len + 1];
  41. WideCharToMultiByte(CP_ACP, 0, ws.data(), nlen, m_char, len, NULL, NULL);
  42. m_char[len] = '\0';
  43. std::string s(m_char);
  44. delete[] m_char;
  45. return s;
  46. }
  47. long Path2GlobalPath(const TString& file, const TString& curr_path, TString& out) {
  48. if (::PathFileExists(file.c_str())) {
  49. out = file;
  50. return 1;
  51. }
  52. out.clear();
  53. out = curr_path + _T("\\") + file;
  54. if (::PathFileExists(out.c_str())) {
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. long setlog(const wchar_t* format, ...) {
  60. va_list args;
  61. wchar_t buf[512];
  62. va_start(args, format);
  63. vswprintf(buf, format, args);
  64. va_end(args);
  65. std::wstring tmpw = buf;
  66. std::string tmps = _ws2string(tmpw);
  67. return setlog(tmps.data());
  68. }
  69. long setlog(const char* format, ...) {
  70. std::stringstream ss(std::wstringstream::in | std::wstringstream::out);
  71. va_list args;
  72. char buf[512];
  73. SYSTEMTIME sys;
  74. GetLocalTime(&sys);
  75. char tm[128];
  76. sprintf(tm, "[%4d/%02d/%02d %02d:%02d:%02d.%03d]",
  77. sys.wYear, sys.wMonth, sys.wDay,
  78. sys.wHour, sys.wMinute, sys.wSecond,
  79. sys.wMilliseconds);
  80. va_start(args, format);
  81. vsprintf(buf, format, args);
  82. va_end(args);
  83. ss << tm << (OP64 == 1 ? "x64" : "x32") << "info: " << buf << std::endl;
  84. #ifdef USE_BOOST_STACK_TRACE
  85. ss << "<stack>\n"
  86. << boost::stacktrace::stacktrace() << std::endl;
  87. #endif // USE_BOOST_STACK_TRACE
  88. std::string s = ss.str();
  89. if (opEnv::m_showErrorMsg == 1) {
  90. MessageBoxA(NULL, s.data(), "error", MB_ICONERROR);
  91. }
  92. else if (opEnv::m_showErrorMsg == 2) {
  93. /* wchar_t dll_path[MAX_PATH];
  94. ::GetModuleFileNameW(gInstance, dll_path, MAX_PATH);
  95. wstring fname = dll_path;
  96. fname = fname.substr(0, fname.rfind(L'\\'));
  97. fname += L"\\op.log";*/
  98. std::fstream file;
  99. file.open("__op.log", std::ios::app | std::ios::out);
  100. if (!file.is_open())
  101. return 0;
  102. file << s << std::endl;
  103. file.close();
  104. }
  105. else if (opEnv::m_showErrorMsg == 3) {
  106. std::cout << s << std::endl;
  107. }
  108. return 1;
  109. }
  110. void split(const TString& s, std::vector<TString>& v, const TString& c)
  111. {
  112. std::wstring::size_type pos1, pos2;
  113. size_t len = s.length();
  114. pos2 = s.find(c);
  115. pos1 = 0;
  116. v.clear();
  117. while (std::wstring::npos != pos2)
  118. {
  119. v.emplace_back(s.substr(pos1, pos2 - pos1));
  120. pos1 = pos2 + c.size();
  121. pos2 = s.find(c, pos1);
  122. }
  123. if (pos1 != len)
  124. v.emplace_back(s.substr(pos1));
  125. }
  126. void wstring2upper(std::wstring& s) {
  127. std::transform(s.begin(), s.end(), s.begin(), towupper);
  128. }
  129. void string2upper(std::string& s) {
  130. std::transform(s.begin(), s.end(), s.begin(), toupper);
  131. }
  132. void wstring2lower(std::wstring& s) {
  133. std::transform(s.begin(), s.end(), s.begin(), towlower);
  134. }
  135. void string2lower(std::string& s) {
  136. std::transform(s.begin(), s.end(), s.begin(), tolower);
  137. }
  138. void replacea(TString& str, const TString& oldval, const TString& newval) {
  139. size_t x0 = 0, dx = newval.length() - oldval.length() + 1;
  140. size_t idx = str.find(oldval, x0);
  141. while (idx != -1 && x0 >= 0) {
  142. str.replace(idx, oldval.length(), newval);
  143. x0 = idx + dx;
  144. idx = str.find(oldval, x0);
  145. }
  146. }
  147. std::ostream& operator<<(std::ostream& o, point_t const& rhs) {
  148. o << rhs.x << "," << rhs.y;
  149. return o;
  150. }
  151. std::wostream& operator<<(std::wostream& o, point_t const& rhs) {
  152. o << rhs.x << L"," << rhs.y;
  153. return o;
  154. }
  155. std::ostream& operator<<(std::ostream& o, FrameInfo const& rhs) {
  156. o << "hwnd:" << rhs.hwnd << std::endl
  157. << "frameId:" << rhs.frameId << std::endl
  158. << "time:" << rhs.time << std::endl
  159. << "height" << rhs.height << std::endl
  160. << "width:" << rhs.width << std::endl;
  161. return o;
  162. }
  163. std::wostream& operator<<(std::wostream& o, FrameInfo const& rhs) {
  164. o << L"hwnd:" << rhs.hwnd << std::endl
  165. << L"frameId:" << rhs.frameId << std::endl
  166. << L"time:" << rhs.time << std::endl
  167. << L"height" << rhs.height << std::endl
  168. << L"width:" << rhs.width << std::endl;
  169. return o;
  170. }