]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/config/locale/generic/c_locale.h
re PR libstdc++/26970 (-O3 -Wformat=2 complains about floats written to ostream)
[thirdparty/gcc.git] / libstdc++-v3 / config / locale / generic / 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 <clocale>
43 #include <cstring> // get std::strlen
44 #include <cstdio> // get std::vsnprintf or std::vsprintf
45 #include <cstdarg>
46
47 #define _GLIBCXX_NUM_CATEGORIES 0
48
49 _GLIBCXX_BEGIN_NAMESPACE(std)
50
51 typedef int* __c_locale;
52
53 // Convert numeric value of type double and long double to string and
54 // return length of string. If vsnprintf is available use it, otherwise
55 // fall back to the unsafe vsprintf which, in general, can be dangerous
56 // and should be avoided.
57 inline int
58 __convert_from_v(const __c_locale&, char* __out,
59 const int __size __attribute__((__unused__)),
60 const char* __fmt, ...)
61 {
62 char* __old = std::setlocale(LC_NUMERIC, NULL);
63 char* __sav = NULL;
64 if (std::strcmp(__old, "C"))
65 {
66 __sav = new char[std::strlen(__old) + 1];
67 std::strcpy(__sav, __old);
68 std::setlocale(LC_NUMERIC, "C");
69 }
70
71 va_list __args;
72 va_start(__args, __fmt);
73
74 #ifdef _GLIBCXX_USE_C99
75 const int __ret = std::vsnprintf(__out, __size, __fmt, __args);
76 #else
77 const int __ret = std::vsprintf(__out, __fmt, __args);
78 #endif
79
80 va_end(__args);
81
82 if (__sav)
83 {
84 std::setlocale(LC_NUMERIC, __sav);
85 delete [] __sav;
86 }
87 return __ret;
88 }
89
90 _GLIBCXX_END_NAMESPACE
91
92 #endif