blob: 67542e6c6bc06e9c1f0ace859b65c4314f52b480 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOST_LOCALE_SOURCE
#include "lcid.hpp"
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include "../util/locale_data.hpp"
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <boost/thread/mutex.hpp>
namespace boost {
namespace locale {
namespace impl_win {
typedef std::map<std::string,unsigned> table_type;
static table_type * volatile table = 0;
boost::mutex &lcid_table_mutex()
{
static boost::mutex m;
return m;
}
table_type &real_lcid_table()
{
static table_type table;
return table;
}
BOOL CALLBACK proc(char *s)
{
table_type &tbl = real_lcid_table();
try {
std::istringstream ss;
ss.str(s);
ss >> std::hex;
unsigned lcid ;
ss >>lcid;
if(ss.fail() || !ss.eof()) {
return FALSE;
}
char iso_639_lang[16];
char iso_3166_country[16];
if(GetLocaleInfoA(lcid,LOCALE_SISO639LANGNAME,iso_639_lang,sizeof(iso_639_lang))==0)
return FALSE;
std::string lc_name = iso_639_lang;
if(GetLocaleInfoA(lcid,LOCALE_SISO3166CTRYNAME,iso_3166_country,sizeof(iso_3166_country))!=0) {
lc_name += "_";
lc_name += iso_3166_country;
}
table_type::iterator p = tbl.find(lc_name);
if(p!=tbl.end()) {
if(p->second > lcid)
p->second = lcid;
}
else {
tbl[lc_name]=lcid;
}
}
catch(...) {
tbl.clear();
return FALSE;
}
return TRUE;
}
table_type const &get_ready_lcid_table()
{
if(table)
return *table;
else {
boost::unique_lock<boost::mutex> lock(lcid_table_mutex());
if(table)
return *table;
EnumSystemLocalesA(proc,LCID_INSTALLED);
table = &real_lcid_table();
return *table;
}
}
unsigned locale_to_lcid(std::string const &locale_name)
{
if(locale_name.empty()) {
return LOCALE_USER_DEFAULT;
}
boost::locale::util::locale_data d;
d.parse(locale_name);
std::string id = d.language;
if(!d.country.empty()) {
id+="_"+d.country;
}
if(!d.variant.empty()) {
id+="@" + d.variant;
}
table_type const &tbl = get_ready_lcid_table();
table_type::const_iterator p = tbl.find(id);
unsigned lcid = 0;
if(p!=tbl.end())
lcid = p->second;
return lcid;
}
} // impl_win
} // locale
} // boost
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
|