# c++locale.h

    00001 // Wrapper for underlying C-language localization -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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 _CPP_BITS_C_LOCALE_H
    00037 #define _CPP_BITS_C_LOCALE_H 1
    00038
    00039 #pragma GCC system_header
    00040
    00041 #include <clocale>
    00042 #include <langinfo.h>       // For codecvt
    00043 #include <iconv.h>      // For codecvt using iconv, iconv_t
    00044 #include <libintl.h>        // For messages
    00045
    00046 #define _GLIBCPP_C_LOCALE_GNU 1
    00047
    00048 #define _GLIBCPP_NUM_CATEGORIES 6
    00049
    00050 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
    00051 namespace __gnu_cxx
    00052 {
    00053   extern "C" __typeof(uselocale) __uselocale;
    00054 }
    00055 #endif
    00056
    00057 namespace std
    00058 {
    00059   typedef __locale_t        __c_locale;
    00060
    00061   // Convert numeric value of type _Tv to string and return length of
    00062   // string.  If snprintf is available use it, otherwise fall back to
    00063   // the unsafe sprintf which, in general, can be dangerous and should
    00064   // be avoided.
    00065   template<typename _Tv>
    00066     int
    00067     __convert_from_v(char* __out, const int __size, const char* __fmt,
    00068 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
    00069              _Tv __v, const __c_locale& __cloc, int __prec = -1)
    00070     {
    00071       __c_locale __old = __gnu_cxx::__uselocale(__cloc);
    00072 #else
    00073              _Tv __v, const __c_locale&, int __prec = -1)
    00074     {
    00075       char* __old = setlocale(LC_ALL, NULL);
    00076       char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
    00077       if (__sav)
    00078         strcpy(__sav, __old);
    00079       setlocale(LC_ALL, "C");
    00080 #endif
    00081
    00082       int __ret;
    00083 #ifdef _GLIBCPP_USE_C99
    00084       if (__prec >= 0)
    00085         __ret = snprintf(__out, __size, __fmt, __prec, __v);
    00086       else
    00087         __ret = snprintf(__out, __size, __fmt, __v);
    00088 #else
    00089       if (__prec >= 0)
    00090         __ret = sprintf(__out, __fmt, __prec, __v);
    00091       else
    00092         __ret = sprintf(__out, __fmt, __v);
    00093 #endif
    00094
    00095 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
    00096       __gnu_cxx::__uselocale(__old);
    00097 #else
    00098       setlocale(LC_ALL, __sav);
    00099       free(__sav);
    00100 #endif
    00101       return __ret;
    00102     }
    00103 }
    00104
    00105 #endif