OpenCores
URL https://opencores.org/ocsvn/or1200_soc/or1200_soc/trunk

Subversion Repositories or1200_soc

[/] [or1200_soc/] [trunk/] [sw/] [Meansoffreedom/] [C_plus_hack/] [c++locale.cc] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 26 qaztronic
// Wrapper for underlying C-language localization -*- C++ -*-
2
 
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.
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 <cerrno>  // For errno
37
#include <cmath>  // For isinf, finite, finitef, fabs
38
#include <cstdlib>  // For strof, strtold
39
#include <locale>
40
 
41
#ifdef _GLIBCXX_HAVE_IEEEFP_H
42
#include <ieeefp.h>
43
#endif
44
 
45
namespace std
46
{
47
  // Specializations for all types used in num_get.
48
  template<>
49
    void
50
    __convert_to_v(const char* __s, float& __v, ios_base::iostate& __err,
51
                   const __c_locale&)
52
    {
53
      if (!(__err & ios_base::failbit))
54
        {
55
          // Assumes __s formatted for "C" locale.
56
          char* __old = strdup(setlocale(LC_ALL, NULL));
57
          setlocale(LC_ALL, "C");
58
          char* __sanity;
59
          errno = 0;
60
// #if defined(_GLIBCXX_HAVE_STRTOF)
61
#if 0
62
          float __f = strtof(__s, &__sanity);
63
#else
64
          double __d = strtod(__s, &__sanity);
65
          float __f = static_cast<float>(__d);
66
// #ifdef _GLIBCXX_HAVE_FINITEF
67
#if 0
68
          if (!finitef (__f))
69
            errno = ERANGE;
70
// #elif defined (_GLIBCXX_HAVE_FINITE)
71
#elif 0
72
          if (!finite (static_cast<double> (__f)))
73
            errno = ERANGE;
74
// #elif defined (_GLIBCXX_HAVE_ISINF)
75
#elif 0
76
          if (isinf (static_cast<double> (__f)))
77
            errno = ERANGE;
78
#else
79
          if (fabs(__d) > numeric_limits<float>::max())
80
            errno = ERANGE;
81
#endif
82
#endif
83
          if (__sanity != __s && errno != ERANGE)
84
            __v = __f;
85
          else
86
            __err |= ios_base::failbit;
87
          setlocale(LC_ALL, __old);
88
          free(__old);
89
        }
90
    }
91
 
92
  template<>
93
    void
94
    __convert_to_v(const char* __s, double& __v, ios_base::iostate& __err,
95
                   const __c_locale&)
96
    {
97
      if (!(__err & ios_base::failbit))
98
        {
99
          // Assumes __s formatted for "C" locale.
100
          char* __old = strdup(setlocale(LC_ALL, NULL));
101
          setlocale(LC_ALL, "C");
102
          char* __sanity;
103
          errno = 0;
104
          double __d = strtod(__s, &__sanity);
105
          if (__sanity != __s && errno != ERANGE)
106
            __v = __d;
107
          else
108
            __err |= ios_base::failbit;
109
          setlocale(LC_ALL, __old);
110
          free(__old);
111
        }
112
    }
113
 
114
  template<>
115
    void
116
    __convert_to_v(const char* __s, long double& __v,
117
                   ios_base::iostate& __err, const __c_locale&)
118
    {
119
      if (!(__err & ios_base::failbit))
120
        {
121
          // Assumes __s formatted for "C" locale.
122
          char* __old = strdup(setlocale(LC_ALL, NULL));
123
          setlocale(LC_ALL, "C");
124
#if defined(_GLIBCXX_HAVE_STRTOLD)
125
          char* __sanity;
126
          errno = 0;
127
          long double __ld = strtold(__s, &__sanity);
128
          if (__sanity != __s && errno != ERANGE)
129
            __v = __ld;
130
#else
131
          typedef char_traits<char>::int_type int_type;
132
          long double __ld;
133
          errno = 0;
134
          int __p = sscanf(__s, "%Lf", &__ld);
135
          if (errno == ERANGE)
136
            __p = 0;
137
#ifdef _GLIBCXX_HAVE_FINITEL
138
          if ((__p == 1) && !finitel (__ld))
139
            __p = 0;
140
#endif
141
          if (__p && static_cast<int_type>(__p) != char_traits<char>::eof())
142
            __v = __ld;
143
#endif
144
          else
145
            __err |= ios_base::failbit;
146
          setlocale(LC_ALL, __old);
147
          free(__old);
148
        }
149
    }
150
 
151
  void
152
  locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s,
153
                                    __c_locale)
154
  {
155
    // Currently, the generic model only supports the "C" locale.
156
    // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html
157
    __cloc = NULL;
158
    if (strcmp(__s, "C"))
159
      __throw_runtime_error(__N("locale::facet::_S_create_c_locale "
160
                            "name not valid"));
161
  }
162
 
163
  void
164
  locale::facet::_S_destroy_c_locale(__c_locale& __cloc)
165
  { __cloc = NULL; }
166
 
167
  __c_locale
168
  locale::facet::_S_clone_c_locale(__c_locale&)
169
  { return __c_locale(); }
170
} // namespace std
171
 
172
namespace __gnu_cxx
173
{
174
  const char* const category_names[6 + _GLIBCXX_NUM_CATEGORIES] =
175
    {
176
      "LC_CTYPE",
177
      "LC_NUMERIC",
178
      "LC_TIME",
179
      "LC_COLLATE",
180
      "LC_MONETARY",
181
      "LC_MESSAGES"
182
    };
183
}
184
 
185
namespace std
186
{
187
  const char* const* const locale::_S_categories = __gnu_cxx::category_names;
188
}  // namespace std

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.