]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/config/locale/gnu/c_locale.cc
4dd50d4f376ba5d77971986a01ccd3a0bccaf741
[gcc.git] / libstdc++-v3 / config / locale / gnu / c_locale.cc
1 // Wrapper for underlying C-language localization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 22.8 Standard locale categories.
32 //
33
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37 #include <stdexcept>
38 #include <langinfo.h>
39 #include <bits/c++locale_internal.h>
40
41 namespace std
42 {
43 template<>
44 void
45 __convert_to_v(const char* __s, long& __v, ios_base::iostate& __err,
46 const __c_locale& __cloc, int __base)
47 {
48 if (!(__err & ios_base::failbit))
49 {
50 char* __sanity;
51 errno = 0;
52 long __l = __strtol_l(__s, &__sanity, __base, __cloc);
53 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
54 __v = __l;
55 else
56 __err |= ios_base::failbit;
57 }
58 }
59
60 template<>
61 void
62 __convert_to_v(const char* __s, unsigned long& __v,
63 ios_base::iostate& __err, const __c_locale& __cloc,
64 int __base)
65 {
66 if (!(__err & ios_base::failbit))
67 {
68 char* __sanity;
69 errno = 0;
70 unsigned long __ul = __strtoul_l(__s, &__sanity, __base, __cloc);
71 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
72 __v = __ul;
73 else
74 __err |= ios_base::failbit;
75 }
76 }
77
78 #ifdef _GLIBCPP_USE_LONG_LONG
79 template<>
80 void
81 __convert_to_v(const char* __s, long long& __v, ios_base::iostate& __err,
82 const __c_locale& __cloc, int __base)
83 {
84 if (!(__err & ios_base::failbit))
85 {
86 char* __sanity;
87 errno = 0;
88 long long __ll = __strtoll_l(__s, &__sanity, __base, __cloc);
89 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
90 __v = __ll;
91 else
92 __err |= ios_base::failbit;
93 }
94 }
95
96 template<>
97 void
98 __convert_to_v(const char* __s, unsigned long long& __v,
99 ios_base::iostate& __err, const __c_locale& __cloc,
100 int __base)
101 {
102 if (!(__err & ios_base::failbit))
103 {
104 char* __sanity;
105 errno = 0;
106 unsigned long long __ull = __strtoull_l(__s, &__sanity, __base,
107 __cloc);
108 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
109 __v = __ull;
110 else
111 __err |= ios_base::failbit;
112 }
113 }
114 #endif
115
116 template<>
117 void
118 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
119 const __c_locale& __cloc, int)
120 {
121 if (!(__err & ios_base::failbit))
122 {
123 char* __sanity;
124 errno = 0;
125 float __f = __strtof_l(__s, &__sanity, __cloc);
126 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
127 __v = __f;
128 else
129 __err |= ios_base::failbit;
130 }
131 }
132
133 template<>
134 void
135 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
136 const __c_locale& __cloc, int)
137 {
138 if (!(__err & ios_base::failbit))
139 {
140 char* __sanity;
141 errno = 0;
142 double __d = __strtod_l(__s, &__sanity, __cloc);
143 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
144 __v = __d;
145 else
146 __err |= ios_base::failbit;
147 }
148 }
149
150 template<>
151 void
152 __convert_to_v(const char* __s, long double& __v, ios_base::iostate& __err,
153 const __c_locale& __cloc, int)
154 {
155 if (!(__err & ios_base::failbit))
156 {
157 char* __sanity;
158 errno = 0;
159 long double __ld = __strtold_l(__s, &__sanity, __cloc);
160 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
161 __v = __ld;
162 else
163 __err |= ios_base::failbit;
164 }
165 }
166
167 void
168 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
169 __c_locale __old)
170 {
171 __cloc = __newlocale(1 << LC_ALL, __s, __old);
172 if (!__cloc)
173 {
174 // This named locale is not supported by the underlying OS.
175 __throw_runtime_error("locale::facet::_S_create_c_locale "
176 "name not valid");
177 }
178 }
179
180 void
181 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
182 {
183 if (_S_c_locale != __cloc)
184 __freelocale(__cloc);
185 }
186
187 __c_locale
188 locale::facet::_S_clone_c_locale(__c_locale& __cloc)
189 { return __duplocale(__cloc); }
190 } // namespace std
191
192 namespace __gnu_cxx
193 {
194 const char* category_names[6 + _GLIBCPP_NUM_CATEGORIES] =
195 {
196 "LC_CTYPE",
197 "LC_NUMERIC",
198 "LC_TIME",
199 "LC_COLLATE",
200 "LC_MONETARY",
201 "LC_MESSAGES",
202 "LC_PAPER",
203 "LC_NAME",
204 "LC_ADDRESS",
205 "LC_TELEPHONE",
206 "LC_MEASUREMENT",
207 "LC_IDENTIFICATION"
208 };
209 }
210
211 namespace std
212 {
213 const char** locale::_S_categories = __gnu_cxx::category_names;
214 } // namespace std
This page took 0.04473 seconds and 4 git commands to generate.