utf8_helper.cpp
author Volker Birk <vb@pep-project.org>
Sun, 31 May 2015 17:03:40 +0200
changeset 37 863fc3ccd19d
parent 10 15fd256d4adb
child 38 d314ed57180c
permissions -rw-r--r--
...
     1 #include "stdafx.h"
     2 #include "utf8_helper.h"
     3 
     4 using namespace ATL;
     5 using namespace std;
     6 
     7 namespace pEp {
     8     namespace utility {
     9 
    10         string utf8_string(wstring wstr, NORM_FORM norm)
    11         {
    12             if (wstr.size() == 0)
    13                 return string();
    14 
    15             wstring _wstr_normalized;
    16 
    17             if (norm == NormalizationOther)
    18                 _wstr_normalized = wstr;
    19             else {
    20                 int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    21                 assert(size > 0);
    22                 if (size > 0) {
    23                     wchar_t *buf = new wchar_t[size];
    24                     NormalizeString(norm, wstr.c_str(), -1, buf, size);
    25                     _wstr_normalized = buf;
    26                     delete[] buf;
    27                 }
    28                 else
    29                     throw out_of_range("input wstring is not valid while normalizing.");
    30             }
    31             string result;
    32 
    33             int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    34             assert(size);
    35             if (size) {
    36                 char *buf = new char[size];
    37                 WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    38                 result = buf;
    39                 delete[] buf;
    40             }
    41             else
    42                 throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    43 
    44             return result;
    45         }
    46 
    47         string utf8_string(BSTR bstr, NORM_FORM norm)
    48         {
    49             if (bstr == NULL)
    50                 return "";
    51 
    52             return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
    53         }
    54 
    55         wstring utf16_string(string str)
    56         {
    57             if (str.size() == 0)
    58                 return wstring();
    59 
    60             wstring result;
    61 
    62             int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
    63             assert(size);
    64             if (size) {
    65                 wchar_t * buf = new wchar_t[size];
    66                 MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
    67                 result = buf;
    68                 delete[] buf;
    69             }
    70             else
    71                 throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
    72 
    73             return result;
    74         }
    75 
    76         _bstr_t utf16_bstr(string str)
    77         {
    78             return _bstr_t(utf16_string(str).c_str());
    79         }
    80 
    81         CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
    82         {
    83             if (stringlist == NULL)
    84                 return CComSafeArray<BSTR>((ULONG)0);
    85 
    86             CComSafeArray<BSTR> sa_string_list;
    87             int n = 0;
    88             for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
    89                 if (k->value) {
    90                     HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
    91                     assert(_result == S_OK);
    92                     if (_result == E_OUTOFMEMORY)
    93                         throw std::bad_alloc();
    94                     ++n;
    95                 }
    96             }
    97 
    98             return sa_string_list;
    99         }
   100 
   101         ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
   102         {
   103             if (safearray == NULL)
   104                 return NULL;
   105 
   106             CComSafeArray<BSTR> sa(safearray);
   107             int n_strings = 0;
   108             ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
   109             assert(_stringlist);
   110             if (_stringlist == NULL)
   111                 throw std::bad_alloc();
   112 
   113             n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
   114             ::stringlist_t *k = _stringlist;
   115             for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
   116                 k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
   117                 assert(k);
   118                 if (k == NULL) {
   119                     ::free_stringlist(_stringlist);
   120                     throw std::bad_alloc();
   121                 }
   122             }
   123 
   124             return _stringlist;
   125         }
   126 
   127     }
   128 }