Mostly work by Pedro to port libstdc++
[cegcc.git] / cegcc / src / gcc-4.4.0 / libstdc++-v3 / config / locale / generic / c_locale.cc
blob9dd403d1541fb2cfe93c4c4cd6db019f4904c949
1 // Wrapper for underlying C-language localization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3, or (at your option)
10 // any later version.
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.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
27 // ISO C++ 14882: 22.8 Standard locale categories.
30 // Written by Benjamin Kosnik <bkoz@redhat.com>
32 #include <bits/c++config.h>
34 #ifdef _GLIBCXX_HAVE_ERRNO_H
35 #include <cerrno> // For errno
36 #endif
38 #ifdef __MINGW32CE__
39 /* Provide a fake ERANGE to keep the
40 sources as clean as possible. */
41 # ifndef ERANGE
42 # define ERANGE 1
43 # endif
44 #endif
46 #include <cmath> // For isinf, finite, finitef, fabs
47 #include <cstdlib> // For strof, strtold
48 #include <cstring>
49 #include <cstdio>
50 #include <locale>
51 #include <limits>
52 #include <cstddef>
54 #ifdef _GLIBCXX_HAVE_IEEEFP_H
55 #include <ieeefp.h>
56 #endif
58 _GLIBCXX_BEGIN_NAMESPACE(std)
60 template<>
61 void
62 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
63 const __c_locale&)
65 // Assumes __s formatted for "C" locale.
66 #ifndef __MINGW32CE__
67 char* __old = setlocale(LC_ALL, NULL);
68 const size_t __len = strlen(__old) + 1;
69 char* __sav = new char[__len];
70 memcpy(__sav, __old, __len);
71 setlocale(LC_ALL, "C");
72 #endif
73 char* __sanity;
74 bool __overflow = false;
76 #if !__FLT_HAS_INFINITY__
77 errno = 0;
78 #endif
80 #ifdef _GLIBCXX_HAVE_STRTOF
81 __v = strtof(__s, &__sanity);
82 #else
83 double __d = strtod(__s, &__sanity);
84 __v = static_cast<float>(__d);
85 #ifdef _GLIBCXX_HAVE_FINITEF
86 if (!finitef (__v))
87 __overflow = true;
88 #elif defined (_GLIBCXX_HAVE_FINITE)
89 if (!finite (static_cast<double> (__v)))
90 __overflow = true;
91 #elif defined (_GLIBCXX_HAVE_ISINF)
92 if (isinf (static_cast<double> (__v)))
93 __overflow = true;
94 #else
95 if (fabs(__d) > numeric_limits<float>::max())
96 __overflow = true;
97 #endif
98 #endif // _GLIBCXX_HAVE_STRTOF
100 // _GLIBCXX_RESOLVE_LIB_DEFECTS
101 // 23. Num_get overflow result.
102 if (__sanity == __s || *__sanity != '\0')
104 __v = 0.0f;
105 __err = ios_base::failbit;
107 else if (__overflow
108 #if __FLT_HAS_INFINITY__
109 || __v == numeric_limits<float>::infinity()
110 || __v == -numeric_limits<float>::infinity()
111 #else
112 || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
113 #endif
116 if (__v > 0.0f)
117 __v = numeric_limits<float>::max();
118 else
119 __v = -numeric_limits<float>::max();
120 __err = ios_base::failbit;
122 #ifndef __MINGW32CE__
123 setlocale(LC_ALL, __sav);
124 delete [] __sav;
125 #endif
128 template<>
129 void
130 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
131 const __c_locale&)
133 // Assumes __s formatted for "C" locale.
134 #ifndef __MINGW32CE__
135 char* __old = setlocale(LC_ALL, NULL);
136 const size_t __len = strlen(__old) + 1;
137 char* __sav = new char[__len];
138 memcpy(__sav, __old, __len);
139 setlocale(LC_ALL, "C");
140 #endif
141 char* __sanity;
143 #if !__DBL_HAS_INFINITY__
144 errno = 0;
145 #endif
147 __v = strtod(__s, &__sanity);
149 // _GLIBCXX_RESOLVE_LIB_DEFECTS
150 // 23. Num_get overflow result.
151 if (__sanity == __s || *__sanity != '\0')
153 __v = 0.0;
154 __err = ios_base::failbit;
156 else if (
157 #if __DBL_HAS_INFINITY__
158 __v == numeric_limits<double>::infinity()
159 || __v == -numeric_limits<double>::infinity())
160 #else
161 (__v > 1.0 || __v < -1.0) && errno == ERANGE)
162 #endif
164 if (__v > 0.0)
165 __v = numeric_limits<double>::max();
166 else
167 __v = -numeric_limits<double>::max();
168 __err = ios_base::failbit;
171 #ifndef __MINGW32CE__
172 setlocale(LC_ALL, __sav);
173 delete [] __sav;
174 #endif
177 template<>
178 void
179 __convert_to_v(const char* __s, long double& __v,
180 ios_base::iostate& __err, const __c_locale&)
182 // Assumes __s formatted for "C" locale.
183 #ifndef __MINGW32CE__
184 char* __old = setlocale(LC_ALL, NULL);
185 const size_t __len = strlen(__old) + 1;
186 char* __sav = new char[__len];
187 memcpy(__sav, __old, __len);
188 setlocale(LC_ALL, "C");
189 #endif
191 #if !__LDBL_HAS_INFINITY__
192 errno = 0;
193 #endif
195 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
196 char* __sanity;
197 __v = strtold(__s, &__sanity);
199 // _GLIBCXX_RESOLVE_LIB_DEFECTS
200 // 23. Num_get overflow result.
201 if (__sanity == __s || *__sanity != '\0')
202 #else
203 typedef char_traits<char>::int_type int_type;
204 int __p = sscanf(__s, "%Lf", &__v);
206 if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
207 #endif
209 __v = 0.0l;
210 __err = ios_base::failbit;
212 else if (
213 #if __LDBL_HAS_INFINITY__
214 __v == numeric_limits<long double>::infinity()
215 || __v == -numeric_limits<long double>::infinity())
216 #else
217 (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
218 #endif
220 if (__v > 0.0l)
221 __v = numeric_limits<long double>::max();
222 else
223 __v = -numeric_limits<long double>::max();
224 __err = ios_base::failbit;
227 #ifndef __MINGW32CE__
228 setlocale(LC_ALL, __sav);
229 delete [] __sav;
230 #endif
233 void
234 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
235 __c_locale)
237 // Currently, the generic model only supports the "C" locale.
238 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
239 __cloc = NULL;
240 if (strcmp(__s, "C"))
241 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
242 "name not valid"));
245 void
246 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
247 { __cloc = NULL; }
249 __c_locale
250 locale::facet::_S_clone_c_locale(__c_locale&)
251 { return __c_locale(); }
253 _GLIBCXX_END_NAMESPACE
255 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
257 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
259 "LC_CTYPE",
260 "LC_NUMERIC",
261 "LC_TIME",
262 "LC_COLLATE",
263 "LC_MONETARY",
264 "LC_MESSAGES"
267 _GLIBCXX_END_NAMESPACE
269 _GLIBCXX_BEGIN_NAMESPACE(std)
271 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
273 _GLIBCXX_END_NAMESPACE
275 // XXX GLIBCXX_ABI Deprecated
276 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
277 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
278 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
279 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
280 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT