PageRenderTime 25ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Root-branch-php-utl/SWIG/Examples/ocaml/std_string/example.h

#
C++ Header | 36 lines | 32 code | 3 blank | 1 comment | 0 complexity | b3c57187aeb882353d5e78cbc61bed49 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
 1/* File : example.h -- stolen from the guile std_vector example */
 2
 3#include <string>
 4#include <algorithm>
 5#include <functional>
 6#include <numeric>
 7#include <stdlib.h>
 8#include <locale.h>
 9
10std::string from_wstring_with_locale( const std::wstring source,
11				      const std::string locale ) {
12    const char *current_locale = setlocale( LC_CTYPE, locale.c_str() );
13    int required_chars = wcstombs( NULL, source.c_str(), 0 );
14    std::string s;
15    char *temp_chars = new char[required_chars + 1];
16    temp_chars[0] = 0;
17    wcstombs( temp_chars, source.c_str(), required_chars + 1 );
18    s = temp_chars;
19    delete [] temp_chars;
20    setlocale( LC_CTYPE, current_locale );
21    return s;
22}
23
24std::wstring to_wstring_with_locale( const std::string source,
25				     const std::string locale ) {
26    const char *current_locale = setlocale( LC_CTYPE, locale.c_str() );
27    int required_chars = mbstowcs( NULL, source.c_str(), 0 );
28    std::wstring s;
29    wchar_t *temp_chars = new wchar_t[required_chars + 1];
30    temp_chars[0] = 0;
31    mbstowcs( temp_chars, source.c_str(), required_chars + 1 );
32    s = temp_chars;
33    delete [] temp_chars;
34    setlocale( LC_CTYPE, current_locale );
35    return s;
36}