1 // Wrapper for underlying C-language localization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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)
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 <cerrno> // For errno
33 #include <cmath> // For isinf, finite, finitef, fabs
34 #include <cstdlib> // For strof, strtold
40 #ifdef _GLIBCXX_HAVE_IEEEFP_H
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
51 const __c_locale&) throw()
53 // Assumes __s formatted for "C" locale.
54 char* __old = setlocale(LC_ALL, 0);
55 const size_t __len = strlen(__old) + 1;
56 char* __sav = new char[__len];
57 memcpy(__sav, __old, __len);
58 setlocale(LC_ALL, "C");
60 bool __overflow = false;
62 #if !__FLT_HAS_INFINITY__
66 #ifdef _GLIBCXX_HAVE_STRTOF
67 __v = strtof(__s, &__sanity);
69 double __d = strtod(__s, &__sanity);
70 __v = static_cast<float>(__d);
71 #ifdef _GLIBCXX_HAVE_FINITEF
74 #elif defined (_GLIBCXX_HAVE_FINITE)
75 if (!finite (static_cast<double> (__v)))
77 #elif defined (_GLIBCXX_HAVE_ISINF)
78 if (isinf (static_cast<double> (__v)))
81 if (fabs(__d) > numeric_limits<float>::max())
84 #endif // _GLIBCXX_HAVE_STRTOF
86 // _GLIBCXX_RESOLVE_LIB_DEFECTS
87 // 23. Num_get overflow result.
88 if (__sanity == __s || *__sanity != '\0')
91 __err = ios_base::failbit;
94 #if __FLT_HAS_INFINITY__
95 || __v == numeric_limits<float>::infinity()
96 || __v == -numeric_limits<float>::infinity()
98 || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
103 __v = numeric_limits<float>::max();
105 __v = -numeric_limits<float>::max();
106 __err = ios_base::failbit;
109 setlocale(LC_ALL, __sav);
115 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
116 const __c_locale&) throw()
118 // Assumes __s formatted for "C" locale.
119 char* __old = setlocale(LC_ALL, 0);
120 const size_t __len = strlen(__old) + 1;
121 char* __sav = new char[__len];
122 memcpy(__sav, __old, __len);
123 setlocale(LC_ALL, "C");
126 #if !__DBL_HAS_INFINITY__
130 __v = strtod(__s, &__sanity);
132 // _GLIBCXX_RESOLVE_LIB_DEFECTS
133 // 23. Num_get overflow result.
134 if (__sanity == __s || *__sanity != '\0')
137 __err = ios_base::failbit;
140 #if __DBL_HAS_INFINITY__
141 __v == numeric_limits<double>::infinity()
142 || __v == -numeric_limits<double>::infinity())
144 (__v > 1.0 || __v < -1.0) && errno == ERANGE)
148 __v = numeric_limits<double>::max();
150 __v = -numeric_limits<double>::max();
151 __err = ios_base::failbit;
154 setlocale(LC_ALL, __sav);
160 __convert_to_v(const char* __s, long double& __v,
161 ios_base::iostate& __err, const __c_locale&) throw()
163 // Assumes __s formatted for "C" locale.
164 char* __old = setlocale(LC_ALL, 0);
165 const size_t __len = strlen(__old) + 1;
166 char* __sav = new char[__len];
167 memcpy(__sav, __old, __len);
168 setlocale(LC_ALL, "C");
170 #if !__LDBL_HAS_INFINITY__
174 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
176 __v = strtold(__s, &__sanity);
178 // _GLIBCXX_RESOLVE_LIB_DEFECTS
179 // 23. Num_get overflow result.
180 if (__sanity == __s || *__sanity != '\0')
182 typedef char_traits<char>::int_type int_type;
183 int __p = sscanf(__s, "%Lf", &__v);
185 if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
189 __err = ios_base::failbit;
192 #if __LDBL_HAS_INFINITY__
193 __v == numeric_limits<long double>::infinity()
194 || __v == -numeric_limits<long double>::infinity())
196 (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
200 __v = numeric_limits<long double>::max();
202 __v = -numeric_limits<long double>::max();
203 __err = ios_base::failbit;
206 setlocale(LC_ALL, __sav);
211 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
214 // Currently, the generic model only supports the "C" locale.
215 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
217 if (strcmp(__s, "C"))
218 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
223 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
227 locale::facet::_S_clone_c_locale(__c_locale&) throw()
228 { return __c_locale(); }
231 locale::facet::_S_lc_ctype_c_locale(__c_locale, const char*)
232 { return __c_locale(); }
234 _GLIBCXX_END_NAMESPACE_VERSION
237 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
239 _GLIBCXX_BEGIN_NAMESPACE_VERSION
241 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
251 _GLIBCXX_END_NAMESPACE_VERSION
254 namespace std _GLIBCXX_VISIBILITY(default)
256 _GLIBCXX_BEGIN_NAMESPACE_VERSION
258 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
260 _GLIBCXX_END_NAMESPACE_VERSION
263 // XXX GLIBCXX_ABI Deprecated
264 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
265 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
266 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
267 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
268 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT