utf8_helper.cpp
changeset 10 15fd256d4adb
parent 0 0aa9866d751c
child 37 863fc3ccd19d
     1.1 --- a/utf8_helper.cpp	Fri Apr 24 17:40:16 2015 +0200
     1.2 +++ b/utf8_helper.cpp	Fri Apr 24 20:24:47 2015 +0200
     1.3 @@ -1,68 +1,119 @@
     1.4  #include "stdafx.h"
     1.5  #include "utf8_helper.h"
     1.6  
     1.7 +using namespace ATL;
     1.8  using namespace std;
     1.9  
    1.10 -string utf8_string(wstring wstr, NORM_FORM norm) {
    1.11 -	if (wstr.size() == 0)
    1.12 -		return string();
    1.13 +namespace pEp {
    1.14 +    namespace utility {
    1.15  
    1.16 -	wstring _wstr_normalized;
    1.17 +        string utf8_string(wstring wstr, NORM_FORM norm)
    1.18 +        {
    1.19 +            if (wstr.size() == 0)
    1.20 +                return string();
    1.21  
    1.22 -	if (norm == NormalizationOther)
    1.23 -		_wstr_normalized = wstr;
    1.24 -	else {
    1.25 -		int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    1.26 -		assert(size > 0);
    1.27 -		if (size > 0) {
    1.28 -			wchar_t *buf = new wchar_t[size];
    1.29 -			NormalizeString(norm, wstr.c_str(), -1, buf, size);
    1.30 -			_wstr_normalized = buf;
    1.31 -			delete[] buf;
    1.32 -		}
    1.33 -		else
    1.34 -			throw out_of_range("input wstring is not valid while normalizing.");
    1.35 -	}
    1.36 -	string result;
    1.37 +            wstring _wstr_normalized;
    1.38  
    1.39 -	int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    1.40 -	assert(size);
    1.41 -	if (size) {
    1.42 -		char *buf = new char[size];
    1.43 -		WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    1.44 -		result = buf;
    1.45 -		delete[] buf;
    1.46 -	}
    1.47 -	else
    1.48 -		throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    1.49 +            if (norm == NormalizationOther)
    1.50 +                _wstr_normalized = wstr;
    1.51 +            else {
    1.52 +                int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    1.53 +                assert(size > 0);
    1.54 +                if (size > 0) {
    1.55 +                    wchar_t *buf = new wchar_t[size];
    1.56 +                    NormalizeString(norm, wstr.c_str(), -1, buf, size);
    1.57 +                    _wstr_normalized = buf;
    1.58 +                    delete[] buf;
    1.59 +                }
    1.60 +                else
    1.61 +                    throw out_of_range("input wstring is not valid while normalizing.");
    1.62 +            }
    1.63 +            string result;
    1.64  
    1.65 -	return result;
    1.66 -}
    1.67 +            int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    1.68 +            assert(size);
    1.69 +            if (size) {
    1.70 +                char *buf = new char[size];
    1.71 +                WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    1.72 +                result = buf;
    1.73 +                delete[] buf;
    1.74 +            }
    1.75 +            else
    1.76 +                throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    1.77  
    1.78 -string utf8_string(BSTR bstr, NORM_FORM norm) {
    1.79 -	return utf8_string((wstring)(wchar_t *)_bstr_t(bstr, true), norm);
    1.80 -}
    1.81 +            return result;
    1.82 +        }
    1.83  
    1.84 -wstring utf16_string(string str) {
    1.85 -	if (str.size() == 0)
    1.86 -		return wstring();
    1.87 +        string utf8_string(BSTR bstr, NORM_FORM norm)
    1.88 +        {
    1.89 +            return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
    1.90 +        }
    1.91  
    1.92 -	wstring result;
    1.93 +        wstring utf16_string(string str)
    1.94 +        {
    1.95 +            if (str.size() == 0)
    1.96 +                return wstring();
    1.97  
    1.98 -	int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
    1.99 -	assert(size);
   1.100 -	if (size) {
   1.101 -		wchar_t * buf = new wchar_t[size];
   1.102 -		MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
   1.103 -		result = buf;
   1.104 -		delete[] buf;
   1.105 -	}
   1.106 -	else
   1.107 -		throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
   1.108 +            wstring result;
   1.109  
   1.110 -	return result;
   1.111 -}
   1.112 +            int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
   1.113 +            assert(size);
   1.114 +            if (size) {
   1.115 +                wchar_t * buf = new wchar_t[size];
   1.116 +                MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
   1.117 +                result = buf;
   1.118 +                delete[] buf;
   1.119 +            }
   1.120 +            else
   1.121 +                throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
   1.122  
   1.123 -_bstr_t utf16_bstr(string str) {
   1.124 -	return _bstr_t(utf16_string(str).c_str());
   1.125 -}
   1.126 +            return result;
   1.127 +        }
   1.128 +
   1.129 +        _bstr_t utf16_bstr(string str)
   1.130 +        {
   1.131 +            return _bstr_t(utf16_string(str).c_str());
   1.132 +        }
   1.133 +
   1.134 +        CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
   1.135 +        {
   1.136 +            CComSafeArray<BSTR> sa_string_list;
   1.137 +            int n = 0;
   1.138 +            for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
   1.139 +                if (k->value) {
   1.140 +                    HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
   1.141 +                    assert(_result == S_OK);
   1.142 +                    if (_result == E_OUTOFMEMORY)
   1.143 +                        throw std::bad_alloc();
   1.144 +                    ++n;
   1.145 +                }
   1.146 +            }
   1.147 +
   1.148 +            return sa_string_list;
   1.149 +        }
   1.150 +
   1.151 +        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
   1.152 +        {
   1.153 +            CComSafeArray<BSTR> sa(safearray);
   1.154 +            int n_strings = 0;
   1.155 +            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
   1.156 +            assert(_stringlist);
   1.157 +            if (_stringlist == NULL)
   1.158 +                throw std::bad_alloc();
   1.159 +
   1.160 +            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
   1.161 +            ::stringlist_t *k = _stringlist;
   1.162 +            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
   1.163 +                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
   1.164 +                assert(k);
   1.165 +                if (k == NULL) {
   1.166 +                    ::free_stringlist(_stringlist);
   1.167 +                    throw std::bad_alloc();
   1.168 +                }
   1.169 +            }
   1.170 +
   1.171 +            return _stringlist;
   1.172 +        }
   1.173 +
   1.174 +    }
   1.175 +}