OSDN Git Service

2006-06-12 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / config / locale / gnu / c_locale.h
1 // Wrapper for underlying C-language localization -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 22.8  Standard locale categories.
33 //
34
35 // Written by Benjamin Kosnik <bkoz@redhat.com>
36
37 #ifndef _C_LOCALE_H
38 #define _C_LOCALE_H 1
39
40 #pragma GCC system_header
41
42 #include <cstring>              // get std::strlen
43 #include <cstdio>               // get std::vsnprintf or std::vsprintf
44 #include <clocale>
45 #include <langinfo.h>           // For codecvt
46 #include <iconv.h>              // For codecvt using iconv, iconv_t
47 #include <libintl.h>            // For messages
48 #include <cstdarg>
49
50 #define _GLIBCXX_C_LOCALE_GNU 1
51
52 #define _GLIBCXX_NUM_CATEGORIES 6
53
54 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
55 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
56
57   extern "C" __typeof(uselocale) __uselocale;
58
59 _GLIBCXX_END_NAMESPACE
60 #endif
61
62 _GLIBCXX_BEGIN_NAMESPACE(std)
63
64   typedef __locale_t            __c_locale;
65
66   // Convert numeric value of type double and long double to string and
67   // return length of string.  If vsnprintf is available use it, otherwise
68   // fall back to the unsafe vsprintf which, in general, can be dangerous
69   // and should be avoided.
70   inline int
71   __convert_from_v(const __c_locale& __cloc __attribute__ ((__unused__)),
72                    char* __out,
73                    const int __size __attribute__ ((__unused__)),
74                    const char* __fmt, ...)
75   {
76 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
77     __c_locale __old = __gnu_cxx::__uselocale(__cloc);
78 #else
79     char* __old = std::setlocale(LC_ALL, NULL);
80     char* __sav = new char[std::strlen(__old) + 1];
81     std::strcpy(__sav, __old);
82     std::setlocale(LC_ALL, "C");
83 #endif
84
85     va_list __args;
86     va_start(__args, __fmt);
87
88 #ifdef _GLIBCXX_USE_C99
89     const int __ret = std::vsnprintf(__out, __size, __fmt, __args);
90 #else
91     const int __ret = std::vsprintf(__out, __fmt, __args);
92 #endif
93
94     va_end(__args);
95
96 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
97     __gnu_cxx::__uselocale(__old);
98 #else
99     std::setlocale(LC_ALL, __sav);
100     delete [] __sav;
101 #endif
102     return __ret;
103   }
104
105 _GLIBCXX_END_NAMESPACE
106
107 #endif