Advertisement
Guest User

utf8 to wchar

a guest
Feb 15th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. void _GetTextFromFile(const char* fn, std::wstring* str)
  2. {
  3.     str->clear();
  4.     FILE* file = fopen(fn, "rb");
  5.     if (file)
  6.     {
  7.         fseek(file, 0, SEEK_END);
  8.         int file_sz = ftell(file);
  9.         fseek(file, 0, SEEK_SET);
  10.         if (file_sz > 2)
  11.         {
  12.             unsigned char bom[3] = { 0, 0, 0 };
  13.             fread(&bom[0], 3, 1, file);
  14.             //printf("BOM [%#02X %#02X %#02X]\n", bom[0], bom[1], bom[2]);
  15.             fseek(file, 0, SEEK_SET);
  16.  
  17.             if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
  18.             {
  19.                 fseek(file, 3, SEEK_SET);
  20.                 file_sz -= 3;
  21.             }
  22.  
  23.         }
  24.         if (file_sz)
  25.         {
  26.             unsigned char* text_buffer = new unsigned char[file_sz + 1];
  27.             fread(text_buffer, file_sz + 1, 1, file);
  28.             text_buffer[file_sz] = 0;
  29.  
  30.             for (int i = 0; i < file_sz; ++i)
  31.             {
  32.                 wchar_t wch = 0;
  33.  
  34.                 unsigned char ch = text_buffer[i];
  35.  
  36.                 if (ch >= 0 && ch < 0x80)
  37.                 {
  38.                     wch = ch;
  39.                 }
  40.                 else if (ch >= 0xC0 && ch < 0xE0)
  41.                 {
  42.                     if (i + 1 < file_sz)
  43.                     {
  44.                         unsigned char ch2 = text_buffer[i + 1];
  45.  
  46.                         if (ch2)
  47.                         {
  48.                             wchar_t char16 = 0;
  49.                             char16 = ch;
  50.                             char16 ^= 0xC0;
  51.                             char16 <<= 6;
  52.                             char16 |= (ch2 ^ 0x80);
  53.  
  54.                             wch = char16;
  55.                         }
  56.                     }
  57.                 }
  58.                 else if (ch >= 0xE0 && ch < 0xF0) // 3
  59.                 {
  60.                     if (i + 1 < file_sz)
  61.                     {
  62.                         unsigned char ch2 = text_buffer[i + 1];
  63.  
  64.                         if (ch2)
  65.                         {
  66.                             if (i + 1 < file_sz)
  67.                             {
  68.                                 unsigned char ch3 = text_buffer[i + 2];
  69.                                 if (ch3)
  70.                                 {
  71.                                     wchar_t char16 = 0;
  72.                                     char16 = ch;
  73.                                     char16 ^= 0xE0;
  74.                                     char16 <<= 12;
  75.                                     char16 |= (ch2 ^ 0x80) << 6;
  76.                                     char16 |= (ch3 ^ 0x80);
  77.  
  78.                                     wch = char16;
  79.                                 }
  80.                             }
  81.                         }
  82.                     }
  83.                 }
  84.                 str->push_back(wch);
  85.             }
  86.             delete text_buffer;
  87.         }
  88.         fclose(file);
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement