• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See
5 //  accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #ifndef BOOST_LOCLAE_TEST_LOCALE_TOOLS_HPP
10 #define BOOST_LOCLAE_TEST_LOCALE_TOOLS_HPP
11 
12 #include <boost/locale/encoding.hpp>
13 
14 #include <fstream>
15 #include <stdlib.h>
16 #include <stdio.h>
17 
18 template<typename Char>
to_correct_string(std::string const & e,std::locale)19 std::basic_string<Char> to_correct_string(std::string const &e,std::locale /*l*/)
20 {
21     return boost::locale::conv::to_utf<Char>(e,"UTF-8");
22 }
23 
24 
25 template<>
to_correct_string(std::string const & e,std::locale l)26 inline std::string to_correct_string(std::string const &e,std::locale l)
27 {
28     return boost::locale::conv::from_utf(e,l);
29 }
30 
has_std_locale(std::string const & name)31 bool has_std_locale(std::string const &name)
32 {
33     try {
34         std::locale tmp(name.c_str());
35         return true;
36     }
37     catch(...) {
38         return false;
39     }
40 }
41 
test_std_supports_SJIS_codecvt(std::string const & locale_name)42 inline bool test_std_supports_SJIS_codecvt(std::string const &locale_name)
43 {
44     bool res = true;
45     {
46     // Japan in Shift JIS/cp932
47         char const *japan_932 = "\x93\xfa\x96\x7b";
48         std::ofstream f("test-siftjis.txt");
49         f<<japan_932;
50         f.close();
51     }
52     try {
53         std::wfstream test;
54         test.imbue(std::locale(locale_name.c_str()));
55         test.open("test-siftjis.txt");
56         // Japan in Unicode
57         std::wstring cmp = L"\u65e5\u672c";
58         std::wstring ref;
59         test >> ref;
60         res = ref == cmp;
61     }
62     catch(std::exception const &)
63     {
64         res = false;
65     }
66     remove("test-siftjis.txt");
67     return res;
68 }
69 
get_std_name(std::string const & name,std::string * real_name=0)70 std::string get_std_name(std::string const &name,std::string *real_name = 0)
71 {
72     if(has_std_locale(name)) {
73         if(real_name)
74             *real_name = name;
75         return name;
76     }
77 
78     #ifdef BOOST_WINDOWS
79     bool utf8=name.find("UTF-8")!=std::string::npos;
80 
81     if(name=="en_US.UTF-8" || name == "en_US.ISO8859-1") {
82         if(has_std_locale("English_United States.1252")) {
83             if(real_name)
84                 *real_name = "English_United States.1252";
85             return utf8 ? name : "en_US.windows-1252";
86         }
87         return "";
88     }
89     else if(name=="he_IL.UTF-8" || name == "he_IL.ISO8859-8")  {
90         if(has_std_locale("Hebrew_Israel.1255")) {
91             if(real_name)
92                 *real_name = "Hebrew_Israel.1255";
93             return utf8 ? name : "he_IL.windows-1255";
94             return name;
95         }
96     }
97     else if(name=="ru_RU.UTF-8")  {
98         if(has_std_locale("Russian_Russia.1251")) {
99             if(real_name)
100                 *real_name = "Russian_Russia.1251";
101             return name;
102         }
103     }
104     else if(name == "tr_TR.UTF-8") {
105         if(has_std_locale("Turkish_Turkey.1254")) {
106             if(real_name)
107                 *real_name = "Turkish_Turkey.1254";
108             return name;
109         }
110     }
111     if(name == "ja_JP.SJIS") {
112         if(has_std_locale("Japanese_Japan.932")) {
113             if(real_name)
114                 *real_name = "Japanese_Japan.932";
115             return name;
116         }
117         return "";
118     }
119     #endif
120     return "";
121 }
122 
123 
124 
125 #endif
126 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
127