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 /* DragonFly's implementation of setlocale won't accept something like
212 "de_DE". According to nls manpage, the expected format is:
213 language[_territory][.codeset][@modifier], but it seems that both
214 the _territory and .codeset components are required.
216 As an attempt to correct for this, we'll tack on ".UTF-8" if
217 a period is not detected in the locale string.
219 There are no locales with modifiers on DragonFly so if found, they
220 will just be stripped off silently. e.g "de_DE@euro" will be reduced
221 to "de_DE". The UTF-8 default would be added after that.
225 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
228 const size_t size__s = (__s == NULL) ? 1 : strlen (__s);
229 const char UTF8[] = ".UTF-8";
230 char localspec[size__s + 6 + 1];
235 strcpy (localspec, __s);
236 char * pch = strchr (localspec, '@');
240 if ( (strchr (__s, '.') == NULL)
241 && (strcmp (__s, "C") != 0)
242 && (strcmp (__s, "POSIX") != 0))
243 strncat (localspec, UTF8, 6);
246 const char * result = std::setlocale(LC_ALL, localspec);
248 if ((strcmp(result, "C") != 0) && (strcmp (result, localspec) != 0))
249 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
255 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
259 locale::facet::_S_clone_c_locale(__c_locale&) throw()
260 { return __c_locale(); }
263 locale::facet::_S_lc_ctype_c_locale(__c_locale, const char*)
264 { return __c_locale(); }
266 _GLIBCXX_END_NAMESPACE_VERSION
269 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
271 _GLIBCXX_BEGIN_NAMESPACE_VERSION
273 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
283 _GLIBCXX_END_NAMESPACE_VERSION
286 namespace std _GLIBCXX_VISIBILITY(default)
288 _GLIBCXX_BEGIN_NAMESPACE_VERSION
290 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
292 _GLIBCXX_END_NAMESPACE_VERSION
295 // XXX GLIBCXX_ABI Deprecated
296 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
297 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
298 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
299 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
300 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT