FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / config / locale / gnu / c_locale.cc
blob065e10aa2e1b82e1c92c80aa7db93b91f3a0df51
1 // Wrapper for underlying C-language localization -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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.
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.
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.
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.
31 // ISO C++ 14882: 22.8 Standard locale categories.
34 // Written by Benjamin Kosnik <bkoz@redhat.com>
36 #include <locale>
37 #include <stdexcept>
38 #include <langinfo.h>
39 #include <bits/c++locale_internal.h>
41 namespace std
43 template<>
44 void
45 __convert_to_v(const char* __s, long& __v, ios_base::iostate& __err,
46 const __c_locale& __cloc, int __base)
48 if (!(__err & ios_base::failbit))
50 char* __sanity;
51 errno = 0;
52 long __l = __strtol_l(__s, &__sanity, __base, __cloc);
53 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
54 __v = __l;
55 else
56 __err |= ios_base::failbit;
60 template<>
61 void
62 __convert_to_v(const char* __s, unsigned long& __v,
63 ios_base::iostate& __err, const __c_locale& __cloc,
64 int __base)
66 if (!(__err & ios_base::failbit))
68 char* __sanity;
69 errno = 0;
70 unsigned long __ul = __strtoul_l(__s, &__sanity, __base, __cloc);
71 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
72 __v = __ul;
73 else
74 __err |= ios_base::failbit;
78 #ifdef _GLIBCPP_USE_LONG_LONG
79 template<>
80 void
81 __convert_to_v(const char* __s, long long& __v, ios_base::iostate& __err,
82 const __c_locale& __cloc, int __base)
84 if (!(__err & ios_base::failbit))
86 char* __sanity;
87 errno = 0;
88 long long __ll = __strtoll_l(__s, &__sanity, __base, __cloc);
89 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
90 __v = __ll;
91 else
92 __err |= ios_base::failbit;
96 template<>
97 void
98 __convert_to_v(const char* __s, unsigned long long& __v,
99 ios_base::iostate& __err, const __c_locale& __cloc,
100 int __base)
102 if (!(__err & ios_base::failbit))
104 char* __sanity;
105 errno = 0;
106 unsigned long long __ull = __strtoull_l(__s, &__sanity, __base,
107 __cloc);
108 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
109 __v = __ull;
110 else
111 __err |= ios_base::failbit;
114 #endif
116 template<>
117 void
118 __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
119 const __c_locale& __cloc, int)
121 if (!(__err & ios_base::failbit))
123 char* __sanity;
124 errno = 0;
125 float __f = __strtof_l(__s, &__sanity, __cloc);
126 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
127 __v = __f;
128 else
129 __err |= ios_base::failbit;
133 template<>
134 void
135 __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
136 const __c_locale& __cloc, int)
138 if (!(__err & ios_base::failbit))
140 char* __sanity;
141 errno = 0;
142 double __d = __strtod_l(__s, &__sanity, __cloc);
143 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
144 __v = __d;
145 else
146 __err |= ios_base::failbit;
150 template<>
151 void
152 __convert_to_v(const char* __s, long double& __v, ios_base::iostate& __err,
153 const __c_locale& __cloc, int)
155 if (!(__err & ios_base::failbit))
157 char* __sanity;
158 errno = 0;
159 long double __ld = __strtold_l(__s, &__sanity, __cloc);
160 if (__sanity != __s && *__sanity == '\0' && errno != ERANGE)
161 __v = __ld;
162 else
163 __err |= ios_base::failbit;
167 void
168 locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
169 __c_locale __old)
171 __cloc = __newlocale(1 << LC_ALL, __s, __old);
172 if (!__cloc)
174 // This named locale is not supported by the underlying OS.
175 __throw_runtime_error("attempt to create locale from unknown name");
179 void
180 locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
182 if (_S_c_locale != __cloc)
183 __freelocale(__cloc);
186 __c_locale
187 locale::facet::_S_clone_c_locale(__c_locale& __cloc)
188 { return __duplocale(__cloc); }
190 const char* locale::_S_categories[_S_categories_size
191 + _S_extra_categories_size] =
193 "LC_CTYPE",
194 "LC_NUMERIC",
195 "LC_TIME",
196 "LC_COLLATE",
197 "LC_MONETARY",
198 "LC_MESSAGES",
199 "LC_PAPER",
200 "LC_NAME",
201 "LC_ADDRESS",
202 "LC_TELEPHONE",
203 "LC_MEASUREMENT",
204 "LC_IDENTIFICATION"
206 } // namespace std