utf8_helper.cpp
changeset 42 7d1be3be0c07
parent 40 dfb59114debe
child 47 a364a7231af7
     1.1 --- a/utf8_helper.cpp	Sun May 31 22:41:05 2015 +0200
     1.2 +++ b/utf8_helper.cpp	Sun May 31 23:39:18 2015 +0200
     1.3 @@ -49,7 +49,11 @@
     1.4              if (bstr == NULL)
     1.5                  return "";
     1.6  
     1.7 -            return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
     1.8 +            _bstr_t _bstr(bstr);
     1.9 +            wstring wstr = (wchar_t *) _bstr;
    1.10 +            _bstr.Detach();
    1.11 +
    1.12 +            return utf8_string(wstr, norm);
    1.13          }
    1.14  
    1.15          wstring utf16_string(string str)
    1.16 @@ -79,53 +83,53 @@
    1.17              return _bstr_t(wstr.c_str()).Detach();
    1.18          }
    1.19  
    1.20 -        LPSAFEARRAY string_array(const ::stringlist_t *stringlist)
    1.21 -        {
    1.22 -            int len = ::stringlist_length(stringlist);
    1.23 -
    1.24 -            if (len = 0)
    1.25 -                return NULL;
    1.26 -
    1.27 -            CComSafeArray<BSTR> sa_string_list((LONG) len);
    1.28 -            LONG n = 0;
    1.29 -            for (const ::stringlist_t *k = stringlist; k && k->value; k = k->next) {
    1.30 -                if (k->value) {
    1.31 -                    HRESULT _result = sa_string_list.SetAt(n, utf16_bstr(k->value), false);
    1.32 -                    assert(_result == S_OK);
    1.33 -                    if (_result == E_OUTOFMEMORY)
    1.34 -                        throw std::bad_alloc();
    1.35 -                    ++n;
    1.36 -                }
    1.37 -            }
    1.38 -
    1.39 -            return sa_string_list.Detach();
    1.40 -        }
    1.41 -
    1.42 -        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
    1.43 -        {
    1.44 -            if (safearray == NULL)
    1.45 -                return NULL;
    1.46 -
    1.47 -            CComSafeArray<BSTR> sa(safearray);
    1.48 -            int n_strings = 0;
    1.49 -            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
    1.50 -            assert(_stringlist);
    1.51 -            if (_stringlist == NULL)
    1.52 -                throw std::bad_alloc();
    1.53 -
    1.54 -            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
    1.55 -            ::stringlist_t *k = _stringlist;
    1.56 -            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
    1.57 -                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
    1.58 -                assert(k);
    1.59 -                if (k == NULL) {
    1.60 -                    ::free_stringlist(_stringlist);
    1.61 -                    throw std::bad_alloc();
    1.62 -                }
    1.63 -            }
    1.64 -
    1.65 -            return _stringlist;
    1.66 -        }
    1.67 -
    1.68 -    }
    1.69 -}
    1.70 +        LPSAFEARRAY string_array(const ::stringlist_t *stringlist)
    1.71 +        {
    1.72 +            int len = ::stringlist_length(stringlist);
    1.73 +
    1.74 +            if (len == 0)
    1.75 +                return NULL;
    1.76 +
    1.77 +            CComSafeArray<BSTR> sa_string_list((LONG) len);
    1.78 +            LONG n = 0;
    1.79 +            for (const ::stringlist_t *k = stringlist; k && k->value; k = k->next) {
    1.80 +                if (k->value) {
    1.81 +                    HRESULT _result = sa_string_list.SetAt(n, utf16_bstr(k->value), false);
    1.82 +                    assert(_result == S_OK);
    1.83 +                    if (_result == E_OUTOFMEMORY)
    1.84 +                        throw std::bad_alloc();
    1.85 +                    ++n;
    1.86 +                }
    1.87 +            }
    1.88 +
    1.89 +            return sa_string_list.Detach();
    1.90 +        }
    1.91 +
    1.92 +        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
    1.93 +        {
    1.94 +            if (safearray == NULL)
    1.95 +                return NULL;
    1.96 +
    1.97 +            CComSafeArray<BSTR> sa(safearray);
    1.98 +            int n_strings = 0;
    1.99 +            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
   1.100 +            assert(_stringlist);
   1.101 +            if (_stringlist == NULL)
   1.102 +                throw std::bad_alloc();
   1.103 +
   1.104 +            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
   1.105 +            ::stringlist_t *k = _stringlist;
   1.106 +            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
   1.107 +                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
   1.108 +                assert(k);
   1.109 +                if (k == NULL) {
   1.110 +                    ::free_stringlist(_stringlist);
   1.111 +                    throw std::bad_alloc();
   1.112 +                }
   1.113 +            }
   1.114 +
   1.115 +            return _stringlist;
   1.116 +        }
   1.117 +
   1.118 +    }
   1.119 +}