]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/gnu/numeric_members.cc
re PR libstdc++/38368 (locale(const char* std_name) may create locale with broken...
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / gnu / numeric_members.cc
1 // std::numpunct implementation details, GNU version -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 22.2.3.1.2 numpunct virtual functions
33 //
34
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
36
37 #include <locale>
38 #include <bits/c++locale_internal.h>
39
40 _GLIBCXX_BEGIN_NAMESPACE(std)
41
42 template<>
43 void
44 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc)
45 {
46 if (!_M_data)
47 _M_data = new __numpunct_cache<char>;
48
49 if (!__cloc)
50 {
51 // "C" locale
52 _M_data->_M_grouping = "";
53 _M_data->_M_grouping_size = 0;
54 _M_data->_M_use_grouping = false;
55
56 _M_data->_M_decimal_point = '.';
57 _M_data->_M_thousands_sep = ',';
58
59 for (size_t __i = 0; __i < __num_base::_S_oend; ++__i)
60 _M_data->_M_atoms_out[__i] = __num_base::_S_atoms_out[__i];
61
62 for (size_t __j = 0; __j < __num_base::_S_iend; ++__j)
63 _M_data->_M_atoms_in[__j] = __num_base::_S_atoms_in[__j];
64 }
65 else
66 {
67 // Named locale.
68 _M_data->_M_decimal_point = *(__nl_langinfo_l(DECIMAL_POINT,
69 __cloc));
70 _M_data->_M_thousands_sep = *(__nl_langinfo_l(THOUSANDS_SEP,
71 __cloc));
72
73 // Check for NULL, which implies no grouping.
74 if (_M_data->_M_thousands_sep == '\0')
75 {
76 // Like in "C" locale.
77 _M_data->_M_grouping = "";
78 _M_data->_M_grouping_size = 0;
79 _M_data->_M_use_grouping = false;
80 _M_data->_M_thousands_sep = ',';
81 }
82 else
83 {
84 _M_data->_M_grouping = __nl_langinfo_l(GROUPING, __cloc);
85 _M_data->_M_grouping_size = strlen(_M_data->_M_grouping);
86 }
87 }
88
89 // NB: There is no way to extact this info from posix locales.
90 // _M_truename = __nl_langinfo_l(YESSTR, __cloc);
91 _M_data->_M_truename = "true";
92 _M_data->_M_truename_size = 4;
93 // _M_falsename = __nl_langinfo_l(NOSTR, __cloc);
94 _M_data->_M_falsename = "false";
95 _M_data->_M_falsename_size = 5;
96 }
97
98 template<>
99 numpunct<char>::~numpunct()
100 { delete _M_data; }
101
102 #ifdef _GLIBCXX_USE_WCHAR_T
103 template<>
104 void
105 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc)
106 {
107 if (!_M_data)
108 _M_data = new __numpunct_cache<wchar_t>;
109
110 if (!__cloc)
111 {
112 // "C" locale
113 _M_data->_M_grouping = "";
114 _M_data->_M_grouping_size = 0;
115 _M_data->_M_use_grouping = false;
116
117 _M_data->_M_decimal_point = L'.';
118 _M_data->_M_thousands_sep = L',';
119
120 // Use ctype::widen code without the facet...
121 for (size_t __i = 0; __i < __num_base::_S_oend; ++__i)
122 _M_data->_M_atoms_out[__i] =
123 static_cast<wchar_t>(__num_base::_S_atoms_out[__i]);
124
125 for (size_t __j = 0; __j < __num_base::_S_iend; ++__j)
126 _M_data->_M_atoms_in[__j] =
127 static_cast<wchar_t>(__num_base::_S_atoms_in[__j]);
128 }
129 else
130 {
131 // Named locale.
132 // NB: In the GNU model wchar_t is always 32 bit wide.
133 union { char *__s; wchar_t __w; } __u;
134 __u.__s = __nl_langinfo_l(_NL_NUMERIC_DECIMAL_POINT_WC, __cloc);
135 _M_data->_M_decimal_point = __u.__w;
136
137 __u.__s = __nl_langinfo_l(_NL_NUMERIC_THOUSANDS_SEP_WC, __cloc);
138 _M_data->_M_thousands_sep = __u.__w;
139
140 // Check for NULL, which implies no grouping.
141 if (_M_data->_M_thousands_sep == L'\0')
142 {
143 // Like in "C" locale.
144 _M_data->_M_grouping = "";
145 _M_data->_M_grouping_size = 0;
146 _M_data->_M_use_grouping = false;
147 _M_data->_M_thousands_sep = L',';
148 }
149 else
150 {
151 _M_data->_M_grouping = __nl_langinfo_l(GROUPING, __cloc);
152 _M_data->_M_grouping_size = strlen(_M_data->_M_grouping);
153 }
154 }
155
156 // NB: There is no way to extact this info from posix locales.
157 // _M_truename = __nl_langinfo_l(YESSTR, __cloc);
158 _M_data->_M_truename = L"true";
159 _M_data->_M_truename_size = 4;
160 // _M_falsename = __nl_langinfo_l(NOSTR, __cloc);
161 _M_data->_M_falsename = L"false";
162 _M_data->_M_falsename_size = 5;
163 }
164
165 template<>
166 numpunct<wchar_t>::~numpunct()
167 { delete _M_data; }
168 #endif
169
170 _GLIBCXX_END_NAMESPACE