strUtil.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include <Windows.h>
  7. #pragma warning(disable:4996)
  8. namespace StrUtil
  9. {
  10. using namespace std;
  11. inline std::wstring AnsiToUnicode(const char* buf);
  12. inline std::string AnsiToUtf8(const char* buf);
  13. inline std::string UnicodeToAnsi(const wchar_t* buf);
  14. inline std::wstring Utf8ToUnicode(const char* buf);
  15. inline std::string UnicodeToUtf8(const wchar_t* buf);
  16. inline std::string TrimLeft(const std::string& str);
  17. inline std::string TrimRight(const std::string& str);
  18. inline std::string Trim(const std::string& str);
  19. inline std::vector<std::string> Split(std::string& str, const char* c);
  20. inline std::string GetModulePath();
  21. inline void Replace(std::string& srcstr, const std::string& oldstr, const std::string& newstr);
  22. inline HMODULE GetCurrentModule();
  23. };
  24. namespace StrUtil {
  25. void Replace(std::string& srcstr, const std::string& oldstr, const std::string& newstr)
  26. {
  27. string::size_type pos = 0;
  28. string::size_type a = oldstr.size();
  29. string::size_type b = newstr.size();
  30. while ((pos = srcstr.find(oldstr, pos)) != string::npos)
  31. {
  32. srcstr.replace(pos, a, newstr);
  33. pos += b;
  34. }
  35. }
  36. std::vector<std::string> Split(std::string& str, const char* c)
  37. {
  38. char* cstr, * p;
  39. std::vector<std::string> res;
  40. cstr = new char[str.size() + 1];
  41. strcpy(cstr, str.c_str());
  42. p = strtok(cstr, c);
  43. while (p != NULL)
  44. {
  45. res.push_back(p);
  46. p = strtok(NULL, c);
  47. }
  48. return res;
  49. }
  50. std::wstring AnsiToUnicode(const char* buf)
  51. {
  52. int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
  53. if (len == 0) return L"";
  54. std::vector<wchar_t> unicode(len);
  55. ::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
  56. return &unicode[0];
  57. }
  58. std::string AnsiToUtf8(const char* buf)
  59. {
  60. return UnicodeToUtf8(AnsiToUnicode(buf).c_str());
  61. }
  62. std::string UnicodeToAnsi(const wchar_t* buf)
  63. {
  64. int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
  65. if (len == 0) return "";
  66. std::vector<char> utf8(len);
  67. ::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);
  68. return &utf8[0];
  69. }
  70. std::wstring Utf8ToUnicode(const char* buf)
  71. {
  72. int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
  73. if (len == 0) return L"";
  74. std::vector<wchar_t> unicode(len);
  75. ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);
  76. return &unicode[0];
  77. }
  78. std::string UnicodeToUtf8(const wchar_t* buf)
  79. {
  80. int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
  81. if (len == 0) return "";
  82. std::vector<char> utf8(len);
  83. ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
  84. return &utf8[0];
  85. }
  86. std::string TrimLeft(const std::string& str) {
  87. std::string t = str;
  88. for (std::string::iterator i = t.begin(); i != t.end(); i++) {
  89. if (!isspace(*i)) {
  90. t.erase(t.begin(), i);
  91. break;
  92. }
  93. }
  94. return t;
  95. }
  96. std::string TrimRight(const std::string& str) {
  97. if (str.begin() == str.end()) {
  98. return str;
  99. }
  100. std::string t = str;
  101. for (std::string::iterator i = t.end() - 1; i != t.begin(); i--) {
  102. if (!isspace(*i)) {
  103. t.erase(i + 1, t.end());
  104. break;
  105. }
  106. }
  107. return t;
  108. }
  109. std::string Trim(const std::string& str) {
  110. std::string t = str;
  111. std::string::iterator i;
  112. for (i = t.begin(); i != t.end(); i++) {
  113. if (!isspace(*i)) {
  114. t.erase(t.begin(), i);
  115. break;
  116. }
  117. }
  118. if (i == t.end()) {
  119. return t;
  120. }
  121. for (i = t.end() - 1; i != t.begin(); i--) {
  122. if (!isspace(*i)) {
  123. t.erase(i + 1, t.end());
  124. break;
  125. }
  126. }
  127. return t;
  128. }
  129. HMODULE GetCurrentModule()
  130. { // NB: XP+ solution!
  131. HMODULE hModule = NULL;
  132. GetModuleHandleEx(
  133. GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  134. (LPCTSTR)GetCurrentModule,
  135. &hModule);
  136. return hModule;
  137. }
  138. std::string GetModulePath()
  139. {
  140. char fullPath[MAX_PATH];
  141. char drive[_MAX_DRIVE];
  142. char dir[_MAX_DIR];
  143. HMODULE hModule = GetCurrentModule();
  144. if (0 == GetModuleFileNameA(hModule, fullPath, MAX_PATH))
  145. return "";
  146. _splitpath(fullPath, drive, dir, NULL, NULL);
  147. std::string str = std::string(drive) + dir;
  148. return str;
  149. }
  150. }