OSDN Git Service

2008-09-23 Paolo Carlini <paolo.carlini@oracle.com>
[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, 2008
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 <cstdio>
42 #include <locale>
43 #include <limits>
44 #include <cstddef>
45
46 #ifdef _GLIBCXX_HAVE_IEEEFP_H
47 #include <ieeefp.h>
48 #endif
49
50 _GLIBCXX_BEGIN_NAMESPACE(std)
51
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       bool __overflow = false;
65
66 #if !__FLT_HAS_INFINITY__
67       errno = 0;
68 #endif
69
70 #ifdef _GLIBCXX_HAVE_STRTOF
71       __v = strtof(__s, &__sanity);
72 #else
73       double __d = strtod(__s, &__sanity);
74       __v = static_cast<float>(__d);
75 #ifdef _GLIBCXX_HAVE_FINITEF
76       if (!finitef (__v))
77         __overflow = true;
78 #elif defined (_GLIBCXX_HAVE_FINITE)
79       if (!finite (static_cast<double> (__v)))
80         __overflow = true;
81 #elif defined (_GLIBCXX_HAVE_ISINF)
82       if (isinf (static_cast<double> (__v)))
83         __overflow = true;
84 #else
85       if (fabs(__d) > numeric_limits<float>::max())
86         __overflow = true;
87 #endif
88 #endif // _GLIBCXX_HAVE_STRTOF
89
90       // _GLIBCXX_RESOLVE_LIB_DEFECTS
91       // 23. Num_get overflow result.
92       if (__sanity == __s || *__sanity != '\0')
93         {
94           __v = 0.0f;
95           __err = ios_base::failbit;
96         }
97       else if (__overflow
98 #if __FLT_HAS_INFINITY__
99                || __v == numeric_limits<float>::infinity()
100                || __v == -numeric_limits<float>::infinity())
101 #else
102                || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
103 #endif
104         {
105           if (__v > 0.0f)
106             __v = numeric_limits<float>::max();
107           else
108             __v = -numeric_limits<float>::max();
109           __err = ios_base::failbit;
110         }
111
112       setlocale(LC_ALL, __sav);
113       delete [] __sav;
114     }
115
116   template<>
117     void
118     __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err, 
119                    const __c_locale&) 
120     {
121       // Assumes __s formatted for "C" locale.
122       char* __old = setlocale(LC_ALL, NULL);
123       const size_t __len = strlen(__old) + 1;
124       char* __sav = new char[__len];
125       memcpy(__sav, __old, __len);
126       setlocale(LC_ALL, "C");
127       char* __sanity;
128
129 #if !__DBL_HAS_INFINITY__
130       errno = 0;
131 #endif
132
133       __v = strtod(__s, &__sanity);
134
135       // _GLIBCXX_RESOLVE_LIB_DEFECTS
136       // 23. Num_get overflow result.
137       if (__sanity == __s || *__sanity != '\0')
138         {
139           __v = 0.0;
140           __err = ios_base::failbit;
141         }
142       else if (
143 #if __DBL_HAS_INFINITY__
144                __v == numeric_limits<double>::infinity()
145                || __v == -numeric_limits<double>::infinity())
146 #else          
147                (__v > 1.0 || __v < -1.0) && errno == ERANGE)
148 #endif
149         {
150           if (__v > 0.0)
151             __v = numeric_limits<double>::max();
152           else
153             __v = -numeric_limits<double>::max();
154           __err = ios_base::failbit;
155         }
156
157       setlocale(LC_ALL, __sav);
158       delete [] __sav;
159     }
160
161   template<>
162     void
163     __convert_to_v(const char* __s, long double& __v, 
164                    ios_base::iostate& __err, const __c_locale&) 
165     {
166       // Assumes __s formatted for "C" locale.
167       char* __old = setlocale(LC_ALL, NULL);
168       const size_t __len = strlen(__old) + 1;
169       char* __sav = new char[__len];
170       memcpy(__sav, __old, __len);
171       setlocale(LC_ALL, "C");
172
173 #if !__LDBL_HAS_INFINITY__
174       errno = 0;
175 #endif
176
177 #if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
178       char* __sanity;
179       __v = strtold(__s, &__sanity);
180
181       // _GLIBCXX_RESOLVE_LIB_DEFECTS
182       // 23. Num_get overflow result.
183       if (__sanity == __s || *__sanity != '\0')
184 #else
185       typedef char_traits<char>::int_type int_type;
186       int __p = sscanf(__s, "%Lf", &__v);
187
188       if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
189 #endif
190         {
191           __v = 0.0l;
192           __err = ios_base::failbit;
193         }
194        else if (
195 #if __LDBL_HAS_INFINITY__
196                 __v == numeric_limits<long double>::infinity()
197                 || __v == -numeric_limits<long double>::infinity())
198 #else
199                 (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
200 #endif
201         {
202           if (__v > 0.0l)
203             __v = numeric_limits<long double>::max();
204           else
205             __v = -numeric_limits<long double>::max();
206           __err = ios_base::failbit;
207         }
208
209       setlocale(LC_ALL, __sav);
210       delete [] __sav;
211     }
212
213   void
214   locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s, 
215                                     __c_locale)
216   {
217     // Currently, the generic model only supports the "C" locale.
218     // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
219     __cloc = NULL;
220     if (strcmp(__s, "C"))
221       __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
222                             "name not valid"));
223   }
224
225   void
226   locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
227   { __cloc = NULL; }
228
229   __c_locale
230   locale::facet::_S_clone_c_locale(__c_locale&)
231   { return __c_locale(); }
232
233 _GLIBCXX_END_NAMESPACE
234
235 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
236
237   const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
238     {
239       "LC_CTYPE", 
240       "LC_NUMERIC",
241       "LC_TIME",   
242       "LC_COLLATE", 
243       "LC_MONETARY",
244       "LC_MESSAGES"
245     };
246
247 _GLIBCXX_END_NAMESPACE
248
249 _GLIBCXX_BEGIN_NAMESPACE(std)
250
251   const char* const* const locale::_S_categories = __gnu_cxx::category_names;
252
253 _GLIBCXX_END_NAMESPACE
254
255 // XXX GLIBCXX_ABI Deprecated
256 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
257 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
258   extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
259 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
260 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT