c++locale.h

00001 // Wrapper for underlying C-language localization -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 22.8  Standard locale categories.
00032 //
00033 
00034 // Written by Benjamin Kosnik <bkoz@redhat.com>
00035 
00036 #ifndef _C_LOCALE_H
00037 #define _C_LOCALE_H 1
00038 
00039 #pragma GCC system_header
00040 
00041 #include <cstring>              // get std::strlen
00042 #include <cstdio>               // get std::snprintf or std::sprintf
00043 #include <clocale>
00044 #include <langinfo.h>       // For codecvt
00045 #include <iconv.h>      // For codecvt using iconv, iconv_t
00046 #include <libintl.h>        // For messages
00047 
00048 #define _GLIBCXX_C_LOCALE_GNU 1
00049 
00050 #define _GLIBCXX_NUM_CATEGORIES 6
00051 
00052 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00053 namespace __gnu_cxx
00054 {
00055   extern "C" __typeof(uselocale) __uselocale;
00056 }
00057 #endif
00058 
00059 namespace std
00060 {
00061   typedef __locale_t        __c_locale;
00062 
00063   // Convert numeric value of type _Tv to string and return length of
00064   // string.  If snprintf is available use it, otherwise fall back to
00065   // the unsafe sprintf which, in general, can be dangerous and should
00066   // be avoided.
00067   template<typename _Tv>
00068     int
00069     __convert_from_v(char* __out, 
00070              const int __size __attribute__ ((__unused__)),
00071              const char* __fmt,
00072 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00073              _Tv __v, const __c_locale& __cloc, int __prec)
00074     {
00075       __c_locale __old = __gnu_cxx::__uselocale(__cloc);
00076 #else
00077              _Tv __v, const __c_locale&, int __prec)
00078     {
00079       char* __old = std::setlocale(LC_ALL, NULL);
00080       char* __sav = new char[std::strlen(__old) + 1];
00081       std::strcpy(__sav, __old);
00082       std::setlocale(LC_ALL, "C");
00083 #endif
00084 
00085 #ifdef _GLIBCXX_USE_C99
00086       const int __ret = std::snprintf(__out, __size, __fmt, __prec, __v);
00087 #else
00088       const int __ret = std::sprintf(__out, __fmt, __prec, __v);
00089 #endif
00090 
00091 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00092       __gnu_cxx::__uselocale(__old);
00093 #else
00094       std::setlocale(LC_ALL, __sav);
00095       delete [] __sav;
00096 #endif
00097       return __ret;
00098     }
00099 }
00100 
00101 #endif

Generated on Thu Nov 1 17:35:57 2007 for libstdc++ by  doxygen 1.5.1