]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/generic/c_locale.cc
re PR libstdc++/4164 (33 Memory Leak when using iostream)
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / generic / c_locale.cc
1 // Wrapper for underlying C-language localization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 22.8 Standard locale categories.
32 //
33
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
35
36 #include <locale>
37
38 #ifdef _GLIBCPP_HAVE_IEEEFP_H
39 #include <ieeefp.h>
40 #endif
41
42 namespace std
43 {
44 // Specializations for all types used in num_get.
45 template<>
46 void
47 __convert_to_v(const char* __s, long& __v, ios_base::iostate& __err,
48 const __c_locale&, int __base)
49 {
50 if (!(__err & ios_base::failbit))
51 {
52 char* __sanity;
53 errno = 0;
54 long __l = strtol(__s, &__sanity, __base);
55 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
56 __v = __l;
57 else
58 __err |= ios_base::failbit;
59 }
60 }
61
62 template<>
63 void
64 __convert_to_v(const char* __s, unsigned long& __v,
65 ios_base::iostate& __err, const __c_locale&, int __base)
66 {
67 if (!(__err & ios_base::failbit))
68 {
69 char* __sanity;
70 errno = 0;
71 unsigned long __ul = strtoul(__s, &__sanity, __base);
72 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
73 __v = __ul;
74 else
75 __err |= ios_base::failbit;
76 }
77 }
78
79 #ifdef _GLIBCPP_USE_LONG_LONG
80 template<>
81 void
82 __convert_to_v(const char* __s, long long& __v, ios_base::iostate& __err,
83 const __c_locale&, int __base)
84 {
85 if (!(__err & ios_base::failbit))
86 {
87 char* __sanity;
88 errno = 0;
89 long long __ll = strtoll(__s, &__sanity, __base);
90 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
91 __v = __ll;
92 else
93 __err |= ios_base::failbit;
94 }
95 }
96
97 template<>
98 void
99 __convert_to_v(const char* __s, unsigned long long& __v,
100 ios_base::iostate& __err, const __c_locale&, int __base)
101 {
102 if (!(__err & ios_base::failbit))
103 {
104 char* __sanity;
105 errno = 0;
106 unsigned long long __ull = strtoull(__s, &__sanity, __base);
107 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
108 __v = __ull;
109 else
110 __err |= ios_base::failbit;
111 }
112 }
113 #endif
114
115 template<>
116 void
117 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
118 const __c_locale&, int)
119 {
120 if (!(__err & ios_base::failbit))
121 {
122 // Assumes __s formatted for "C" locale.
123 const char* __old = setlocale(LC_ALL, "C");
124 char* __sanity;
125 errno = 0;
126 #if defined(_GLIBCPP_USE_C99)
127 float __f = strtof(__s, &__sanity);
128 #else
129 double __d = strtod(__s, &__sanity);
130 float __f = static_cast<float>(__d);
131 #ifdef _GLIBCPP_HAVE_FINITEF
132 if (!finitef (__f))
133 errno = ERANGE;
134 #elif defined (_GLIBCPP_HAVE_FINITE)
135 if (!finite (static_cast<double> (__f)))
136 errno = ERANGE;
137 #elif defined (_GLIBCPP_HAVE_ISINF)
138 if (isinf (static_cast<double> (__f)))
139 errno = ERANGE;
140 #else
141 if (fabs(__d) > numeric_limits<float>::max())
142 errno = ERANGE;
143 #endif
144 #endif
145 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
146 __v = __f;
147 else
148 __err |= ios_base::failbit;
149 setlocale(LC_ALL, __old);
150 }
151 }
152
153 template<>
154 void
155 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
156 const __c_locale&, int)
157 {
158 if (!(__err & ios_base::failbit))
159 {
160 // Assumes __s formatted for "C" locale.
161 const char* __old = setlocale(LC_ALL, "C");
162 char* __sanity;
163 errno = 0;
164 double __d = strtod(__s, &__sanity);
165 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
166 __v = __d;
167 else
168 __err |= ios_base::failbit;
169 setlocale(LC_ALL, __old);
170 }
171 }
172
173 template<>
174 void
175 __convert_to_v(const char* __s, long double& __v,
176 ios_base::iostate& __err, const __c_locale&, int)
177 {
178 if (!(__err & ios_base::failbit))
179 {
180 // Assumes __s formatted for "C" locale.
181 const char* __old = setlocale(LC_ALL, "C");
182 #if defined(_GLIBCPP_USE_C99)
183 char* __sanity;
184 errno = 0;
185 long double __ld = strtold(__s, &__sanity);
186 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
187 __v = __ld;
188 #else
189 typedef char_traits<char>::int_type int_type;
190 long double __ld;
191 errno = 0;
192 int __p = sscanf(__s, "%Lf", &__ld);
193 if (errno == ERANGE)
194 __p = 0;
195 #ifdef _GLIBCPP_HAVE_FINITEL
196 if ((__p == 1) && !finitel (__ld))
197 __p = 0;
198 #endif
199 if (__p && static_cast<int_type>(__p) != char_traits<char>::eof())
200 __v = __ld;
201 #endif
202 else
203 __err |= ios_base::failbit;
204 setlocale(LC_ALL, __old);
205 }
206 }
207
208 void
209 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char*,
210 __c_locale)
211 { __cloc = NULL; }
212
213 void
214 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
215 { __cloc = NULL; }
216
217 __c_locale
218 locale::facet::_S_clone_c_locale(__c_locale&)
219 { return __c_locale(); }
220 } // namespace std