OSDN Git Service

PR libstdc++/31836
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / generic / c_locale.cc
1 // Wrapper for underlying C-language localization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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 2, or (at your option)
10 // any later version.
11
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.
16
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,
20 // USA.
21
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.
30
31 //
32 // ISO C++ 14882: 22.8  Standard locale categories.
33 //
34
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
36
37 #include <cerrno>  // For errno
38 #include <cmath>  // For isinf, finite, finitef, fabs
39 #include <cstdlib>  // For strof, strtold
40 #include <cstring>
41 #include <locale>
42 #include <limits>
43 #include <cstddef>
44
45 #ifdef _GLIBCXX_HAVE_IEEEFP_H
46 #include <ieeefp.h>
47 #endif
48
49 _GLIBCXX_BEGIN_NAMESPACE(std)
50
51   // Specializations for all types used in num_get.
52   template<>
53     void
54     __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err, 
55                    const __c_locale&)         
56     {
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");
63       char* __sanity;
64
65 #if !__FLT_HAS_INFINITY__
66       errno = 0;
67 #endif
68
69 #if defined(_GLIBCXX_HAVE_STRTOF)
70       float __f = strtof(__s, &__sanity);
71 #else
72       double __d = strtod(__s, &__sanity);
73       float __f = static_cast<float>(__d);
74 #ifdef _GLIBCXX_HAVE_FINITEF
75       if (!finitef (__f))
76         __s = __sanity;
77 #elif defined (_GLIBCXX_HAVE_FINITE)
78       if (!finite (static_cast<double> (__f)))
79         __s = __sanity;
80 #elif defined (_GLIBCXX_HAVE_ISINF)
81       if (isinf (static_cast<double> (__f)))
82         __s = __sanity;
83 #else
84       if (fabs(__d) > numeric_limits<float>::max())
85         __s = __sanity;
86 #endif
87 #endif
88
89       if (__sanity != __s
90 #if !__FLT_HAS_INFINITY__
91           && errno != ERANGE)
92 #else
93           && __f != __builtin_huge_valf() && __f != -__builtin_huge_valf())
94 #endif
95         __v = __f;
96       else
97         __err |= ios_base::failbit;
98
99       setlocale(LC_ALL, __sav);
100       delete [] __sav;
101     }
102
103   template<>
104     void
105     __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err, 
106                    const __c_locale&) 
107     {
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");
114       char* __sanity;
115
116 #if !__DBL_HAS_INFINITY__
117       errno = 0;
118 #endif
119
120       double __d = strtod(__s, &__sanity);
121
122       if (__sanity != __s
123 #if !__DBL_HAS_INFINITY__
124           && errno != ERANGE) 
125 #else
126           && __d != __builtin_huge_val() && __d != -__builtin_huge_val())
127 #endif
128         __v = __d;
129       else
130         __err |= ios_base::failbit;
131
132       setlocale(LC_ALL, __sav);
133       delete [] __sav;
134     }
135
136   template<>
137     void
138     __convert_to_v(const char* __s, long double& __v, 
139                    ios_base::iostate& __err, const __c_locale&) 
140     {
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");
147
148 #if !__LDBL_HAS_INFINITY__
149       errno = 0;
150 #endif
151
152 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
153       char* __sanity;
154       long double __ld = strtold(__s, &__sanity);
155
156       if (__sanity != __s
157 #if !__LDBL_HAS_INFINITY__
158           && errno != ERANGE)
159 #else
160           && __ld != __builtin_huge_vall() && __ld != -__builtin_huge_vall())
161 #endif
162         __v = __ld;
163
164 #else
165       typedef char_traits<char>::int_type int_type;
166       long double __ld;
167       int __p = sscanf(__s, "%Lf", &__ld);
168
169       if (__p && static_cast<int_type>(__p) != char_traits<char>::eof()
170 #if !__LDBL_HAS_INFINITY__
171           && errno != ERANGE)
172 #else
173           && __ld != __builtin_huge_vall() && __ld != -__builtin_huge_vall())
174 #endif
175         __v = __ld;
176
177 #endif
178       else
179         __err |= ios_base::failbit;
180
181       setlocale(LC_ALL, __sav);
182       delete [] __sav;
183     }
184
185   void
186   locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s, 
187                                     __c_locale)
188   {
189     // Currently, the generic model only supports the "C" locale.
190     // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
191     __cloc = NULL;
192     if (strcmp(__s, "C"))
193       __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
194                             "name not valid"));
195   }
196
197   void
198   locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
199   { __cloc = NULL; }
200
201   __c_locale
202   locale::facet::_S_clone_c_locale(__c_locale&)
203   { return __c_locale(); }
204
205 _GLIBCXX_END_NAMESPACE
206
207 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
208
209   const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
210     {
211       "LC_CTYPE", 
212       "LC_NUMERIC",
213       "LC_TIME",   
214       "LC_COLLATE", 
215       "LC_MONETARY",
216       "LC_MESSAGES"
217     };
218
219 _GLIBCXX_END_NAMESPACE
220
221 _GLIBCXX_BEGIN_NAMESPACE(std)
222
223   const char* const* const locale::_S_categories = __gnu_cxx::category_names;
224
225 _GLIBCXX_END_NAMESPACE
226
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