]> gcc.gnu.org Git - gcc.git/blame - libstdc++-v3/config/locale/generic/c_locale.cc
[multiple changes]
[gcc.git] / libstdc++-v3 / config / locale / generic / c_locale.cc
CommitLineData
0214010c
BK
1// Wrapper for underlying C-language localization -*- C++ -*-
2
3d838e28 3// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
0214010c
BK
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>
0214010c 37
3d7c150e 38#ifdef _GLIBCXX_HAVE_IEEEFP_H
99a4deb8
LR
39#include <ieeefp.h>
40#endif
41
0214010c
BK
42namespace std
43{
4b9aaf63
BK
44 // Specializations for all types used in num_get.
45 template<>
46 void
47 __convert_to_v(const char* __s, long& __v, ios_base::iostate& __err,
48 const __c_locale&, int __base)
49 {
50 if (!(__err & ios_base::failbit))
51 {
52 char* __sanity;
53 errno = 0;
54 long __l = strtol(__s, &__sanity, __base);
2083b5be 55 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
56 __v = __l;
57 else
58 __err |= ios_base::failbit;
59 }
60 }
61
62 template<>
63 void
64 __convert_to_v(const char* __s, unsigned long& __v,
65 ios_base::iostate& __err, const __c_locale&, int __base)
66 {
67 if (!(__err & ios_base::failbit))
68 {
69 char* __sanity;
70 errno = 0;
71 unsigned long __ul = strtoul(__s, &__sanity, __base);
2083b5be 72 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
73 __v = __ul;
74 else
75 __err |= ios_base::failbit;
76 }
77 }
78
3d7c150e 79#ifdef _GLIBCXX_USE_LONG_LONG
4b9aaf63
BK
80 template<>
81 void
82 __convert_to_v(const char* __s, long long& __v, ios_base::iostate& __err,
83 const __c_locale&, int __base)
84 {
85 if (!(__err & ios_base::failbit))
86 {
87 char* __sanity;
88 errno = 0;
89 long long __ll = strtoll(__s, &__sanity, __base);
2083b5be 90 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
91 __v = __ll;
92 else
93 __err |= ios_base::failbit;
94 }
95 }
96
97 template<>
98 void
99 __convert_to_v(const char* __s, unsigned long long& __v,
100 ios_base::iostate& __err, const __c_locale&, int __base)
101 {
102 if (!(__err & ios_base::failbit))
103 {
104 char* __sanity;
105 errno = 0;
106 unsigned long long __ull = strtoull(__s, &__sanity, __base);
2083b5be 107 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
108 __v = __ull;
109 else
110 __err |= ios_base::failbit;
111 }
112 }
113#endif
114
115 template<>
116 void
117 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
118 const __c_locale&, int)
119 {
120 if (!(__err & ios_base::failbit))
121 {
122 // Assumes __s formatted for "C" locale.
6d030676
PC
123 char* __old = strdup(setlocale(LC_ALL, NULL));
124 setlocale(LC_ALL, "C");
4b9aaf63
BK
125 char* __sanity;
126 errno = 0;
3d7c150e 127#if defined(_GLIBCXX_USE_C99)
4b9aaf63
BK
128 float __f = strtof(__s, &__sanity);
129#else
99a4deb8
LR
130 double __d = strtod(__s, &__sanity);
131 float __f = static_cast<float>(__d);
3d7c150e 132#ifdef _GLIBCXX_HAVE_FINITEF
99a4deb8
LR
133 if (!finitef (__f))
134 errno = ERANGE;
3d7c150e 135#elif defined (_GLIBCXX_HAVE_FINITE)
99a4deb8
LR
136 if (!finite (static_cast<double> (__f)))
137 errno = ERANGE;
3d7c150e 138#elif defined (_GLIBCXX_HAVE_ISINF)
99a4deb8
LR
139 if (isinf (static_cast<double> (__f)))
140 errno = ERANGE;
141#else
142 if (fabs(__d) > numeric_limits<float>::max())
143 errno = ERANGE;
144#endif
4b9aaf63 145#endif
2083b5be 146 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
147 __v = __f;
148 else
149 __err |= ios_base::failbit;
150 setlocale(LC_ALL, __old);
6d030676 151 free(__old);
4b9aaf63
BK
152 }
153 }
154
155 template<>
156 void
157 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
158 const __c_locale&, int)
159 {
160 if (!(__err & ios_base::failbit))
161 {
162 // Assumes __s formatted for "C" locale.
6d030676
PC
163 char* __old = strdup(setlocale(LC_ALL, NULL));
164 setlocale(LC_ALL, "C");
4b9aaf63
BK
165 char* __sanity;
166 errno = 0;
167 double __d = strtod(__s, &__sanity);
2083b5be 168 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
169 __v = __d;
170 else
171 __err |= ios_base::failbit;
172 setlocale(LC_ALL, __old);
6d030676 173 free(__old);
4b9aaf63
BK
174 }
175 }
176
177 template<>
178 void
179 __convert_to_v(const char* __s, long double& __v,
180 ios_base::iostate& __err, const __c_locale&, int)
181 {
182 if (!(__err & ios_base::failbit))
183 {
184 // Assumes __s formatted for "C" locale.
6d030676
PC
185 char* __old = strdup(setlocale(LC_ALL, NULL));
186 setlocale(LC_ALL, "C");
3d7c150e 187#if defined(_GLIBCXX_USE_C99)
4b9aaf63
BK
188 char* __sanity;
189 errno = 0;
190 long double __ld = strtold(__s, &__sanity);
2083b5be 191 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
4b9aaf63
BK
192 __v = __ld;
193#else
d419f1cd 194 typedef char_traits<char>::int_type int_type;
4b9aaf63 195 long double __ld;
99a4deb8 196 errno = 0;
4b9aaf63 197 int __p = sscanf(__s, "%Lf", &__ld);
99a4deb8
LR
198 if (errno == ERANGE)
199 __p = 0;
3d7c150e 200#ifdef _GLIBCXX_HAVE_FINITEL
99a4deb8
LR
201 if ((__p == 1) && !finitel (__ld))
202 __p = 0;
203#endif
9b6e0e57 204 if (__p && static_cast<int_type>(__p) != char_traits<char>::eof())
4b9aaf63
BK
205 __v = __ld;
206#endif
207 else
208 __err |= ios_base::failbit;
209 setlocale(LC_ALL, __old);
6d030676 210 free(__old);
4b9aaf63
BK
211 }
212 }
213
0214010c 214 void
85c39c6d 215 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
d3a193e3 216 __c_locale)
85c39c6d 217 {
988ad90d
BK
218 // Currently, the generic model only supports the "C" locale.
219 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
85c39c6d 220 __cloc = NULL;
3d838e28 221 if (strcmp(__s, "C"))
988ad90d
BK
222 __throw_runtime_error("locale::facet::_S_create_c_locale "
223 "name not valid");
85c39c6d 224 }
0214010c
BK
225
226 void
d3a193e3
BK
227 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
228 { __cloc = NULL; }
0214010c 229
33590f13
BK
230 __c_locale
231 locale::facet::_S_clone_c_locale(__c_locale&)
232 { return __c_locale(); }
73c4dcc6 233} // namespace std
aa53f832 234
73c4dcc6
BK
235namespace __gnu_cxx
236{
8ae81136 237 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
aa53f832
PC
238 {
239 "LC_CTYPE",
0e9501e6
PC
240 "LC_NUMERIC",
241 "LC_TIME",
aa53f832 242 "LC_COLLATE",
aa53f832
PC
243 "LC_MONETARY",
244 "LC_MESSAGES"
245 };
73c4dcc6
BK
246}
247
248namespace std
249{
8ae81136 250 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
0214010c 251} // namespace std
This page took 0.268223 seconds and 5 git commands to generate.