TString.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "TString.hpp"
  2. #include "ExceptionSystem.hpp"
  3. #undef __BASE_FILE__
  4. #define __BASE_FILE__ TEXT("TString.cpp")
  5. TString TStringBuilder(PCSTR LpMultiByteStr, UINT CodePage) {
  6. #ifdef _UNICODE
  7. TString TStr;
  8. int RequiredSize = MultiByteToWideChar(CodePage,
  9. NULL,
  10. LpMultiByteStr,
  11. -1,
  12. NULL,
  13. 0);
  14. if (RequiredSize == 0)
  15. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  16. TEXT("MultiByteToWideChar failed."));
  17. TStr.resize(RequiredSize);
  18. if (!MultiByteToWideChar(CP_ACP,
  19. NULL,
  20. LpMultiByteStr,
  21. -1,
  22. TStr.data(),
  23. RequiredSize))
  24. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  25. TEXT("MultiByteToWideChar failed."));
  26. while (TStr.back() == L'\x00')
  27. TStr.pop_back();
  28. return TStr;
  29. #else
  30. if (CodePage == CP_ACP) {
  31. return TString(LpMultiByteStr);
  32. } else {
  33. std::wstring WideCharStr;
  34. TString TStr;
  35. int RequiredSize = MultiByteToWideChar(CodePage,
  36. NULL,
  37. LpMultiByteStr,
  38. -1,
  39. NULL,
  40. 0);
  41. if (RequiredSize == 0)
  42. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  43. TEXT("MultiByteToWideChar failed."));
  44. WideCharStr.resize(RequiredSize);
  45. if (!MultiByteToWideChar(CP_ACP,
  46. NULL,
  47. LpMultiByteStr,
  48. -1,
  49. WideCharStr.data(),
  50. RequiredSize))
  51. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  52. TEXT("MultiByteToWideChar failed."));
  53. while (WideCharStr.back() == L'\x00')
  54. WideCharStr.pop_back();
  55. RequiredSize = WideCharToMultiByte(CP_ACP,
  56. NULL,
  57. WideCharStr.c_str(),
  58. -1,
  59. NULL,
  60. 0,
  61. NULL,
  62. NULL);
  63. if (RequiredSize == 0)
  64. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  65. TEXT("WideCharToMultiByte failed."));
  66. TStr.resize(RequiredSize);
  67. if (!WideCharToMultiByte(CP_ACP,
  68. NULL,
  69. WideCharStr.c_str(),
  70. -1,
  71. TStr.data(),
  72. RequiredSize,
  73. NULL,
  74. NULL))
  75. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  76. TEXT("WideCharToMultiByte failed."));
  77. while (TStr.back() == '\x00')
  78. TStr.pop_back();
  79. return TStr;
  80. }
  81. #endif
  82. }
  83. TString TStringBuilder(PCWSTR LpWideCharStr) {
  84. #ifdef _UNICODE
  85. return TString(LpWideCharStr);
  86. #else
  87. TString TStr;
  88. int RequiredSize = WideCharToMultiByte(CP_ACP,
  89. NULL,
  90. LpWideCharStr,
  91. -1,
  92. NULL,
  93. 0,
  94. NULL,
  95. NULL);
  96. if (RequiredSize == 0)
  97. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  98. TEXT("WideCharToMultiByte failed."));
  99. TStr.resize(RequiredSize);
  100. if (!WideCharToMultiByte(CP_ACP,
  101. NULL,
  102. LpWideCharStr,
  103. -1,
  104. TStr.data(),
  105. RequiredSize,
  106. NULL,
  107. NULL))
  108. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  109. TEXT("WideCharToMultiByte failed."));
  110. while (TStr.back() == '\x00')
  111. TStr.pop_back();
  112. return TStr;
  113. #endif
  114. }
  115. TString TStringBuilder(const std::string& RefMultiByteStr, UINT CodePage) {
  116. return TStringBuilder(RefMultiByteStr.c_str(), CodePage);
  117. }
  118. TString TStringBuilder(const std::wstring& RefWideCharStr) {
  119. return TStringBuilder(RefWideCharStr.c_str());
  120. }
  121. std::string TStringEncode(PTSTR LpTString, UINT CodePage) {
  122. std::string MultiByteStr;
  123. int RequiredSize;
  124. #ifdef _UNICODE
  125. RequiredSize = WideCharToMultiByte(CodePage,
  126. NULL,
  127. LpTString,
  128. -1,
  129. NULL,
  130. 0,
  131. NULL,
  132. NULL);
  133. if (RequiredSize == 0)
  134. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  135. TEXT("WideCharToMultiByte failed."));
  136. MultiByteStr.resize(RequiredSize);
  137. if (!WideCharToMultiByte(CodePage,
  138. NULL,
  139. LpTString,
  140. -1,
  141. MultiByteStr.data(),
  142. RequiredSize,
  143. NULL,
  144. NULL))
  145. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  146. TEXT("WideCharToMultiByte failed."));
  147. while (MultiByteStr.back() == '\x00')
  148. MultiByteStr.pop_back();
  149. #else
  150. if (CodePage == CP_ACP)
  151. return RefTStr;
  152. std::wstring WideCharStr;
  153. RequiredSize = MultiByteToWideChar(CP_ACP,
  154. NULL,
  155. LpTString,
  156. -1,
  157. NULL,
  158. 0);
  159. if (RequiredSize == 0)
  160. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  161. TEXT("MultiByteToWideChar failed."));
  162. WideCharStr.resize(RequiredSize);
  163. if (!MultiByteToWideChar(CP_ACP,
  164. NULL,
  165. LpTString,
  166. -1,
  167. WideCharStr.data(),
  168. RequiredSize))
  169. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  170. TEXT("MultiByteToWideChar failed."));
  171. while (WideCharStr.back() == L'\x00')
  172. WideCharStr.pop_back();
  173. RequiredSize = WideCharToMultiByte(CodePage,
  174. NULL,
  175. WideCharStr.c_str(),
  176. -1,
  177. NULL,
  178. 0,
  179. NULL,
  180. NULL);
  181. if (RequiredSize == 0)
  182. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  183. TEXT("WideCharToMultiByte failed."));
  184. MultiByteStr.resize(RequiredSize);
  185. if (!WideCharToMultiByte(CodePage,
  186. NULL,
  187. WideCharStr.c_str(),
  188. -1,
  189. MultiByteStr.data(),
  190. RequiredSize,
  191. NULL,
  192. NULL))
  193. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  194. TEXT("WideCharToMultiByte failed."));
  195. while (MultiByteStr.back() == '\x00')
  196. MultiByteStr.pop_back();
  197. #endif
  198. return MultiByteStr;
  199. }
  200. std::string TStringEncode(const TString& RefTStr, UINT CodePage) {
  201. std::string MultiByteStr;
  202. int RequiredSize;
  203. #ifdef _UNICODE
  204. RequiredSize = WideCharToMultiByte(CodePage,
  205. NULL,
  206. RefTStr.c_str(),
  207. -1,
  208. NULL,
  209. 0,
  210. NULL,
  211. NULL);
  212. if (RequiredSize == 0)
  213. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  214. TEXT("WideCharToMultiByte failed."));
  215. MultiByteStr.resize(RequiredSize);
  216. if (!WideCharToMultiByte(CodePage,
  217. NULL,
  218. RefTStr.c_str(),
  219. -1,
  220. MultiByteStr.data(),
  221. RequiredSize,
  222. NULL,
  223. NULL))
  224. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  225. TEXT("WideCharToMultiByte failed."));
  226. while (MultiByteStr.back() == '\x00')
  227. MultiByteStr.pop_back();
  228. #else
  229. if (CodePage == CP_ACP)
  230. return RefTStr;
  231. std::wstring WideCharStr;
  232. RequiredSize = MultiByteToWideChar(CP_ACP,
  233. NULL,
  234. RefTStr.c_str(),
  235. -1,
  236. NULL,
  237. 0);
  238. if (RequiredSize == 0)
  239. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  240. TEXT("MultiByteToWideChar failed."));
  241. WideCharStr.resize(RequiredSize);
  242. if (!MultiByteToWideChar(CP_ACP,
  243. NULL,
  244. RefTStr.c_str(),
  245. -1,
  246. WideCharStr.data(),
  247. RequiredSize))
  248. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  249. TEXT("MultiByteToWideChar failed."));
  250. while (WideCharStr.back() == L'\x00')
  251. WideCharStr.pop_back();
  252. RequiredSize = WideCharToMultiByte(CodePage,
  253. NULL,
  254. WideCharStr.c_str(),
  255. -1,
  256. NULL,
  257. 0,
  258. NULL,
  259. NULL);
  260. if (RequiredSize == 0)
  261. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  262. TEXT("WideCharToMultiByte failed."));
  263. MultiByteStr.resize(RequiredSize);
  264. if (!WideCharToMultiByte(CodePage,
  265. NULL,
  266. WideCharStr.c_str(),
  267. -1,
  268. MultiByteStr.data(),
  269. RequiredSize,
  270. NULL,
  271. NULL))
  272. throw SystemException(__BASE_FILE__, __LINE__, GetLastError(),
  273. TEXT("WideCharToMultiByte failed."));
  274. while (MultiByteStr.back() == '\x00')
  275. MultiByteStr.pop_back();
  276. #endif
  277. return MultiByteStr;
  278. }
  279. TString TStringDecode(PCSTR LpMultiByteStr, UINT CodePage) {
  280. return TStringBuilder(LpMultiByteStr, CodePage);
  281. }
  282. TString TStringDecode(const std::string& RefMultiByteStr, UINT CodePage) {
  283. return TStringBuilder(RefMultiByteStr.c_str(), CodePage);
  284. }