moving into cpp file
authorVolker Birk <vb@pep-project.org>
Fri, 24 Apr 2015 20:24:47 +0200
changeset 1015fd256d4adb
parent 9 12c8f92de54e
child 11 76a1b3bfc78f
moving into cpp file
CpEpEngine.cpp
CpEpEngine.h
TextMessage.cpp
pEpCOMServerAdapter.vcxproj
pEpCOMServerAdapter.vcxproj.filters
pEp_identity_helper.cpp
pEp_identity_helper.h
utf8_helper.cpp
utf8_helper.h
     1.1 --- a/CpEpEngine.cpp	Fri Apr 24 17:40:16 2015 +0200
     1.2 +++ b/CpEpEngine.cpp	Fri Apr 24 20:24:47 2015 +0200
     1.3 @@ -3,6 +3,8 @@
     1.4  #include "stdafx.h"
     1.5  #include "CpEpEngine.h"
     1.6  
     1.7 +using namespace std;
     1.8 +using namespace pEp::utility;
     1.9  
    1.10  // CpEpEngine
    1.11  
    1.12 @@ -23,48 +25,6 @@
    1.13  
    1.14  #define FAIL(msg) error(msg)
    1.15  
    1.16 -using namespace std;
    1.17 -
    1.18 -static CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
    1.19 -{
    1.20 -    CComSafeArray<BSTR> sa_string_list;
    1.21 -    int n = 0;
    1.22 -    for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
    1.23 -        if (k->value) {
    1.24 -            HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
    1.25 -            assert(_result == S_OK);
    1.26 -            if (_result == E_OUTOFMEMORY)
    1.27 -                throw std::bad_alloc();
    1.28 -            ++n;
    1.29 -        }
    1.30 -    }
    1.31 -
    1.32 -    return sa_string_list;
    1.33 -}
    1.34 -
    1.35 -static ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
    1.36 -{
    1.37 -    CComSafeArray<BSTR> sa(safearray);
    1.38 -    int n_strings = 0;
    1.39 -    ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
    1.40 -    assert(_stringlist);
    1.41 -    if (_stringlist == NULL)
    1.42 -        throw std::bad_alloc();
    1.43 -
    1.44 -    n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
    1.45 -    ::stringlist_t *k = _stringlist;
    1.46 -    for (int i = 0, j = sa.GetLowerBound(); i<n_strings; ++i, ++j) {
    1.47 -        k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
    1.48 -        assert(k);
    1.49 -        if (k == NULL) {
    1.50 -            ::free_stringlist(_stringlist);
    1.51 -            throw std::bad_alloc();
    1.52 -        }
    1.53 -    }
    1.54 -
    1.55 -    return _stringlist;
    1.56 -}
    1.57 -
    1.58  // CpEpEngine
    1.59  
    1.60  STDMETHODIMP CpEpEngine::log(BSTR title, BSTR entity, BSTR description, BSTR comment)
     2.1 --- a/CpEpEngine.h	Fri Apr 24 17:40:16 2015 +0200
     2.2 +++ b/CpEpEngine.h	Fri Apr 24 20:24:47 2015 +0200
     2.3 @@ -18,7 +18,7 @@
     2.4  
     2.5  using namespace ATL;
     2.6  using namespace utility;
     2.7 -
     2.8 +using namespace pEp::utility;
     2.9  
    2.10  // CpEpEngine
    2.11  
     3.1 --- a/TextMessage.cpp	Fri Apr 24 17:40:16 2015 +0200
     3.2 +++ b/TextMessage.cpp	Fri Apr 24 20:24:47 2015 +0200
     3.3 @@ -3,6 +3,7 @@
     3.4  #include "stdafx.h"
     3.5  #include "TextMessage.h"
     3.6  
     3.7 +using namespace pEp::utility;
     3.8  
     3.9  // CTextMessage
    3.10  
     4.1 --- a/pEpCOMServerAdapter.vcxproj	Fri Apr 24 17:40:16 2015 +0200
     4.2 +++ b/pEpCOMServerAdapter.vcxproj	Fri Apr 24 20:24:47 2015 +0200
     4.3 @@ -123,6 +123,7 @@
     4.4        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
     4.5        </PrecompiledHeader>
     4.6      </ClCompile>
     4.7 +    <ClCompile Include="pEp_identity_helper.cpp" />
     4.8      <ClCompile Include="stdafx.cpp">
     4.9        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
    4.10        <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
     5.1 --- a/pEpCOMServerAdapter.vcxproj.filters	Fri Apr 24 17:40:16 2015 +0200
     5.2 +++ b/pEpCOMServerAdapter.vcxproj.filters	Fri Apr 24 20:24:47 2015 +0200
     5.3 @@ -40,6 +40,9 @@
     5.4      <ClCompile Include="TextMessage.cpp">
     5.5        <Filter>Source Files</Filter>
     5.6      </ClCompile>
     5.7 +    <ClCompile Include="pEp_identity_helper.cpp">
     5.8 +      <Filter>Source Files</Filter>
     5.9 +    </ClCompile>
    5.10    </ItemGroup>
    5.11    <ItemGroup>
    5.12      <ClInclude Include="stdafx.h">
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/pEp_identity_helper.cpp	Fri Apr 24 20:24:47 2015 +0200
     6.3 @@ -0,0 +1,163 @@
     6.4 +#include "stdafx.h"
     6.5 +#include "pEp_identity_helper.h"
     6.6 +
     6.7 +namespace pEp {
     6.8 +    namespace utility {
     6.9 +        pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
    6.10 +            : me(false)
    6.11 +        {
    6.12 +            if (_ident->address)
    6.13 +                address = _ident->address;
    6.14 +            if (_ident->fpr)
    6.15 +                fpr = _ident->fpr;
    6.16 +            if (_ident->user_id)
    6.17 +                user_id = _ident->user_id;
    6.18 +            if (_ident->username)
    6.19 +                username = _ident->username;
    6.20 +            comm_type = (pEp_comm_type) _ident->comm_type;
    6.21 +            lang = _ident->lang;
    6.22 +        }
    6.23 +
    6.24 +        pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_ident)
    6.25 +            : me(false)
    6.26 +        {
    6.27 +            if (_ident->address)
    6.28 +                address = utf8_string(_ident->address);
    6.29 +            if (_ident->fpr)
    6.30 +                fpr = utf8_string(_ident->fpr);
    6.31 +            if (_ident->user_id)
    6.32 +                user_id = utf8_string(_ident->user_id);
    6.33 +            if (_ident->username)
    6.34 +                username = utf8_string(_ident->username);
    6.35 +            comm_type = _ident->comm_type;
    6.36 +            if (_ident->lang)
    6.37 +                lang = utf8_string(_ident->lang);
    6.38 +        }
    6.39 +
    6.40 +        pEp_identity * pEp_identity_cpp::to_pEp_identity()
    6.41 +        {
    6.42 +            ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
    6.43 +            assert(_ident);
    6.44 +            if (_ident == NULL)
    6.45 +                throw bad_alloc();
    6.46 +
    6.47 +            _ident->comm_type = (::PEP_comm_type) this->comm_type;
    6.48 +            _ident->me = this->me;
    6.49 +
    6.50 +            assert(this->lang.size() == 0 || this->lang.size() == 2);
    6.51 +
    6.52 +            if (this->lang.size()) {
    6.53 +                _ident->lang[0] = this->lang[0];
    6.54 +                _ident->lang[1] = this->lang[1];
    6.55 +            }
    6.56 +
    6.57 +            return _ident;
    6.58 +        }
    6.59 +
    6.60 +        pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
    6.61 +        {
    6.62 +            pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
    6.63 +            assert(_ident);
    6.64 +            if (_ident == NULL)
    6.65 +                throw bad_alloc();
    6.66 +
    6.67 +            _ident->address = utf16_bstr(this->address).Detach();
    6.68 +            _ident->comm_type = this->comm_type;
    6.69 +            _ident->fpr = utf16_bstr(this->fpr).Detach();
    6.70 +            _ident->lang = utf16_bstr(this->lang).Detach();
    6.71 +            _ident->username = utf16_bstr(this->username).Detach();
    6.72 +            _ident->user_id = utf16_bstr(this->user_id).Detach();
    6.73 +
    6.74 +            return _ident;
    6.75 +        }
    6.76 +
    6.77 +        void free_pEp_identity_s(pEp_identity_s *ident)
    6.78 +        {
    6.79 +            if (ident) {
    6.80 +                SysFreeString(ident->address);
    6.81 +                SysFreeString(ident->fpr);
    6.82 +                SysFreeString(ident->lang);
    6.83 +                SysFreeString(ident->username);
    6.84 +                SysFreeString(ident->user_id);
    6.85 +                free(ident);
    6.86 +            }
    6.87 +        }
    6.88 +
    6.89 +        void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
    6.90 +        {
    6.91 +            assert(ident_s);
    6.92 +
    6.93 +            ::memset(ident_s, 0, sizeof(pEp_identity_s));
    6.94 +            if (ident) {
    6.95 +                if (ident->address)
    6.96 +                    ident_s->address = utf16_bstr(ident->address).Detach();
    6.97 +                if (ident->fpr)
    6.98 +                    ident_s->fpr = utf16_bstr(ident->fpr).Detach();
    6.99 +                if (ident->user_id)
   6.100 +                    ident_s->user_id = utf16_bstr(ident->user_id).Detach();
   6.101 +                if (ident->username)
   6.102 +                    ident_s->username = utf16_bstr(ident->username).Detach();
   6.103 +                ident_s->comm_type = (pEp_comm_type) ident->comm_type;
   6.104 +                if (ident->lang)
   6.105 +                    ident_s->lang = utf16_bstr(ident->lang).Detach();
   6.106 +            }
   6.107 +        }
   6.108 +
   6.109 +        ::pEp_identity *new_identity(const pEp_identity_s * ident)
   6.110 +        {
   6.111 +            ::pEp_identity *_ident;
   6.112 +
   6.113 +            string _address;
   6.114 +            string _fpr;
   6.115 +            string _user_id;
   6.116 +            string _username;
   6.117 +
   6.118 +            if (ident->address)
   6.119 +                _address = utf8_string(ident->address);
   6.120 +            if (ident->fpr) {
   6.121 +                _fpr = utf8_string(ident->fpr);
   6.122 +                for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
   6.123 +                    if (*p >= 'A' && *p <= 'Z')
   6.124 +                        continue;
   6.125 +                    if (*p >= '0' && *p <= '9')
   6.126 +                        continue;
   6.127 +                    throw invalid_argument("invalid hex digits in fingerprint");
   6.128 +                }
   6.129 +            }
   6.130 +            if (ident->user_id)
   6.131 +                _user_id = utf8_string(ident->user_id);
   6.132 +            if (ident->username)
   6.133 +                _username = utf8_string(ident->username);
   6.134 +
   6.135 +            _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
   6.136 +            assert(_ident);
   6.137 +            if (_ident == NULL)
   6.138 +                throw bad_alloc();
   6.139 +
   6.140 +            _ident->comm_type = (PEP_comm_type) ident->comm_type;
   6.141 +
   6.142 +            if (ident->lang) {
   6.143 +                string _lang = utf8_string(ident->lang);
   6.144 +                if (_lang.length() != 0) {
   6.145 +                    if (_lang.length() != 2) {
   6.146 +                        ::free_identity(_ident);
   6.147 +                        throw invalid_argument("invalid language code");
   6.148 +                    }
   6.149 +                    if (_lang[0] < 'a' || _lang[0] > 'z') {
   6.150 +                        ::free_identity(_ident);
   6.151 +                        throw invalid_argument("invalid language code");
   6.152 +                    }
   6.153 +                    if (_lang[1] < 'a' || _lang[1] > 'z') {
   6.154 +                        ::free_identity(_ident);
   6.155 +                        throw invalid_argument("invalid language code");
   6.156 +                    }
   6.157 +                    _ident->lang[0] = _lang[0];
   6.158 +                    _ident->lang[1] = _lang[1];
   6.159 +                }
   6.160 +            }
   6.161 +
   6.162 +            return _ident;
   6.163 +        }
   6.164 +
   6.165 +    }
   6.166 +}
     7.1 --- a/pEp_identity_helper.h	Fri Apr 24 17:40:16 2015 +0200
     7.2 +++ b/pEp_identity_helper.h	Fri Apr 24 20:24:47 2015 +0200
     7.3 @@ -6,178 +6,38 @@
     7.4  
     7.5  using namespace std;
     7.6  
     7.7 -struct pEp_identity_cpp {
     7.8 -    string address;
     7.9 -    string fpr;
    7.10 -    string user_id;
    7.11 -    string username;
    7.12 -    pEp_comm_type comm_type;
    7.13 -    string lang;
    7.14 -    bool me;
    7.15 +namespace pEp {
    7.16 +    namespace utility {
    7.17  
    7.18 -    pEp_identity_cpp(
    7.19 -        string _address = string(),
    7.20 -        string _fpr = string(),
    7.21 -        string _user_id = string(),
    7.22 -        string _username = string(),
    7.23 -        pEp_comm_type _comm_type = pEp_ct_unknown,
    7.24 -        string _lang = string()
    7.25 -        ) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang), me(false)
    7.26 -    { }
    7.27 +        struct pEp_identity_cpp {
    7.28 +            string address;
    7.29 +            string fpr;
    7.30 +            string user_id;
    7.31 +            string username;
    7.32 +            pEp_comm_type comm_type;
    7.33 +            string lang;
    7.34 +            bool me;
    7.35  
    7.36 -    pEp_identity_cpp(const ::pEp_identity *_ident)
    7.37 -        : me(false)
    7.38 -    {
    7.39 -        if (_ident->address)
    7.40 -            address = _ident->address;
    7.41 -        if (_ident->fpr)
    7.42 -            fpr = _ident->fpr;
    7.43 -        if (_ident->user_id)
    7.44 -            user_id = _ident->user_id;
    7.45 -        if (_ident->username)
    7.46 -            username = _ident->username;
    7.47 -        comm_type = (pEp_comm_type) _ident->comm_type;
    7.48 -        lang = _ident->lang;
    7.49 -    }
    7.50 +            pEp_identity_cpp(
    7.51 +                string _address = string(),
    7.52 +                string _fpr = string(),
    7.53 +                string _user_id = string(),
    7.54 +                string _username = string(),
    7.55 +                pEp_comm_type _comm_type = pEp_ct_unknown,
    7.56 +                string _lang = string()
    7.57 +                ) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang), me(false)
    7.58 +            { }
    7.59  
    7.60 -    pEp_identity_cpp(const pEp_identity_s *_ident)
    7.61 -        : me(false)
    7.62 -    {
    7.63 -        if (_ident->address)
    7.64 -            address = utf8_string(_ident->address);
    7.65 -        if (_ident->fpr)
    7.66 -            fpr = utf8_string(_ident->fpr);
    7.67 -        if (_ident->user_id)
    7.68 -            user_id = utf8_string(_ident->user_id);
    7.69 -        if (_ident->username)
    7.70 -            username = utf8_string(_ident->username);
    7.71 -        comm_type = _ident->comm_type;
    7.72 -        if (_ident->lang)
    7.73 -            lang = utf8_string(_ident->lang);
    7.74 -    }
    7.75 +            pEp_identity_cpp(const ::pEp_identity *_ident);
    7.76 +            pEp_identity_cpp(const pEp_identity_s *_ident);
    7.77  
    7.78 -    pEp_identity * to_pEp_identity()
    7.79 -    {
    7.80 -        ::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
    7.81 -        assert(_ident);
    7.82 -        if (_ident == NULL)
    7.83 -            throw bad_alloc();
    7.84 +            pEp_identity * to_pEp_identity();
    7.85 +            pEp_identity_s * to_pEp_identity_s();
    7.86 +        };
    7.87  
    7.88 -        _ident->comm_type = (::PEP_comm_type) this->comm_type;
    7.89 -        _ident->me = this->me;
    7.90 +        void free_pEp_identity_s(pEp_identity_s *ident);
    7.91 +        void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident);
    7.92 +        ::pEp_identity *new_identity(const pEp_identity_s * ident);
    7.93  
    7.94 -        assert(this->lang.size() == 0 || this->lang.size() == 2);
    7.95 -
    7.96 -        if (this->lang.size()) {
    7.97 -            _ident->lang[0] = this->lang[0];
    7.98 -            _ident->lang[1] = this->lang[1];
    7.99 -        }
   7.100 -
   7.101 -        return _ident;
   7.102 -    }
   7.103 -
   7.104 -    pEp_identity_s * to_pEp_identity_s()
   7.105 -    {
   7.106 -        pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
   7.107 -        assert(_ident);
   7.108 -        if (_ident == NULL)
   7.109 -            throw bad_alloc();
   7.110 -
   7.111 -        _ident->address = utf16_bstr(this->address).Detach();
   7.112 -        _ident->comm_type = this->comm_type;
   7.113 -        _ident->fpr = utf16_bstr(this->fpr).Detach();
   7.114 -        _ident->lang = utf16_bstr(this->lang).Detach();
   7.115 -        _ident->username = utf16_bstr(this->username).Detach();
   7.116 -        _ident->user_id = utf16_bstr(this->user_id).Detach();
   7.117 -
   7.118 -        return _ident;
   7.119 -    }
   7.120 -};
   7.121 -
   7.122 -static void free_pEp_identity_s(pEp_identity_s *ident)
   7.123 -{
   7.124 -    if (ident) {
   7.125 -        SysFreeString(ident->address);
   7.126 -        SysFreeString(ident->fpr);
   7.127 -        SysFreeString(ident->lang);
   7.128 -        SysFreeString(ident->username);
   7.129 -        SysFreeString(ident->user_id);
   7.130 -        free(ident);
   7.131      }
   7.132  }
   7.133 -
   7.134 -static void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
   7.135 -{
   7.136 -    assert(ident_s);
   7.137 -
   7.138 -    ::memset(ident_s, 0, sizeof(pEp_identity_s));
   7.139 -    if (ident) {
   7.140 -        if (ident->address)
   7.141 -            ident_s->address = utf16_bstr(ident->address).Detach();
   7.142 -        if (ident->fpr)
   7.143 -            ident_s->fpr = utf16_bstr(ident->fpr).Detach();
   7.144 -        if (ident->user_id)
   7.145 -            ident_s->user_id = utf16_bstr(ident->user_id).Detach();
   7.146 -        if (ident->username)
   7.147 -            ident_s->username = utf16_bstr(ident->username).Detach();
   7.148 -        ident_s->comm_type = (pEp_comm_type) ident->comm_type;
   7.149 -        if (ident->lang)
   7.150 -            ident_s->lang = utf16_bstr(ident->lang).Detach();
   7.151 -    }
   7.152 -}
   7.153 -
   7.154 -static ::pEp_identity *new_identity(const pEp_identity_s * ident)
   7.155 -{
   7.156 -    ::pEp_identity *_ident;
   7.157 -
   7.158 -    string _address;
   7.159 -    string _fpr;
   7.160 -    string _user_id;
   7.161 -    string _username;
   7.162 -
   7.163 -    if (ident->address)
   7.164 -        _address = utf8_string(ident->address);
   7.165 -    if (ident->fpr) {
   7.166 -        _fpr = utf8_string(ident->fpr);
   7.167 -        for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
   7.168 -            if (*p >= 'A' && *p <= 'Z')
   7.169 -                continue;
   7.170 -            if (*p >= '0' && *p <= '9')
   7.171 -                continue;
   7.172 -            throw invalid_argument("invalid hex digits in fingerprint");
   7.173 -        }
   7.174 -    }
   7.175 -    if (ident->user_id)
   7.176 -        _user_id = utf8_string(ident->user_id);
   7.177 -    if (ident->username)
   7.178 -        _username = utf8_string(ident->username);
   7.179 -
   7.180 -    _ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
   7.181 -    assert(_ident);
   7.182 -    if (_ident == NULL)
   7.183 -        throw bad_alloc();
   7.184 -
   7.185 -    _ident->comm_type = (PEP_comm_type) ident->comm_type;
   7.186 -
   7.187 -    if (ident->lang) {
   7.188 -        string _lang = utf8_string(ident->lang);
   7.189 -        if (_lang.length() != 0) {
   7.190 -            if (_lang.length() != 2) {
   7.191 -                ::free_identity(_ident);
   7.192 -                throw invalid_argument("invalid language code");
   7.193 -            }
   7.194 -            if (_lang[0] < 'a' || _lang[0] > 'z') {
   7.195 -                ::free_identity(_ident);
   7.196 -                throw invalid_argument("invalid language code");
   7.197 -            }
   7.198 -            if (_lang[1] < 'a' || _lang[1] > 'z') {
   7.199 -                ::free_identity(_ident);
   7.200 -                throw invalid_argument("invalid language code");
   7.201 -            }
   7.202 -            _ident->lang[0] = _lang[0];
   7.203 -            _ident->lang[1] = _lang[1];
   7.204 -        }
   7.205 -    }
   7.206 -
   7.207 -    return _ident;
   7.208 -}
     8.1 --- a/utf8_helper.cpp	Fri Apr 24 17:40:16 2015 +0200
     8.2 +++ b/utf8_helper.cpp	Fri Apr 24 20:24:47 2015 +0200
     8.3 @@ -1,68 +1,119 @@
     8.4  #include "stdafx.h"
     8.5  #include "utf8_helper.h"
     8.6  
     8.7 +using namespace ATL;
     8.8  using namespace std;
     8.9  
    8.10 -string utf8_string(wstring wstr, NORM_FORM norm) {
    8.11 -	if (wstr.size() == 0)
    8.12 -		return string();
    8.13 +namespace pEp {
    8.14 +    namespace utility {
    8.15  
    8.16 -	wstring _wstr_normalized;
    8.17 +        string utf8_string(wstring wstr, NORM_FORM norm)
    8.18 +        {
    8.19 +            if (wstr.size() == 0)
    8.20 +                return string();
    8.21  
    8.22 -	if (norm == NormalizationOther)
    8.23 -		_wstr_normalized = wstr;
    8.24 -	else {
    8.25 -		int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    8.26 -		assert(size > 0);
    8.27 -		if (size > 0) {
    8.28 -			wchar_t *buf = new wchar_t[size];
    8.29 -			NormalizeString(norm, wstr.c_str(), -1, buf, size);
    8.30 -			_wstr_normalized = buf;
    8.31 -			delete[] buf;
    8.32 -		}
    8.33 -		else
    8.34 -			throw out_of_range("input wstring is not valid while normalizing.");
    8.35 -	}
    8.36 -	string result;
    8.37 +            wstring _wstr_normalized;
    8.38  
    8.39 -	int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    8.40 -	assert(size);
    8.41 -	if (size) {
    8.42 -		char *buf = new char[size];
    8.43 -		WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    8.44 -		result = buf;
    8.45 -		delete[] buf;
    8.46 -	}
    8.47 -	else
    8.48 -		throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    8.49 +            if (norm == NormalizationOther)
    8.50 +                _wstr_normalized = wstr;
    8.51 +            else {
    8.52 +                int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    8.53 +                assert(size > 0);
    8.54 +                if (size > 0) {
    8.55 +                    wchar_t *buf = new wchar_t[size];
    8.56 +                    NormalizeString(norm, wstr.c_str(), -1, buf, size);
    8.57 +                    _wstr_normalized = buf;
    8.58 +                    delete[] buf;
    8.59 +                }
    8.60 +                else
    8.61 +                    throw out_of_range("input wstring is not valid while normalizing.");
    8.62 +            }
    8.63 +            string result;
    8.64  
    8.65 -	return result;
    8.66 -}
    8.67 +            int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    8.68 +            assert(size);
    8.69 +            if (size) {
    8.70 +                char *buf = new char[size];
    8.71 +                WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    8.72 +                result = buf;
    8.73 +                delete[] buf;
    8.74 +            }
    8.75 +            else
    8.76 +                throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    8.77  
    8.78 -string utf8_string(BSTR bstr, NORM_FORM norm) {
    8.79 -	return utf8_string((wstring)(wchar_t *)_bstr_t(bstr, true), norm);
    8.80 -}
    8.81 +            return result;
    8.82 +        }
    8.83  
    8.84 -wstring utf16_string(string str) {
    8.85 -	if (str.size() == 0)
    8.86 -		return wstring();
    8.87 +        string utf8_string(BSTR bstr, NORM_FORM norm)
    8.88 +        {
    8.89 +            return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
    8.90 +        }
    8.91  
    8.92 -	wstring result;
    8.93 +        wstring utf16_string(string str)
    8.94 +        {
    8.95 +            if (str.size() == 0)
    8.96 +                return wstring();
    8.97  
    8.98 -	int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
    8.99 -	assert(size);
   8.100 -	if (size) {
   8.101 -		wchar_t * buf = new wchar_t[size];
   8.102 -		MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
   8.103 -		result = buf;
   8.104 -		delete[] buf;
   8.105 -	}
   8.106 -	else
   8.107 -		throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
   8.108 +            wstring result;
   8.109  
   8.110 -	return result;
   8.111 -}
   8.112 +            int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
   8.113 +            assert(size);
   8.114 +            if (size) {
   8.115 +                wchar_t * buf = new wchar_t[size];
   8.116 +                MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
   8.117 +                result = buf;
   8.118 +                delete[] buf;
   8.119 +            }
   8.120 +            else
   8.121 +                throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
   8.122  
   8.123 -_bstr_t utf16_bstr(string str) {
   8.124 -	return _bstr_t(utf16_string(str).c_str());
   8.125 -}
   8.126 +            return result;
   8.127 +        }
   8.128 +
   8.129 +        _bstr_t utf16_bstr(string str)
   8.130 +        {
   8.131 +            return _bstr_t(utf16_string(str).c_str());
   8.132 +        }
   8.133 +
   8.134 +        CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
   8.135 +        {
   8.136 +            CComSafeArray<BSTR> sa_string_list;
   8.137 +            int n = 0;
   8.138 +            for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
   8.139 +                if (k->value) {
   8.140 +                    HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
   8.141 +                    assert(_result == S_OK);
   8.142 +                    if (_result == E_OUTOFMEMORY)
   8.143 +                        throw std::bad_alloc();
   8.144 +                    ++n;
   8.145 +                }
   8.146 +            }
   8.147 +
   8.148 +            return sa_string_list;
   8.149 +        }
   8.150 +
   8.151 +        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
   8.152 +        {
   8.153 +            CComSafeArray<BSTR> sa(safearray);
   8.154 +            int n_strings = 0;
   8.155 +            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
   8.156 +            assert(_stringlist);
   8.157 +            if (_stringlist == NULL)
   8.158 +                throw std::bad_alloc();
   8.159 +
   8.160 +            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
   8.161 +            ::stringlist_t *k = _stringlist;
   8.162 +            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
   8.163 +                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
   8.164 +                assert(k);
   8.165 +                if (k == NULL) {
   8.166 +                    ::free_stringlist(_stringlist);
   8.167 +                    throw std::bad_alloc();
   8.168 +                }
   8.169 +            }
   8.170 +
   8.171 +            return _stringlist;
   8.172 +        }
   8.173 +
   8.174 +    }
   8.175 +}
     9.1 --- a/utf8_helper.h	Fri Apr 24 17:40:16 2015 +0200
     9.2 +++ b/utf8_helper.h	Fri Apr 24 20:24:47 2015 +0200
     9.3 @@ -1,6 +1,14 @@
     9.4  #pragma once
     9.5  
     9.6 -std::string utf8_string(std::wstring wstr, NORM_FORM norm = NormalizationC);
     9.7 -std::string utf8_string(BSTR bstr, NORM_FORM norm = NormalizationC);
     9.8 -std::wstring utf16_string(std::string str);
     9.9 -_bstr_t utf16_bstr(std::string str);
    9.10 +namespace pEp {
    9.11 +    namespace utility {
    9.12 +
    9.13 +        std::string utf8_string(std::wstring wstr, NORM_FORM norm = NormalizationC);
    9.14 +        std::string utf8_string(BSTR bstr, NORM_FORM norm = NormalizationC);
    9.15 +        std::wstring utf16_string(std::string str);
    9.16 +        _bstr_t utf16_bstr(std::string str);
    9.17 +        ATL::CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist);
    9.18 +        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray);
    9.19 +
    9.20 +    }
    9.21 +}