Changeset 281 for cpp/common/Convert.cpp


Ignore:
Timestamp:
12/27/14 01:01:43 (9 years ago)
Author:
Maciej Komosinski
Message:

Support for wide char (unicode) names of files and directories under Windows, internally encoded as char* utf-8

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r257 r281  
    128128        return string(ret, 24); //24 znaki z pominieciem ostatniego \n
    129129}
     130
     131string Convert::wstrToUtf8(const wchar_t *str)
     132{
     133        if (str == NULL) return "";
     134        string res;
     135        wchar_t *wcp = (wchar_t*)str;
     136        while (*wcp != 0)
     137        {
     138                int c = *wcp;
     139                if (c < 0x80) res += c;
     140                else if (c < 0x800) { res += 192 + c / 64; res += 128 + c % 64; }
     141                else if (c - 0xd800u < 0x800) res += "<ERROR-CHAR>";
     142                else if (c < 0x10000) { res += 224 + c / 4096; res += 128 + c / 64 % 64; res += 128 + c % 64; }
     143                else if (c < 0x110000) { res += 240 + c / 262144; res += 128 + c / 4096 % 64; res += 128 + c / 64 % 64; res += 128 + c % 64; }
     144                else res += "<ERROR-CHAR>";
     145                wcp++;
     146        }
     147        return res;
     148}
     149
     150#ifdef _WIN32
     151wstring Convert::utf8ToUtf16(const char *str)
     152{
     153        wstring wstr;
     154        int nOffset = 0;
     155        int nDataLen = strlen(str) + 1; //including ending \0
     156        int nLenWide = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str + nOffset),
     157                (int)(nDataLen - nOffset), NULL, 0);
     158        wstr.resize(nLenWide);
     159        if (MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(str + nOffset),
     160                (int)(nDataLen - nOffset),
     161                &wstr[0], nLenWide) != nLenWide)
     162        {
     163                //ASSERT(false); //some conversion error
     164                return wstr;
     165        }
     166        return wstr;
     167}
     168#endif
Note: See TracChangeset for help on using the changeset viewer.