1117397Skan// Locale support -*- C++ -*-
2117397Skan
3132720Skan// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2004
4117397Skan// Free Software Foundation, Inc.
5117397Skan//
6117397Skan// This file is part of the GNU ISO C++ Library.  This library is free
7117397Skan// software; you can redistribute it and/or modify it under the
8117397Skan// terms of the GNU General Public License as published by the
9117397Skan// Free Software Foundation; either version 2, or (at your option)
10117397Skan// any later version.
11117397Skan
12117397Skan// This library is distributed in the hope that it will be useful,
13117397Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14117397Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15117397Skan// GNU General Public License for more details.
16117397Skan
17117397Skan// You should have received a copy of the GNU General Public License along
18117397Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20117397Skan// USA.
21117397Skan
22117397Skan// As a special exception, you may use this file as part of a free software
23117397Skan// library without restriction.  Specifically, if other files instantiate
24117397Skan// templates or use macros or inline functions from this file, or you compile
25117397Skan// this file and link it with other files to produce an executable, this
26117397Skan// file does not by itself cause the resulting executable to be covered by
27117397Skan// the GNU General Public License.  This exception does not however
28117397Skan// invalidate any other reasons why the executable file might be covered by
29117397Skan// the GNU General Public License.
30117397Skan
31169691Skan/** @file ctype_noninline.h
32169691Skan *  This is an internal header file, included by other library headers.
33169691Skan *  You should not attempt to use it directly.
34169691Skan */
35169691Skan
36117397Skan//
37117397Skan// ISO C++ 14882: 22.1  Locales
38117397Skan//
39117397Skan
40117397Skan// Information as gleaned from /usr/include/ctype.h
41117397Skan
42132720Skan#if _GLIBCXX_C_LOCALE_GNU
43117397Skan  const ctype_base::mask*
44117397Skan  ctype<char>::classic_table() throw()
45132720Skan  { return _S_get_c_locale()->__ctype_b; }
46117397Skan#else
47117397Skan  const ctype_base::mask*
48117397Skan  ctype<char>::classic_table() throw()
49117397Skan  {
50117397Skan    const ctype_base::mask* __ret;
51117397Skan    char* __old = strdup(setlocale(LC_CTYPE, NULL));
52117397Skan    setlocale(LC_CTYPE, "C");
53117397Skan#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
54117397Skan    __ret = *__ctype_b_loc();
55117397Skan#else
56117397Skan    __ret = __ctype_b;
57117397Skan#endif
58117397Skan    setlocale(LC_CTYPE, __old);
59117397Skan    free(__old);
60117397Skan    return __ret;
61117397Skan  }
62117397Skan#endif
63117397Skan
64132720Skan#if _GLIBCXX_C_LOCALE_GNU
65117397Skan  ctype<char>::ctype(__c_locale __cloc, const mask* __table, bool __del,
66117397Skan		     size_t __refs)
67132720Skan  : facet(__refs), _M_c_locale_ctype(_S_clone_c_locale(__cloc)),
68132720Skan  _M_del(__table != 0 && __del),
69132720Skan  _M_toupper(_M_c_locale_ctype->__ctype_toupper),
70132720Skan  _M_tolower(_M_c_locale_ctype->__ctype_tolower),
71132720Skan  _M_table(__table ? __table : _M_c_locale_ctype->__ctype_b),
72132720Skan  _M_widen_ok(0), _M_narrow_ok(0)
73117397Skan  {
74132720Skan    memset(_M_widen, 0, sizeof(_M_widen));
75132720Skan    memset(_M_narrow, 0, sizeof(_M_narrow));
76117397Skan  }
77117397Skan#else
78117397Skan  ctype<char>::ctype(__c_locale, const mask* __table, bool __del,
79117397Skan		     size_t __refs)
80132720Skan  : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
81132720Skan  _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
82117397Skan  {
83117397Skan    char* __old=strdup(setlocale(LC_CTYPE, NULL));
84117397Skan    setlocale(LC_CTYPE, "C");
85117397Skan#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
86117397Skan    _M_toupper = *__ctype_toupper_loc();
87117397Skan    _M_tolower = *__ctype_tolower_loc();
88117397Skan    _M_table = __table ? __table : *__ctype_b_loc();
89117397Skan#else
90117397Skan    _M_toupper = __ctype_toupper;
91117397Skan    _M_tolower = __ctype_tolower;
92117397Skan    _M_table = __table ? __table : __ctype_b;
93117397Skan#endif
94117397Skan    setlocale(LC_CTYPE, __old);
95117397Skan    free(__old);
96132720Skan    memset(_M_widen, 0, sizeof(_M_widen));
97132720Skan    memset(_M_narrow, 0, sizeof(_M_narrow));
98117397Skan  }
99117397Skan#endif
100117397Skan
101132720Skan#if _GLIBCXX_C_LOCALE_GNU
102132720Skan  ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
103132720Skan  : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
104132720Skan  _M_del(__table != 0 && __del),
105132720Skan  _M_toupper(_M_c_locale_ctype->__ctype_toupper),
106132720Skan  _M_tolower(_M_c_locale_ctype->__ctype_tolower),
107132720Skan  _M_table(__table ? __table : _M_c_locale_ctype->__ctype_b),
108132720Skan  _M_widen_ok(0), _M_narrow_ok(0)
109117397Skan  {
110132720Skan    memset(_M_widen, 0, sizeof(_M_widen));
111132720Skan    memset(_M_narrow, 0, sizeof(_M_narrow));
112117397Skan  }
113117397Skan#else
114132720Skan  ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
115132720Skan  : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
116132720Skan  _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
117117397Skan  {
118117397Skan    char* __old=strdup(setlocale(LC_CTYPE, NULL));
119117397Skan    setlocale(LC_CTYPE, "C");
120117397Skan#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
121117397Skan    _M_toupper = *__ctype_toupper_loc();
122117397Skan    _M_tolower = *__ctype_tolower_loc();
123117397Skan    _M_table = __table ? __table : *__ctype_b_loc();
124117397Skan#else
125117397Skan    _M_toupper = __ctype_toupper;
126117397Skan    _M_tolower = __ctype_tolower;
127117397Skan    _M_table = __table ? __table : __ctype_b;
128117397Skan#endif
129117397Skan    setlocale(LC_CTYPE, __old);
130117397Skan    free(__old);
131132720Skan    memset(_M_widen, 0, sizeof(_M_widen));
132132720Skan    memset(_M_narrow, 0, sizeof(_M_narrow));
133117397Skan  }
134117397Skan#endif
135117397Skan
136117397Skan  char
137117397Skan  ctype<char>::do_toupper(char __c) const
138117397Skan  { return _M_toupper[static_cast<unsigned char>(__c)]; }
139117397Skan
140117397Skan  const char*
141117397Skan  ctype<char>::do_toupper(char* __low, const char* __high) const
142117397Skan  {
143117397Skan    while (__low < __high)
144117397Skan      {
145117397Skan	*__low = _M_toupper[static_cast<unsigned char>(*__low)];
146117397Skan	++__low;
147117397Skan      }
148117397Skan    return __high;
149117397Skan  }
150117397Skan
151117397Skan  char
152117397Skan  ctype<char>::do_tolower(char __c) const
153117397Skan  { return _M_tolower[static_cast<unsigned char>(__c)]; }
154117397Skan
155117397Skan  const char*
156117397Skan  ctype<char>::do_tolower(char* __low, const char* __high) const
157117397Skan  {
158117397Skan    while (__low < __high)
159117397Skan      {
160117397Skan	*__low = _M_tolower[static_cast<unsigned char>(*__low)];
161117397Skan	++__low;
162117397Skan      }
163117397Skan    return __high;
164117397Skan  }
165