Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void _GetTextFromFile(const char* fn, std::wstring* str)
- {
- str->clear();
- FILE* file = fopen(fn, "rb");
- if (file)
- {
- fseek(file, 0, SEEK_END);
- int file_sz = ftell(file);
- fseek(file, 0, SEEK_SET);
- if (file_sz > 2)
- {
- unsigned char bom[3] = { 0, 0, 0 };
- fread(&bom[0], 3, 1, file);
- //printf("BOM [%#02X %#02X %#02X]\n", bom[0], bom[1], bom[2]);
- fseek(file, 0, SEEK_SET);
- if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
- {
- fseek(file, 3, SEEK_SET);
- file_sz -= 3;
- }
- }
- if (file_sz)
- {
- unsigned char* text_buffer = new unsigned char[file_sz + 1];
- fread(text_buffer, file_sz + 1, 1, file);
- text_buffer[file_sz] = 0;
- for (int i = 0; i < file_sz; ++i)
- {
- wchar_t wch = 0;
- unsigned char ch = text_buffer[i];
- if (ch >= 0 && ch < 0x80)
- {
- wch = ch;
- }
- else if (ch >= 0xC0 && ch < 0xE0)
- {
- if (i + 1 < file_sz)
- {
- unsigned char ch2 = text_buffer[i + 1];
- if (ch2)
- {
- wchar_t char16 = 0;
- char16 = ch;
- char16 ^= 0xC0;
- char16 <<= 6;
- char16 |= (ch2 ^ 0x80);
- wch = char16;
- }
- }
- }
- else if (ch >= 0xE0 && ch < 0xF0) // 3
- {
- if (i + 1 < file_sz)
- {
- unsigned char ch2 = text_buffer[i + 1];
- if (ch2)
- {
- if (i + 1 < file_sz)
- {
- unsigned char ch3 = text_buffer[i + 2];
- if (ch3)
- {
- wchar_t char16 = 0;
- char16 = ch;
- char16 ^= 0xE0;
- char16 <<= 12;
- char16 |= (ch2 ^ 0x80) << 6;
- char16 |= (ch3 ^ 0x80);
- wch = char16;
- }
- }
- }
- }
- }
- str->push_back(wch);
- }
- delete text_buffer;
- }
- fclose(file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement