1 // Wrapper for underlying C-language localization -*- C++ -*-
3 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
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 3, or (at your option)
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882: 22.8 Standard locale categories.
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
31 #include <cerrno> // For errno
32 #include <cmath> // For isinf, finite, finitef, fabs
33 #include <cstdlib> // For strof, strtold
39 #ifdef _GLIBCXX_HAVE_IEEEFP_H
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
50 const __c_locale&) throw()
52 // Assumes __s formatted for "C" locale.
53 char* __old = setlocale(LC_ALL, 0);
54 const size_t __len = strlen(__old) + 1;
55 char* __sav = new char[__len];
56 memcpy(__sav, __old, __len);
57 setlocale(LC_ALL, "C");
59 bool __overflow = false;
61 #if !__FLT_HAS_INFINITY__
65 #ifdef _GLIBCXX_HAVE_STRTOF
66 __v = strtof(__s, &__sanity);
68 double __d = strtod(__s, &__sanity);
69 __v = static_cast<float>(__d);
70 #ifdef _GLIBCXX_HAVE_FINITEF
73 #elif defined (_GLIBCXX_HAVE_FINITE)
74 if (!finite (static_cast<double> (__v)))
76 #elif defined (_GLIBCXX_HAVE_ISINF)
77 if (isinf (static_cast<double> (__v)))
80 if (fabs(__d) > numeric_limits<float>::max())
83 #endif // _GLIBCXX_HAVE_STRTOF
85 // _GLIBCXX_RESOLVE_LIB_DEFECTS
86 // 23. Num_get overflow result.
87 if (__sanity == __s || *__sanity != '\0')
90 __err = ios_base::failbit;
93 #if __FLT_HAS_INFINITY__
94 || __v == numeric_limits<float>::infinity()
95 || __v == -numeric_limits<float>::infinity()
97 || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
102 __v = numeric_limits<float>::max();
104 __v = -numeric_limits<float>::max();
105 __err = ios_base::failbit;
108 setlocale(LC_ALL, __sav);
114 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
115 const __c_locale&) throw()
117 // Assumes __s formatted for "C" locale.
118 char* __old = setlocale(LC_ALL, 0);
119 const size_t __len = strlen(__old) + 1;
120 char* __sav = new char[__len];
121 memcpy(__sav, __old, __len);
122 setlocale(LC_ALL, "C");
125 #if !__DBL_HAS_INFINITY__
129 __v = strtod(__s, &__sanity);
131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
132 // 23. Num_get overflow result.
133 if (__sanity == __s || *__sanity != '\0')
136 __err = ios_base::failbit;
139 #if __DBL_HAS_INFINITY__
140 __v == numeric_limits<double>::infinity()
141 || __v == -numeric_limits<double>::infinity())
143 (__v > 1.0 || __v < -1.0) && errno == ERANGE)
147 __v = numeric_limits<double>::max();
149 __v = -numeric_limits<double>::max();
150 __err = ios_base::failbit;
153 setlocale(LC_ALL, __sav);
159 __convert_to_v(const char* __s, long double& __v,
160 ios_base::iostate& __err, const __c_locale&) throw()
162 // Assumes __s formatted for "C" locale.
163 char* __old = setlocale(LC_ALL, 0);
164 const size_t __len = strlen(__old) + 1;
165 char* __sav = new char[__len];
166 memcpy(__sav, __old, __len);
167 setlocale(LC_ALL, "C");
169 #if !__LDBL_HAS_INFINITY__
173 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
175 __v = strtold(__s, &__sanity);
177 // _GLIBCXX_RESOLVE_LIB_DEFECTS
178 // 23. Num_get overflow result.
179 if (__sanity == __s || *__sanity != '\0')
181 typedef char_traits<char>::int_type int_type;
182 int __p = sscanf(__s, "%Lf", &__v);
184 if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
188 __err = ios_base::failbit;
191 #if __LDBL_HAS_INFINITY__
192 __v == numeric_limits<long double>::infinity()
193 || __v == -numeric_limits<long double>::infinity())
195 (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
199 __v = numeric_limits<long double>::max();
201 __v = -numeric_limits<long double>::max();
202 __err = ios_base::failbit;
205 setlocale(LC_ALL, __sav);
210 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
213 // Currently, the generic model only supports the "C" locale.
214 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
216 if (strcmp(__s, "C"))
217 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
222 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
226 locale::facet::_S_clone_c_locale(__c_locale&) throw()
227 { return __c_locale(); }
230 locale::facet::_S_lc_ctype_c_locale(__c_locale, const char*)
231 { return __c_locale(); }
233 _GLIBCXX_END_NAMESPACE_VERSION
236 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
238 _GLIBCXX_BEGIN_NAMESPACE_VERSION
240 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
250 _GLIBCXX_END_NAMESPACE_VERSION
253 namespace std _GLIBCXX_VISIBILITY(default)
255 _GLIBCXX_BEGIN_NAMESPACE_VERSION
257 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
259 _GLIBCXX_END_NAMESPACE_VERSION
262 // XXX GLIBCXX_ABI Deprecated
263 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
264 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
265 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
266 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
267 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT