Update cad/opencascade to version 6.9.1_1
[dports.git] / lang / gcc47 / dragonfly / patch-libstdc++-v3_config_locale_dragonfly_c__locale.cc
1 $NetBSD: patch-libstdc++-v3_config_locale_dragonfly_c__locale.cc,v 1.1 2012/06/23 22:13:02 marino Exp $
2
3 --- libstdc++-v3/config/locale/dragonfly/c_locale.cc.orig       2012-06-22 10:35:30.000000000 +0000
4 +++ libstdc++-v3/config/locale/dragonfly/c_locale.cc
5 @@ -0,0 +1,300 @@
6 +// Wrapper for underlying C-language localization -*- C++ -*-
7 +
8 +// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
9 +// Free Software Foundation, Inc.
10 +//
11 +// This file is part of the GNU ISO C++ Library.  This library is free
12 +// software; you can redistribute it and/or modify it under the
13 +// terms of the GNU General Public License as published by the
14 +// Free Software Foundation; either version 3, or (at your option)
15 +// any later version.
16 +
17 +// This library is distributed in the hope that it will be useful,
18 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 +// GNU General Public License for more details.
21 +
22 +// Under Section 7 of GPL version 3, you are granted additional
23 +// permissions described in the GCC Runtime Library Exception, version
24 +// 3.1, as published by the Free Software Foundation.
25 +
26 +// You should have received a copy of the GNU General Public License and
27 +// a copy of the GCC Runtime Library Exception along with this program;
28 +// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
29 +// <http://www.gnu.org/licenses/>.
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 +
45 +#ifdef _GLIBCXX_HAVE_IEEEFP_H
46 +#include <ieeefp.h>
47 +#endif
48 +
49 +namespace std _GLIBCXX_VISIBILITY(default)
50 +{
51 +_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 +
53 +  template<>
54 +    void
55 +    __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
56 +                  const __c_locale&) throw()
57 +    {
58 +      // Assumes __s formatted for "C" locale.
59 +      char* __old = setlocale(LC_ALL, 0);
60 +      const size_t __len = strlen(__old) + 1;
61 +      char* __sav = new char[__len];
62 +      memcpy(__sav, __old, __len);
63 +      setlocale(LC_ALL, "C");
64 +      char* __sanity;
65 +      bool __overflow = false;
66 +
67 +#if !__FLT_HAS_INFINITY__
68 +      errno = 0;
69 +#endif
70 +
71 +#ifdef _GLIBCXX_HAVE_STRTOF
72 +      __v = strtof(__s, &__sanity);
73 +#else
74 +      double __d = strtod(__s, &__sanity);
75 +      __v = static_cast<float>(__d);
76 +#ifdef _GLIBCXX_HAVE_FINITEF
77 +      if (!finitef (__v))
78 +       __overflow = true;
79 +#elif defined (_GLIBCXX_HAVE_FINITE)
80 +      if (!finite (static_cast<double> (__v)))
81 +       __overflow = true;
82 +#elif defined (_GLIBCXX_HAVE_ISINF)
83 +      if (isinf (static_cast<double> (__v)))
84 +       __overflow = true;
85 +#else
86 +      if (fabs(__d) > numeric_limits<float>::max())
87 +       __overflow = true;
88 +#endif
89 +#endif // _GLIBCXX_HAVE_STRTOF
90 +
91 +      // _GLIBCXX_RESOLVE_LIB_DEFECTS
92 +      // 23. Num_get overflow result.
93 +      if (__sanity == __s || *__sanity != '\0')
94 +       {
95 +         __v = 0.0f;
96 +         __err = ios_base::failbit;
97 +       }
98 +      else if (__overflow
99 +#if __FLT_HAS_INFINITY__
100 +              || __v == numeric_limits<float>::infinity()
101 +              || __v == -numeric_limits<float>::infinity()
102 +#else
103 +              || ((__v > 1.0f || __v < -1.0f) && errno == ERANGE)
104 +#endif
105 +             )
106 +       {
107 +         if (__v > 0.0f)
108 +           __v = numeric_limits<float>::max();
109 +         else
110 +           __v = -numeric_limits<float>::max();
111 +         __err = ios_base::failbit;
112 +       }
113 +
114 +      setlocale(LC_ALL, __sav);
115 +      delete [] __sav;
116 +    }
117 +
118 +  template<>
119 +    void
120 +    __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
121 +                  const __c_locale&) throw()
122 +    {
123 +      // Assumes __s formatted for "C" locale.
124 +      char* __old = setlocale(LC_ALL, 0);
125 +      const size_t __len = strlen(__old) + 1;
126 +      char* __sav = new char[__len];
127 +      memcpy(__sav, __old, __len);
128 +      setlocale(LC_ALL, "C");
129 +      char* __sanity;
130 +
131 +#if !__DBL_HAS_INFINITY__
132 +      errno = 0;
133 +#endif
134 +
135 +      __v = strtod(__s, &__sanity);
136 +
137 +      // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 +      // 23. Num_get overflow result.
139 +      if (__sanity == __s || *__sanity != '\0')
140 +       {
141 +         __v = 0.0;
142 +         __err = ios_base::failbit;
143 +       }
144 +      else if (
145 +#if __DBL_HAS_INFINITY__
146 +              __v == numeric_limits<double>::infinity()
147 +              || __v == -numeric_limits<double>::infinity())
148 +#else
149 +              (__v > 1.0 || __v < -1.0) && errno == ERANGE)
150 +#endif
151 +       {
152 +         if (__v > 0.0)
153 +           __v = numeric_limits<double>::max();
154 +         else
155 +           __v = -numeric_limits<double>::max();
156 +         __err = ios_base::failbit;
157 +       }
158 +
159 +      setlocale(LC_ALL, __sav);
160 +      delete [] __sav;
161 +    }
162 +
163 +  template<>
164 +    void
165 +    __convert_to_v(const char* __s, long double& __v,
166 +                  ios_base::iostate& __err, const __c_locale&) throw()
167 +    {
168 +      // Assumes __s formatted for "C" locale.
169 +      char* __old = setlocale(LC_ALL, 0);
170 +      const size_t __len = strlen(__old) + 1;
171 +      char* __sav = new char[__len];
172 +      memcpy(__sav, __old, __len);
173 +      setlocale(LC_ALL, "C");
174 +
175 +#if !__LDBL_HAS_INFINITY__
176 +      errno = 0;
177 +#endif
178 +
179 +#if defined(_GLIBCXX_HAVE_STRTOLD) && !defined(_GLIBCXX_HAVE_BROKEN_STRTOLD)
180 +      char* __sanity;
181 +      __v = strtold(__s, &__sanity);
182 +
183 +      // _GLIBCXX_RESOLVE_LIB_DEFECTS
184 +      // 23. Num_get overflow result.
185 +      if (__sanity == __s || *__sanity != '\0')
186 +#else
187 +      typedef char_traits<char>::int_type int_type;
188 +      int __p = sscanf(__s, "%Lf", &__v);
189 +
190 +      if (!__p || static_cast<int_type>(__p) == char_traits<char>::eof())
191 +#endif
192 +       {
193 +         __v = 0.0l;
194 +         __err = ios_base::failbit;
195 +       }
196 +       else if (
197 +#if __LDBL_HAS_INFINITY__
198 +               __v == numeric_limits<long double>::infinity()
199 +               || __v == -numeric_limits<long double>::infinity())
200 +#else
201 +               (__v > 1.0l || __v < -1.0l) && errno == ERANGE)
202 +#endif
203 +       {
204 +         if (__v > 0.0l)
205 +           __v = numeric_limits<long double>::max();
206 +         else
207 +           __v = -numeric_limits<long double>::max();
208 +         __err = ios_base::failbit;
209 +       }
210 +
211 +      setlocale(LC_ALL, __sav);
212 +      delete [] __sav;
213 +    }
214 +
215 +
216 +  /*  DragonFly's implementation of setlocale won't accept something like
217 +      "de_DE".  According to nls manpage, the expected format is:
218 +      language[_territory][.codeset][@modifier], but it seems that both
219 +      the _territory and .codeset components are required.
220 +      
221 +      As an attempt to correct for this, we'll tack on ".UTF-8" if 
222 +      a period is not detected in the locale string.  
223 +
224 +      There are no locales with modifiers on DragonFly so if found, they
225 +      will just be stripped off silently.  e.g "de_DE@euro" will be reduced
226 +      to "de_DE".  The UTF-8 default would be added after that.
227 +  */
228 +
229 +  void
230 +  locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
231 +                                   __c_locale)
232 +  {
233 +    const size_t size__s = (__s == NULL) ? 1 : strlen (__s);
234 +    const char UTF8[] = ".UTF-8";
235 +    char localspec[size__s + 6 + 1];
236 +    
237 +    if (__s == NULL) {
238 +       localspec[0] = NULL;
239 +    } else {
240 +       strcpy (localspec, __s);
241 +       char * pch = strchr (localspec, '@');
242 +       if (pch != NULL)
243 +          *pch = 0;
244 +
245 +       if (  (strchr (__s, '.') == NULL)
246 +          && (strcmp (__s, "C") != 0)
247 +          && (strcmp (__s, "POSIX") != 0))
248 +          strncat (localspec, UTF8, 6);
249 +    }
250 +
251 +    const char * result = std::setlocale(LC_ALL, localspec);
252 +    
253 +    if ((strcmp(result, "C") != 0) && (strcmp (result, localspec) != 0))
254 +      __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
255 +                           "name not valid"));
256 +    __cloc = 0;
257 +  }
258 +
259 +  void
260 +  locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
261 +  { __cloc = 0; }
262 +
263 +  __c_locale
264 +  locale::facet::_S_clone_c_locale(__c_locale&) throw()
265 +  { return __c_locale(); }
266 +
267 +  __c_locale
268 +  locale::facet::_S_lc_ctype_c_locale(__c_locale, const char*)
269 +  { return __c_locale(); }
270 +
271 +_GLIBCXX_END_NAMESPACE_VERSION
272 +} // namespace
273 +
274 +namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
275 +{
276 +_GLIBCXX_BEGIN_NAMESPACE_VERSION
277 +
278 +  const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
279 +    {
280 +      "LC_CTYPE",
281 +      "LC_NUMERIC",
282 +      "LC_TIME",
283 +      "LC_COLLATE",
284 +      "LC_MONETARY",
285 +      "LC_MESSAGES"
286 +    };
287 +
288 +_GLIBCXX_END_NAMESPACE_VERSION
289 +} // namespace
290 +
291 +namespace std _GLIBCXX_VISIBILITY(default)
292 +{
293 +_GLIBCXX_BEGIN_NAMESPACE_VERSION
294 +
295 +  const char* const* const locale::_S_categories = __gnu_cxx::category_names;
296 +
297 +_GLIBCXX_END_NAMESPACE_VERSION
298 +} // namespace
299 +
300 +// XXX GLIBCXX_ABI Deprecated
301 +#ifdef _GLIBCXX_LONG_DOUBLE_COMPAT
302 +#define _GLIBCXX_LDBL_COMPAT(dbl, ldbl) \
303 +  extern "C" void ldbl (void) __attribute__ ((alias (#dbl)))
304 +_GLIBCXX_LDBL_COMPAT(_ZSt14__convert_to_vIdEvPKcRT_RSt12_Ios_IostateRKPi, _ZSt14__convert_to_vIeEvPKcRT_RSt12_Ios_IostateRKPi);
305 +#endif // _GLIBCXX_LONG_DOUBLE_COMPAT