00001 // Wrapper for underlying C-language localization -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004 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 _C_LOCALE_H 00037 #define _C_LOCALE_H 1 00038 00039 #pragma GCC system_header 00040 00041 #include <clocale> 00042 #include <cstring> // get std::strlen 00043 #include <cstdio> // get std::snprintf or std::sprintf 00044 00045 #define _GLIBCXX_NUM_CATEGORIES 0 00046 00047 namespace std 00048 { 00049 typedef int* __c_locale; 00050 00051 // Convert numeric value of type _Tv to string and return length of 00052 // string. If snprintf is available use it, otherwise fall back to 00053 // the unsafe sprintf which, in general, can be dangerous and should 00054 // be avoided. 00055 template<typename _Tv> 00056 int 00057 __convert_from_v(char* __out, 00058 const int __size __attribute__((__unused__)), 00059 const char* __fmt, 00060 _Tv __v, const __c_locale&, int __prec) 00061 { 00062 char* __old = std::setlocale(LC_NUMERIC, NULL); 00063 char* __sav = NULL; 00064 if (std::strcmp(__old, "C")) 00065 { 00066 __sav = new char[std::strlen(__old) + 1]; 00067 std::strcpy(__sav, __old); 00068 std::setlocale(LC_NUMERIC, "C"); 00069 } 00070 00071 #ifdef _GLIBCXX_USE_C99 00072 const int __ret = std::snprintf(__out, __size, __fmt, __prec, __v); 00073 #else 00074 const int __ret = std::sprintf(__out, __fmt, __prec, __v); 00075 #endif 00076 00077 if (__sav) 00078 { 00079 std::setlocale(LC_NUMERIC, __sav); 00080 delete [] __sav; 00081 } 00082 return __ret; 00083 } 00084 } 00085 00086 #endif