gcc50: Add proper locale support to libstdc++ (local mod)
[dragonfly.git] / contrib / gcc-5.0 / libstdc++-v3 / config / locale / dragonfly / numeric_members.cc
1 // std::numpunct implementation details, DragonFly version -*- C++ -*-
2
3 // Copyright (C) 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.2.3.1.2  numpunct virtual functions
27 //
28
29 // Written by Benjamin Kosnik <bkoz@redhat.com>
30 // Modified for DragonFly by John Marino <gnugcc@marino.st>
31
32 #include <locale>
33 #include <cstring>
34 #include <xlocale.h>
35
36 namespace std _GLIBCXX_VISIBILITY(default)
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40   template<> 
41     void
42     numpunct<char>::_M_initialize_numpunct(__c_locale __cloc)
43     {
44       if (!_M_data)
45         _M_data = new __numpunct_cache<char>;
46
47       if (!__cloc)
48         {
49           // "C" locale
50           _M_data->_M_grouping = "";
51           _M_data->_M_grouping_size = 0;
52           _M_data->_M_use_grouping = false;
53
54           _M_data->_M_decimal_point = '.';
55           _M_data->_M_thousands_sep = ',';
56
57           for (size_t __i = 0; __i < __num_base::_S_oend; ++__i)
58             _M_data->_M_atoms_out[__i] = __num_base::_S_atoms_out[__i];
59
60           for (size_t __j = 0; __j < __num_base::_S_iend; ++__j)
61             _M_data->_M_atoms_in[__j] = __num_base::_S_atoms_in[__j];
62         }
63       else
64         {
65           // Named locale.
66           lconv* lc = localeconv_l((locale_t) __cloc);
67
68           // Decimal point should always be defined, but check null anyway
69           if (lc->decimal_point == NULL)
70             {
71               // Not defined, so use "C" locale default
72               _M_data->_M_decimal_point = '.';
73             }
74           else
75             {
76               _M_data->_M_decimal_point = lc->decimal_point[0];
77             }
78           // Check for NULL, which implies no grouping.
79           if (lc->thousands_sep == NULL || lc->thousands_sep[0] == '\0')
80             {
81               // Like in "C" locale.
82               _M_data->_M_grouping = "";
83               _M_data->_M_grouping_size = 0;
84               _M_data->_M_use_grouping = false;
85               _M_data->_M_thousands_sep = ',';
86             }
87           else
88             {
89               _M_data->_M_thousands_sep = lc->thousands_sep[0];
90
91               const char* __src = lc->grouping;
92               const size_t __len = strlen(__src);
93               if (__len)
94                 {
95                   __try
96                     {
97                       char* __dst = new char[__len + 1];
98                       memcpy(__dst, __src, __len + 1);
99                       _M_data->_M_grouping = __dst;
100                       _M_data->_M_use_grouping = true;
101                     }
102                   __catch(...)
103                     {
104                       delete _M_data;
105                       _M_data = 0;
106                       __throw_exception_again;
107                     }
108                 }
109               else
110                 {
111                   _M_data->_M_grouping = "";
112                   _M_data->_M_use_grouping = false;
113                 }
114               _M_data->_M_grouping_size = __len;
115             }
116         }
117
118       // NB: There is no way to extact this info from posix locales.
119       // _M_truename = __nl_langinfo_l(YESSTR, __cloc);
120       _M_data->_M_truename = "true";
121       _M_data->_M_truename_size = 4;
122       // _M_falsename = __nl_langinfo_l(NOSTR, __cloc);
123       _M_data->_M_falsename = "false";
124       _M_data->_M_falsename_size = 5;
125     }
126  
127   template<> 
128     numpunct<char>::~numpunct()
129     {
130       if (_M_data->_M_grouping_size)
131         delete [] _M_data->_M_grouping;
132       delete _M_data;
133     }
134    
135 #ifdef _GLIBCXX_USE_WCHAR_T
136   template<> 
137     void
138     numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc)
139     {
140       if (!_M_data)
141         _M_data = new __numpunct_cache<wchar_t>;
142
143       if (!__cloc)
144         {
145           // "C" locale
146           _M_data->_M_grouping = "";
147           _M_data->_M_grouping_size = 0;
148           _M_data->_M_use_grouping = false;
149
150           _M_data->_M_decimal_point = L'.';
151           _M_data->_M_thousands_sep = L',';
152
153           // Use ctype::widen code without the facet...
154           for (size_t __i = 0; __i < __num_base::_S_oend; ++__i)
155             _M_data->_M_atoms_out[__i] =
156               static_cast<wchar_t>(__num_base::_S_atoms_out[__i]);
157
158           for (size_t __j = 0; __j < __num_base::_S_iend; ++__j)
159             _M_data->_M_atoms_in[__j] =
160               static_cast<wchar_t>(__num_base::_S_atoms_in[__j]);
161         }
162       else
163         {
164           // Named locale.
165           lconv* lc = localeconv_l((locale_t) __cloc);
166
167           // Decimal point should always be defined, but check null anyway
168           if (lc->decimal_point == NULL)
169             {
170               // Not defined, so use "C" locale default
171               _M_data->_M_decimal_point = L'.';
172             }
173           else
174             {
175               _M_data->_M_decimal_point = (wchar_t)lc->decimal_point[0];
176             }
177           // Check for NULL, which implies no grouping.
178           if (lc->thousands_sep == NULL || lc->thousands_sep[0] == '\0')
179             {
180               // Like in "C" locale.
181               _M_data->_M_grouping = "";
182               _M_data->_M_grouping_size = 0;
183               _M_data->_M_use_grouping = false;
184               _M_data->_M_thousands_sep = L',';
185             }
186           else
187             {
188               _M_data->_M_thousands_sep = (wchar_t)lc->thousands_sep[0];
189
190               const char* __src = lc->grouping;
191               const size_t __len = strlen(__src);
192               if (__len)
193                 {
194                   __try
195                     {
196                       char* __dst = new char[__len + 1];
197                       memcpy(__dst, __src, __len + 1);
198                       _M_data->_M_grouping = __dst;
199                     }
200                   __catch(...)
201                     {
202                       delete _M_data;
203                       _M_data = 0;
204                       __throw_exception_again;
205                     }
206                 }
207               else
208                 {
209                   _M_data->_M_grouping = "";
210                   _M_data->_M_use_grouping = false;
211                 }
212               _M_data->_M_grouping_size = __len;
213             }
214         }
215
216       // NB: There is no way to extact this info from posix locales.
217       // _M_truename = __nl_langinfo_l(YESSTR, __cloc);
218       _M_data->_M_truename = L"true";
219       _M_data->_M_truename_size = 4;
220       // _M_falsename = __nl_langinfo_l(NOSTR, __cloc);
221       _M_data->_M_falsename = L"false";
222       _M_data->_M_falsename_size = 5;
223     }
224
225   template<> 
226     numpunct<wchar_t>::~numpunct()
227     {
228       if (_M_data->_M_grouping_size)
229         delete [] _M_data->_M_grouping;
230       delete _M_data;
231     }
232  #endif
233
234 _GLIBCXX_END_NAMESPACE_VERSION
235 } // namespace