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.
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 <bits/c++config.h>
34 #ifdef _GLIBCXX_HAVE_ERRNO_H
35 #include <cerrno> // For errno
39 /* Provide a fake ERANGE to keep the
40 sources as clean as possible. */
46 #include <cmath> // For isinf, finite, finitef, fabs
47 #include <cstdlib> // For strof, strtold
54 #ifdef _GLIBCXX_HAVE_IEEEFP_H
58 _GLIBCXX_BEGIN_NAMESPACE(std
)
62 __convert_to_v(const char* __s
, float& __v
, ios_base::iostate
& __err
,
65 // Assumes __s formatted for "C" locale.
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");
74 bool __overflow
= false;
76 #if !__FLT_HAS_INFINITY__
80 #ifdef _GLIBCXX_HAVE_STRTOF
81 __v
= strtof(__s
, &__sanity
);
83 double __d
= strtod(__s
, &__sanity
);
84 __v
= static_cast<float>(__d
);
85 #ifdef _GLIBCXX_HAVE_FINITEF
88 #elif defined (_GLIBCXX_HAVE_FINITE)
89 if (!finite (static_cast<double> (__v
)))
91 #elif defined (_GLIBCXX_HAVE_ISINF)
92 if (isinf (static_cast<double> (__v
)))
95 if (fabs(__d
) > numeric_limits
<float>::max())
98 #endif // _GLIBCXX_HAVE_STRTOF
100 // _GLIBCXX_RESOLVE_LIB_DEFECTS
101 // 23. Num_get overflow result.
102 if (__sanity
== __s
|| *__sanity
!= '\0')
105 __err
= ios_base::failbit
;
108 #if __FLT_HAS_INFINITY__
109 || __v
== numeric_limits
<float>::infinity()
110 || __v
== -numeric_limits
<float>::infinity()
112 || ((__v
> 1.0f
|| __v
< -1.0f
) && errno
== ERANGE
)
117 __v
= numeric_limits
<float>::max();
119 __v
= -numeric_limits
<float>::max();
120 __err
= ios_base::failbit
;
122 #ifndef __MINGW32CE__
123 setlocale(LC_ALL
, __sav
);
130 __convert_to_v(const char* __s
, double& __v
, ios_base::iostate
& __err
,
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");
143 #if !__DBL_HAS_INFINITY__
147 __v
= strtod(__s
, &__sanity
);
149 // _GLIBCXX_RESOLVE_LIB_DEFECTS
150 // 23. Num_get overflow result.
151 if (__sanity
== __s
|| *__sanity
!= '\0')
154 __err
= ios_base::failbit
;
157 #if __DBL_HAS_INFINITY__
158 __v
== numeric_limits
<double>::infinity()
159 || __v
== -numeric_limits
<double>::infinity())
161 (__v
> 1.0 || __v
< -1.0) && errno
== ERANGE
)
165 __v
= numeric_limits
<double>::max();
167 __v
= -numeric_limits
<double>::max();
168 __err
= ios_base::failbit
;
171 #ifndef __MINGW32CE__
172 setlocale(LC_ALL
, __sav
);
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");
191 #if !__LDBL_HAS_INFINITY__
195 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
197 __v
= strtold(__s
, &__sanity
);
199 // _GLIBCXX_RESOLVE_LIB_DEFECTS
200 // 23. Num_get overflow result.
201 if (__sanity
== __s
|| *__sanity
!= '\0')
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())
210 __err
= ios_base::failbit
;
213 #if __LDBL_HAS_INFINITY__
214 __v
== numeric_limits
<long double>::infinity()
215 || __v
== -numeric_limits
<long double>::infinity())
217 (__v
> 1.0l || __v
< -1.0l) && errno
== ERANGE
)
221 __v
= numeric_limits
<long double>::max();
223 __v
= -numeric_limits
<long double>::max();
224 __err
= ios_base::failbit
;
227 #ifndef __MINGW32CE__
228 setlocale(LC_ALL
, __sav
);
234 locale::facet::_S_create_c_locale(__c_locale
& __cloc
, const char* __s
,
237 // Currently, the generic model only supports the "C" locale.
238 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
240 if (strcmp(__s
, "C"))
241 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
246 locale::facet::_S_destroy_c_locale(__c_locale
& __cloc
)
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
] =
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