libstdc++
ctype_noninline.h
Go to the documentation of this file.
00001 // Locale support -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2009, 2010
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file bits/ctype_noninline.h
00028  *  This is an internal header file, included by other library headers.
00029  *  Do not attempt to use it directly. @headername{locale}
00030  */
00031 
00032 //
00033 // ISO C++ 14882: 22.1  Locales
00034 //
00035 
00036 // Information as gleaned from /usr/include/ctype.h
00037 
00038 #if _GLIBCXX_C_LOCALE_GNU
00039   const ctype_base::mask*
00040   ctype<char>::classic_table() throw()
00041   { return _S_get_c_locale()->__ctype_b; }
00042 #else
00043   const ctype_base::mask*
00044   ctype<char>::classic_table() throw()
00045   {
00046     const ctype_base::mask* __ret;
00047     char* __old = setlocale(LC_CTYPE, NULL);
00048     char* __sav = NULL;
00049     if (__builtin_strcmp(__old, "C"))
00050       {
00051     const size_t __len = __builtin_strlen(__old) + 1;
00052     __sav = new char[__len];
00053     __builtin_memcpy(__sav, __old, __len);
00054     setlocale(LC_CTYPE, "C");
00055       }
00056 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00057     __ret = *__ctype_b_loc();
00058 #else
00059     __ret = __ctype_b;
00060 #endif
00061     if (__sav)
00062       {
00063     setlocale(LC_CTYPE, __sav);
00064     delete [] __sav;
00065       }
00066     return __ret;
00067   }
00068 #endif
00069 
00070 #if _GLIBCXX_C_LOCALE_GNU
00071   ctype<char>::ctype(__c_locale __cloc, const mask* __table, bool __del,
00072              size_t __refs)
00073   : facet(__refs), _M_c_locale_ctype(_S_clone_c_locale(__cloc)),
00074   _M_del(__table != 0 && __del),
00075   _M_toupper(_M_c_locale_ctype->__ctype_toupper),
00076   _M_tolower(_M_c_locale_ctype->__ctype_tolower),
00077   _M_table(__table ? __table : _M_c_locale_ctype->__ctype_b),
00078   _M_widen_ok(0), _M_narrow_ok(0)
00079   {
00080     __builtin_memset(_M_widen, 0, sizeof(_M_widen));
00081     __builtin_memset(_M_narrow, 0, sizeof(_M_narrow));
00082   }
00083 #else
00084   ctype<char>::ctype(__c_locale, const mask* __table, bool __del,
00085              size_t __refs)
00086   : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
00087   _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
00088   {
00089     char* __old = setlocale(LC_CTYPE, NULL);
00090     char* __sav = NULL;
00091     if (__builtin_strcmp(__old, "C"))
00092       {
00093     const size_t __len = __builtin_strlen(__old) + 1;
00094     __sav = new char[__len];
00095     __builtin_memcpy(__sav, __old, __len);
00096     setlocale(LC_CTYPE, "C");
00097       }
00098 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00099     _M_toupper = *__ctype_toupper_loc();
00100     _M_tolower = *__ctype_tolower_loc();
00101     _M_table = __table ? __table : *__ctype_b_loc();
00102 #else
00103     _M_toupper = __ctype_toupper;
00104     _M_tolower = __ctype_tolower;
00105     _M_table = __table ? __table : __ctype_b;
00106 #endif
00107     if (__sav)
00108       {
00109     setlocale(LC_CTYPE, __sav);
00110     delete [] __sav;
00111       }
00112     __builtin_memset(_M_widen, 0, sizeof(_M_widen));
00113     __builtin_memset(_M_narrow, 0, sizeof(_M_narrow));
00114   }
00115 #endif
00116 
00117 #if _GLIBCXX_C_LOCALE_GNU
00118   ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
00119   : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
00120   _M_del(__table != 0 && __del),
00121   _M_toupper(_M_c_locale_ctype->__ctype_toupper),
00122   _M_tolower(_M_c_locale_ctype->__ctype_tolower),
00123   _M_table(__table ? __table : _M_c_locale_ctype->__ctype_b),
00124   _M_widen_ok(0), _M_narrow_ok(0)
00125   {
00126     __builtin_memset(_M_widen, 0, sizeof(_M_widen));
00127     __builtin_memset(_M_narrow, 0, sizeof(_M_narrow));
00128   }
00129 #else
00130   ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
00131   : facet(__refs), _M_c_locale_ctype(_S_get_c_locale()),
00132   _M_del(__table != 0 && __del), _M_widen_ok(0), _M_narrow_ok(0)
00133   {
00134     char* __old = setlocale(LC_CTYPE, NULL);
00135     char* __sav = NULL;
00136     if (__builtin_strcmp(__old, "C"))
00137       {
00138     const size_t __len = __builtin_strlen(__old) + 1;
00139     __sav = new char[__len];
00140     __builtin_memcpy(__sav, __old, __len);
00141     setlocale(LC_CTYPE, "C");
00142       }
00143 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00144     _M_toupper = *__ctype_toupper_loc();
00145     _M_tolower = *__ctype_tolower_loc();
00146     _M_table = __table ? __table : *__ctype_b_loc();
00147 #else
00148     _M_toupper = __ctype_toupper;
00149     _M_tolower = __ctype_tolower;
00150     _M_table = __table ? __table : __ctype_b;
00151 #endif
00152     if (__sav)
00153       {
00154     setlocale(LC_CTYPE, __sav);
00155     delete [] __sav;
00156       }
00157     __builtin_memset(_M_widen, 0, sizeof(_M_widen));
00158     __builtin_memset(_M_narrow, 0, sizeof(_M_narrow));
00159   }
00160 #endif
00161 
00162   char
00163   ctype<char>::do_toupper(char __c) const
00164   { return _M_toupper[static_cast<unsigned char>(__c)]; }
00165 
00166   const char*
00167   ctype<char>::do_toupper(char* __low, const char* __high) const
00168   {
00169     while (__low < __high)
00170       {
00171     *__low = _M_toupper[static_cast<unsigned char>(*__low)];
00172     ++__low;
00173       }
00174     return __high;
00175   }
00176 
00177   char
00178   ctype<char>::do_tolower(char __c) const
00179   { return _M_tolower[static_cast<unsigned char>(__c)]; }
00180 
00181   const char*
00182   ctype<char>::do_tolower(char* __low, const char* __high) const
00183   {
00184     while (__low < __high)
00185       {
00186     *__low = _M_tolower[static_cast<unsigned char>(*__low)];
00187     ++__low;
00188       }
00189     return __high;
00190   }