Advertisement
Guest User

Untitled

a guest
Apr 29th, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #ifndef NALL_UTF8_HPP
  2. #define NALL_UTF8_HPP
  3.  
  4. //UTF-8 <> UTF-16 conversion
  5. //used only for Win32; Linux, etc use UTF-8 internally
  6.  
  7. #if defined(_WIN32)
  8.  
  9. #undef UNICODE
  10. #undef _WIN32_WINNT
  11. #undef NOMINMAX
  12. #define UNICODE
  13. #define _WIN32_WINNT 0x0501
  14. #define NOMINMAX
  15. #include <windows.h>
  16. #undef interface
  17.  
  18. namespace nall {
  19. //UTF-8 to UTF-16
  20. class utf16_t {
  21. public:
  22. operator wchar_t*() {
  23. return buffer;
  24. }
  25.  
  26. operator const wchar_t*() const {
  27. return buffer;
  28. }
  29.  
  30. utf16_t(const char *s = "") {
  31. if(!s) s = "";
  32. unsigned length = MultiByteToWideChar(CP_UTF8, 0, s, -1, 0, 0);
  33. buffer = new wchar_t[length + 1]();
  34. MultiByteToWideChar(CP_UTF8, 0, s, -1, buffer, length);
  35. }
  36.  
  37. ~utf16_t() {
  38. delete[] buffer;
  39. }
  40.  
  41. private:
  42. wchar_t *buffer;
  43. };
  44.  
  45. //UTF-16 to UTF-8
  46. class utf8_t {
  47. public:
  48. operator char*() {
  49. return buffer;
  50. }
  51.  
  52. operator const char*() const {
  53. return buffer;
  54. }
  55.  
  56. utf8_t(const wchar_t *s = L"") {
  57. if(!s) s = L"";
  58. unsigned length = WideCharToMultiByte(CP_UTF8, 0, s, -1, 0, 0, (const char*)0, (BOOL*)0);
  59. buffer = new char[length + 1]();
  60. WideCharToMultiByte(CP_UTF8, 0, s, -1, buffer, length, (const char*)0, (BOOL*)0);
  61. }
  62.  
  63. ~utf8_t() {
  64. delete[] buffer;
  65. }
  66.  
  67. utf8_t(const utf8_t&) = delete;
  68. utf8_t& operator=(const utf8_t&) = delete;
  69.  
  70. private:
  71. char *buffer;
  72. };
  73.  
  74. inline void utf8_args(int &argc, char **&argv) {
  75. wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
  76. argv = new char*[argc];
  77. for(unsigned i = 0; i < argc; i++) {
  78. argv[i] = new char[_MAX_PATH];
  79. strcpy(argv[i], nall::utf8_t(wargv[i]));
  80. }
  81. }
  82. }
  83.  
  84. #endif //if defined(_WIN32)
  85.  
  86. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement