1 // Wrapper for underlying C-language localization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 22.8 Standard locale categories.
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
37 #include <cerrno> // For errno
38 #include <cmath> // For isinf, finite, finitef, fabs
39 #include <cstdlib> // For strof, strtold
45 #ifdef _GLIBCXX_HAVE_IEEEFP_H
49 _GLIBCXX_BEGIN_NAMESPACE(std)
51 // Specializations for all types used in num_get.
54 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
57 // Assumes __s formatted for "C" locale.
58 char* __old = setlocale(LC_ALL, NULL);
59 const size_t __len = strlen(__old) + 1;
60 char* __sav = new char[__len];
61 memcpy(__sav, __old, __len);
62 setlocale(LC_ALL, "C");
65 #if !__FLT_HAS_INFINITY__
69 #if defined(_GLIBCXX_HAVE_STRTOF)
70 float __f = strtof(__s, &__sanity);
72 double __d = strtod(__s, &__sanity);
73 float __f = static_cast<float>(__d);
74 #ifdef _GLIBCXX_HAVE_FINITEF
77 #elif defined (_GLIBCXX_HAVE_FINITE)
78 if (!finite (static_cast<double> (__f)))
80 #elif defined (_GLIBCXX_HAVE_ISINF)
81 if (isinf (static_cast<double> (__f)))
84 if (fabs(__d) > numeric_limits<float>::max())
90 #if !__FLT_HAS_INFINITY__
93 && __f != __builtin_huge_valf() && __f != -__builtin_huge_valf())
97 __err |= ios_base::failbit;
99 setlocale(LC_ALL, __sav);
105 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
108 // Assumes __s formatted for "C" locale.
109 char* __old = setlocale(LC_ALL, NULL);
110 const size_t __len = strlen(__old) + 1;
111 char* __sav = new char[__len];
112 memcpy(__sav, __old, __len);
113 setlocale(LC_ALL, "C");
116 #if !__DBL_HAS_INFINITY__
120 double __d = strtod(__s, &__sanity);
123 #if !__DBL_HAS_INFINITY__
126 && __d != __builtin_huge_val() && __d != -__builtin_huge_val())
130 __err |= ios_base::failbit;
132 setlocale(LC_ALL, __sav);
138 __convert_to_v(const char* __s, long double& __v,
139 ios_base::iostate& __err, const __c_locale&)
141 // Assumes __s formatted for "C" locale.
142 char* __old = setlocale(LC_ALL, NULL);
143 const size_t __len = strlen(__old) + 1;
144 char* __sav = new char[__len];
145 memcpy(__sav, __old, __len);
146 setlocale(LC_ALL, "C");
148 #if !__LDBL_HAS_INFINITY__
152 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
154 long double __ld = strtold(__s, &__sanity);
157 #if !__LDBL_HAS_INFINITY__
160 && __ld != __builtin_huge_vall() && __ld != -__builtin_huge_vall())
165 typedef char_traits<char>::int_type int_type;
167 int __p = sscanf(__s, "%Lf", &__ld);
169 if (__p && static_cast<int_type>(__p) != char_traits<char>::eof()
170 #if !__LDBL_HAS_INFINITY__
173 && __ld != __builtin_huge_vall() && __ld != -__builtin_huge_vall())
179 __err |= ios_base::failbit;
181 setlocale(LC_ALL, __sav);
186 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
189 // Currently, the generic model only supports the "C" locale.
190 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
192 if (strcmp(__s, "C"))
193 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
198 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
202 locale::facet::_S_clone_c_locale(__c_locale&)
203 { return __c_locale(); }
205 _GLIBCXX_END_NAMESPACE
207 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
209 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
219 _GLIBCXX_END_NAMESPACE
221 _GLIBCXX_BEGIN_NAMESPACE(std)
223 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
225 _GLIBCXX_END_NAMESPACE
227 // XXX GLIBCXX_ABI Deprecated
228 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
229 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
230 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
231 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
232 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT