gcc50: Add proper locale support to libstdc++ (local mod)
[dragonfly.git] / contrib / gcc-5.0 / libstdc++-v3 / config / locale / dragonfly / c_locale.cc
1 // localization implementation details, DragonFly version -*- C++ -*-
2
3 // Copyright (C) 2014-2015 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 //
26 // ISO C++ 14882: 22.8  Standard locale categories.
27 //
28
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
30 // Modified for DragonFly by John Marino <gnugcc@marino.st>
31
32 #include <cstdlib>
33 #include <locale>
34 #include <stdexcept>
35 #include <limits>
36 #include <langinfo.h>
37 #include <xlocale.h>
38
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42
43   template<>
44     void
45     __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
46                    const __c_locale& __cloc) throw()
47     {
48       char* __sanity;
49       __v = strtof_l(__s, &__sanity, (locale_t)__cloc);
50
51       // _GLIBCXX_RESOLVE_LIB_DEFECTS
52       // 23. Num_get overflow result.
53       if (__sanity == __s || *__sanity != '\0')
54         {
55           __v = 0.0f;
56           __err = ios_base::failbit;
57         }
58       else if (__v == numeric_limits<float>::infinity())
59         {
60           __v = numeric_limits<float>::max();
61           __err = ios_base::failbit;
62         }
63       else if (__v == -numeric_limits<float>::infinity())
64         {
65           __v = -numeric_limits<float>::max();
66           __err = ios_base::failbit;
67         }
68     }
69
70   template<>
71     void
72     __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
73                    const __c_locale& __cloc) throw()
74     {
75       char* __sanity;
76       __v = strtod_l(__s, &__sanity, (locale_t)__cloc);
77
78       // _GLIBCXX_RESOLVE_LIB_DEFECTS
79       // 23. Num_get overflow result.
80       if (__sanity == __s || *__sanity != '\0')
81         {
82           __v = 0.0;
83           __err = ios_base::failbit;
84         }
85       else if (__v == numeric_limits<double>::infinity())
86         {
87           __v = numeric_limits<double>::max();
88           __err = ios_base::failbit;
89         }
90       else if (__v == -numeric_limits<double>::infinity())
91         {
92           __v = -numeric_limits<double>::max();
93           __err = ios_base::failbit;
94         }
95     }
96
97   template<>
98     void
99     __convert_to_v(const char* __s, long double& __v, ios_base::iostate& __err,
100                    const __c_locale& __cloc) throw()
101     {
102       char* __sanity;
103       __v = strtold_l(__s, &__sanity, (locale_t)__cloc);
104
105       // _GLIBCXX_RESOLVE_LIB_DEFECTS
106       // 23. Num_get overflow result.
107       if (__sanity == __s || *__sanity != '\0')
108         {
109           __v = 0.0l;
110           __err = ios_base::failbit;
111         }
112       else if (__v == numeric_limits<long double>::infinity())
113         {
114           __v = numeric_limits<long double>::max();
115           __err = ios_base::failbit;
116         }
117       else if (__v == -numeric_limits<long double>::infinity())
118         {
119           __v = -numeric_limits<long double>::max();
120           __err = ios_base::failbit;
121         }
122     }
123
124   void
125   locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
126                                     __c_locale __old)
127   {
128     __cloc = (__c_locale)newlocale(LC_ALL_MASK, __s, (locale_t)__old);
129     if (!__cloc)
130       {
131         // This named locale is not supported by the underlying OS.
132         __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
133                                   "name not valid"));
134       }
135   }
136
137   void
138   locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
139   {
140     if (__cloc && _S_get_c_locale() != __cloc)
141       freelocale((locale_t)__cloc);
142   }
143
144   __c_locale
145   locale::facet::_S_clone_c_locale(__c_locale& __cloc) throw()
146   { return (__c_locale)duplocale((locale_t)__cloc); }
147
148   __c_locale
149   locale::facet::_S_lc_ctype_c_locale(__c_locale __cloc, const char* __s)
150   {
151     __c_locale __dup = (__c_locale)duplocale((locale_t)__cloc);
152     if (__dup == __c_locale(0))
153       __throw_runtime_error(__N("locale::facet::_S_lc_ctype_c_locale "
154                                 "duplocale error"));
155     __c_locale __changed = (__c_locale)newlocale(LC_CTYPE_MASK, __s,
156                                                  (locale_t)__dup);
157     if (__changed == __c_locale(0))
158       {
159         freelocale((locale_t)__dup);
160         __throw_runtime_error(__N("locale::facet::_S_lc_ctype_c_locale "
161                                   "newlocale error"));
162       }
163     return __changed;
164   }
165
166 _GLIBCXX_END_NAMESPACE_VERSION
167 } // namespace
168
169 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
170 {
171 _GLIBCXX_BEGIN_NAMESPACE_VERSION
172
173   const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
174     {
175       "LC_CTYPE",
176       "LC_NUMERIC",
177       "LC_TIME",
178       "LC_COLLATE",
179       "LC_MONETARY",
180       "LC_MESSAGES"
181     };
182
183 _GLIBCXX_END_NAMESPACE_VERSION
184 } // namespace
185
186 namespace std _GLIBCXX_VISIBILITY(default)
187 {
188 _GLIBCXX_BEGIN_NAMESPACE_VERSION
189
190   const char* const* const locale::_S_categories = __gnu_cxx::category_names;
191
192 _GLIBCXX_END_NAMESPACE_VERSION
193 } // namespace
194
195 // XXX GLIBCXX_ABI Deprecated
196 #ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
197 #define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
198   extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
199 _GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKP15__locale_struct, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKP15__locale_struct);
200 #endif // _GLIBCXX_LONG_DOUBLE_COMPAT