Merge from mainline (160224:163495).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / config / locale / generic / c_locale.cc
blob468816092d71612f0265a3a48d8b22f6f0347e3e
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.
5 //
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)
10 // any later version.
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
35 #include <cstring>
36 #include <cstdio>
37 #include <locale>
38 #include <limits>
40 #ifdef _GLIBCXX_HAVE_IEEEFP_H
41 #include <ieeefp.h>
42 #endif
44 _GLIBCXX_BEGIN_NAMESPACE(std)
46 template<>
47 void
48 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
49 const __c_locale&) throw()
51 // Assumes __s formatted for "C" locale.
52 char* __old = setlocale(LC_ALL, 0);
53 const size_t __len = strlen(__old) + 1;
54 char* __sav = new char[__len];
55 memcpy(__sav, __old, __len);
56 setlocale(LC_ALL, "C");
57 char* __sanity;
58 bool __overflow = false;
60 #if !__FLT_HAS_INFINITY__
61 errno = 0;
62 #endif
64 #ifdef _GLIBCXX_HAVE_STRTOF
65 __v = strtof(__s, &__sanity);
66 #else
67 double __d = strtod(__s, &__sanity);
68 __v = static_cast<float>(__d);
69 #ifdef _GLIBCXX_HAVE_FINITEF
70 if (!finitef (__v))
71 __overflow = true;
72 #elif defined (_GLIBCXX_HAVE_FINITE)
73 if (!finite (static_cast<double> (__v)))
74 __overflow = true;
75 #elif defined (_GLIBCXX_HAVE_ISINF)
76 if (isinf (static_cast<double> (__v)))
77 __overflow = true;
78 #else
79 if (fabs(__d) > numeric_limits<float>::max())
80 __overflow = true;
81 #endif
82 #endif // _GLIBCXX_HAVE_STRTOF
84 // _GLIBCXX_RESOLVE_LIB_DEFECTS
85 // 23. Num_get overflow result.
86 if (__sanity == __s || *__sanity != '\0')
88 __v = 0.0f;
89 __err = ios_base::failbit;
91 else if (__overflow
92 #if __FLT_HAS_INFINITY__
93 || __v == numeric_limits<float>::infinity()
94 || __v == -numeric_limits<float>::infinity()
95 #else
96 || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
97 #endif
100 if (__v > 0.0f)
101 __v = numeric_limits<float>::max();
102 else
103 __v = -numeric_limits<float>::max();
104 __err = ios_base::failbit;
107 setlocale(LC_ALL, __sav);
108 delete [] __sav;
111 template<>
112 void
113 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
114 const __c_locale&) throw()
116 // Assumes __s formatted for "C" locale.
117 char* __old = setlocale(LC_ALL, 0);
118 const size_t __len = strlen(__old) + 1;
119 char* __sav = new char[__len];
120 memcpy(__sav, __old, __len);
121 setlocale(LC_ALL, "C");
122 char* __sanity;
124 #if !__DBL_HAS_INFINITY__
125 errno = 0;
126 #endif
128 __v = strtod(__s, &__sanity);
130 // _GLIBCXX_RESOLVE_LIB_DEFECTS
131 // 23. Num_get overflow result.
132 if (__sanity == __s || *__sanity != '\0')
134 __v = 0.0;
135 __err = ios_base::failbit;
137 else if (
138 #if __DBL_HAS_INFINITY__
139 __v == numeric_limits<double>::infinity()
140 || __v == -numeric_limits<double>::infinity())
141 #else
142 (__v > 1.0 || __v < -1.0) && errno == ERANGE)
143 #endif
145 if (__v > 0.0)
146 __v = numeric_limits<double>::max();
147 else
148 __v = -numeric_limits<double>::max();
149 __err = ios_base::failbit;
152 setlocale(LC_ALL, __sav);
153 delete [] __sav;
156 template<>
157 void
158 __convert_to_v(const char* __s, long double& __v,
159 ios_base::iostate& __err, const __c_locale&) throw()
161 // Assumes __s formatted for "C" locale.
162 char* __old = setlocale(LC_ALL, 0);
163 const size_t __len = strlen(__old) + 1;
164 char* __sav = new char[__len];
165 memcpy(__sav, __old, __len);
166 setlocale(LC_ALL, "C");
168 #if !__LDBL_HAS_INFINITY__
169 errno = 0;
170 #endif
172 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
173 char* __sanity;
174 __v = strtold(__s, &__sanity);
176 // _GLIBCXX_RESOLVE_LIB_DEFECTS
177 // 23. Num_get overflow result.
178 if (__sanity == __s || *__sanity != '\0')
179 #else
180 typedef char_traits<char>::int_type int_type;
181 int __p = sscanf(__s, "%Lf", &__v);
183 if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
184 #endif
186 __v = 0.0l;
187 __err = ios_base::failbit;
189 else if (
190 #if __LDBL_HAS_INFINITY__
191 __v == numeric_limits<long double>::infinity()
192 || __v == -numeric_limits<long double>::infinity())
193 #else
194 (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
195 #endif
197 if (__v > 0.0l)
198 __v = numeric_limits<long double>::max();
199 else
200 __v = -numeric_limits<long double>::max();
201 __err = ios_base::failbit;
204 setlocale(LC_ALL, __sav);
205 delete [] __sav;
208 void
209 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
210 __c_locale)
212 // Currently, the generic model only supports the "C" locale.
213 // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
214 __cloc = 0;
215 if (strcmp(__s, "C"))
216 __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
217 "name not valid"));
220 void
221 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
222 { __cloc = 0; }
224 __c_locale
225 locale::facet::_S_clone_c_locale(__c_locale&) throw()
226 { return __c_locale(); }
228 __c_locale
229 locale::facet::_S_lc_ctype_c_locale(__c_locale, const char*)
230 { return __c_locale(); }
232 _GLIBCXX_END_NAMESPACE
234 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
236 const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
238 "LC_CTYPE",
239 "LC_NUMERIC",
240 "LC_TIME",
241 "LC_COLLATE",
242 "LC_MONETARY",
243 "LC_MESSAGES"
246 _GLIBCXX_END_NAMESPACE
248 _GLIBCXX_BEGIN_NAMESPACE(std)
250 const char* const* const locale::_S_categories = __gnu_cxx::category_names;
252 _GLIBCXX_END_NAMESPACE
254 // XXX GLIBCXX_ABI Deprecated
255 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
256 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
257 extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
258 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
259 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT