libstdc++
c++locale.h
Go to the documentation of this file.
00001 // Wrapper for underlying C-language localization -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/c++locale.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{locale}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 22.8  Standard locale categories.
00032 //
00033 
00034 // Written by Benjamin Kosnik <bkoz@redhat.com>
00035 
00036 #ifndef _GLIBCXX_CXX_LOCALE_H
00037 #define _GLIBCXX_CXX_LOCALE_H 1
00038 
00039 #pragma GCC system_header
00040 
00041 #include <clocale>
00042 
00043 #define _GLIBCXX_C_LOCALE_GNU 1
00044 
00045 #define _GLIBCXX_NUM_CATEGORIES 6
00046 
00047 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00048 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
00049 {
00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00051 
00052   extern "C" __typeof(uselocale) __uselocale;
00053 
00054 _GLIBCXX_END_NAMESPACE_VERSION
00055 } // namespace
00056 #endif
00057 
00058 namespace std _GLIBCXX_VISIBILITY(default)
00059 {
00060 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00061 
00062   typedef __locale_t        __c_locale;
00063 
00064   // Convert numeric value of type double and long double to string and
00065   // return length of string.  If vsnprintf is available use it, otherwise
00066   // fall back to the unsafe vsprintf which, in general, can be dangerous
00067   // and should be avoided.
00068   inline int
00069   __convert_from_v(const __c_locale& __cloc __attribute__ ((__unused__)),
00070            char* __out,
00071            const int __size __attribute__ ((__unused__)),
00072            const char* __fmt, ...)
00073   {
00074 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00075     __c_locale __old = __gnu_cxx::__uselocale(__cloc);
00076 #else
00077     char* __old = std::setlocale(LC_NUMERIC, 0);
00078     char* __sav = 0;
00079     if (__builtin_strcmp(__old, "C"))
00080       {
00081     const size_t __len = __builtin_strlen(__old) + 1;
00082     __sav = new char[__len];
00083     __builtin_memcpy(__sav, __old, __len);
00084     std::setlocale(LC_NUMERIC, "C");
00085       }
00086 #endif
00087 
00088     __builtin_va_list __args;
00089     __builtin_va_start(__args, __fmt);
00090 
00091 #ifdef _GLIBCXX_USE_C99
00092     const int __ret = __builtin_vsnprintf(__out, __size, __fmt, __args);
00093 #else
00094     const int __ret = __builtin_vsprintf(__out, __fmt, __args);
00095 #endif
00096 
00097     __builtin_va_end(__args);
00098 
00099 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
00100     __gnu_cxx::__uselocale(__old);
00101 #else
00102     if (__sav)
00103       {
00104     std::setlocale(LC_NUMERIC, __sav);
00105     delete [] __sav;
00106       }
00107 #endif
00108     return __ret;
00109   }
00110 
00111 _GLIBCXX_END_NAMESPACE_VERSION
00112 } // namespace
00113 
00114 #endif