1169691Skan// std::ctype implementation details, GNU version -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the
7169691Skan// terms of the GNU General Public License as published by the
8169691Skan// Free Software Foundation; either version 2, or (at your option)
9169691Skan// any later version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful,
12169691Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14169691Skan// GNU General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License along
17169691Skan// with this library; see the file COPYING.  If not, write to the Free
18169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19169691Skan// USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free software
22169691Skan// library without restriction.  Specifically, if other files instantiate
23169691Skan// templates or use macros or inline functions from this file, or you compile
24169691Skan// this file and link it with other files to produce an executable, this
25169691Skan// file does not by itself cause the resulting executable to be covered by
26169691Skan// the GNU General Public License.  This exception does not however
27169691Skan// invalidate any other reasons why the executable file might be covered by
28169691Skan// the GNU General Public License.
29169691Skan
30169691Skan//
31169691Skan// ISO C++ 14882: 22.2.1.1.2  ctype virtual functions.
32169691Skan//
33169691Skan
34169691Skan// Written by Benjamin Kosnik <bkoz@redhat.com>
35169691Skan
36169691Skan#include <locale>
37169691Skan#include <bits/c++locale_internal.h>
38169691Skan
39169691Skannamespace std
40169691Skan{
41169691Skan  // NB: The other ctype<char> specializations are in src/locale.cc and
42169691Skan  // various /config/os/* files.
43169691Skan  template<>
44169691Skan    ctype_byname<char>::ctype_byname(const char* __s, size_t __refs)
45169691Skan    : ctype<char>(0, false, __refs)
46169691Skan    {
47169691Skan      if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
48169691Skan	{
49169691Skan	  this->_S_destroy_c_locale(this->_M_c_locale_ctype);
50169691Skan	  this->_S_create_c_locale(this->_M_c_locale_ctype, __s);
51169691Skan	}
52169691Skan    }
53169691Skan
54169691Skan#ifdef _GLIBCXX_USE_WCHAR_T
55169691Skan  ctype<wchar_t>::__wmask_type
56169691Skan  ctype<wchar_t>::_M_convert_to_wmask(const mask __m) const
57169691Skan  {
58169691Skan    // Darwin uses the same codes for 'char' as 'wchar_t', so this routine
59169691Skan    // never gets called.
60169691Skan    return __m;
61169691Skan  };
62169691Skan
63169691Skan  wchar_t
64169691Skan  ctype<wchar_t>::do_toupper(wchar_t __c) const
65169691Skan  { return towupper(__c); }
66169691Skan
67169691Skan  const wchar_t*
68169691Skan  ctype<wchar_t>::do_toupper(wchar_t* __lo, const wchar_t* __hi) const
69169691Skan  {
70169691Skan    while (__lo < __hi)
71169691Skan      {
72169691Skan        *__lo = towupper(*__lo);
73169691Skan        ++__lo;
74169691Skan      }
75169691Skan    return __hi;
76169691Skan  }
77169691Skan
78169691Skan  wchar_t
79169691Skan  ctype<wchar_t>::do_tolower(wchar_t __c) const
80169691Skan  { return towlower(__c); }
81169691Skan
82169691Skan  const wchar_t*
83169691Skan  ctype<wchar_t>::do_tolower(wchar_t* __lo, const wchar_t* __hi) const
84169691Skan  {
85169691Skan    while (__lo < __hi)
86169691Skan      {
87169691Skan        *__lo = towlower(*__lo);
88169691Skan        ++__lo;
89169691Skan      }
90169691Skan    return __hi;
91169691Skan  }
92169691Skan
93169691Skan  wchar_t
94169691Skan  ctype<wchar_t>::
95169691Skan  do_widen(char __c) const
96169691Skan  { return _M_widen[static_cast<unsigned char>(__c)]; }
97169691Skan
98169691Skan  const char*
99169691Skan  ctype<wchar_t>::
100169691Skan  do_widen(const char* __lo, const char* __hi, wchar_t* __dest) const
101169691Skan  {
102169691Skan    while (__lo < __hi)
103169691Skan      {
104169691Skan	*__dest = _M_widen[static_cast<unsigned char>(*__lo)];
105169691Skan	++__lo;
106169691Skan	++__dest;
107169691Skan      }
108169691Skan    return __hi;
109169691Skan  }
110169691Skan
111169691Skan  char
112169691Skan  ctype<wchar_t>::
113169691Skan  do_narrow(wchar_t __wc, char __dfault) const
114169691Skan  {
115169691Skan    if (__wc >= 0 && __wc < 128 && _M_narrow_ok)
116169691Skan      return _M_narrow[__wc];
117169691Skan    const int __c = wctob(__wc);
118169691Skan    return (__c == EOF ? __dfault : static_cast<char>(__c));
119169691Skan  }
120169691Skan
121169691Skan  const wchar_t*
122169691Skan  ctype<wchar_t>::
123169691Skan  do_narrow(const wchar_t* __lo, const wchar_t* __hi, char __dfault,
124169691Skan	    char* __dest) const
125169691Skan  {
126169691Skan    if (_M_narrow_ok)
127169691Skan      while (__lo < __hi)
128169691Skan	{
129169691Skan	  if (*__lo >= 0 && *__lo < 128)
130169691Skan	    *__dest = _M_narrow[*__lo];
131169691Skan	  else
132169691Skan	    {
133169691Skan	      const int __c = wctob(*__lo);
134169691Skan	      *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
135169691Skan	    }
136169691Skan	  ++__lo;
137169691Skan	  ++__dest;
138169691Skan	}
139169691Skan    else
140169691Skan      while (__lo < __hi)
141169691Skan	{
142169691Skan	  const int __c = wctob(*__lo);
143169691Skan	  *__dest = (__c == EOF ? __dfault : static_cast<char>(__c));
144169691Skan	  ++__lo;
145169691Skan	  ++__dest;
146169691Skan	}
147169691Skan    return __hi;
148169691Skan  }
149169691Skan
150169691Skan  void
151169691Skan  ctype<wchar_t>::_M_initialize_ctype()
152169691Skan  {
153169691Skan    wint_t __i;
154169691Skan    for (__i = 0; __i < 128; ++__i)
155169691Skan      {
156169691Skan	const int __c = wctob(__i);
157169691Skan	if (__c == EOF)
158169691Skan	  break;
159169691Skan	else
160169691Skan	  _M_narrow[__i] = static_cast<char>(__c);
161169691Skan      }
162169691Skan    if (__i == 128)
163169691Skan      _M_narrow_ok = true;
164169691Skan    else
165169691Skan      _M_narrow_ok = false;
166169691Skan    for (size_t __i = 0;
167169691Skan	 __i < sizeof(_M_widen) / sizeof(wint_t); ++__i)
168169691Skan      _M_widen[__i] = btowc(__i);
169169691Skan  }
170169691Skan#endif //  _GLIBCXX_USE_WCHAR_T
171169691Skan}
172