This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH] Use __uselocale in __convert_from_v
> Ah! I see...
like this....
2002-09-11 Benjamin Kosnik <bkoz@redhat.com>
* include/bits/locale_facets.tcc (__convert_from_v): Remove.
* config/locale/gnu/c_locale.h (__convert_from_v): Add.
* config/locale/generic/c_locale.h (__convert_from_v): Add.
2002-09-11 Paolo Carlini <pcarlini@unitus.it>
* include/bits/locale_facets.tcc (__convert_from_v):
Use __uselocale instead of setlocale for glibc 2.3+.
Index: config/locale/generic/c_locale.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/config/locale/generic/c_locale.h,v
retrieving revision 1.1
diff -c -p -r1.1 c_locale.h
*** config/locale/generic/c_locale.h 9 Mar 2002 02:16:32 -0000 1.1
--- config/locale/generic/c_locale.h 11 Sep 2002 04:20:18 -0000
***************
*** 1,6 ****
// Wrapper for underlying C-language localization -*- C++ -*-
! // Copyright (C) 2001 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
--- 1,6 ----
// Wrapper for underlying C-language localization -*- C++ -*-
! // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
***************
*** 38,41 ****
--- 38,68 ----
namespace std
{
typedef int* __c_locale;
+
+ template<typename _Tv>
+ int
+ __convert_from_v(char* __out, const int __size, const char* __fmt,
+ _Tv __v, const __c_locale&, int __prec = -1)
+ {
+ int __ret;
+ char* __old = setlocale(LC_ALL, NULL);
+ char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
+ if (__sav)
+ strcpy(__sav, __old);
+ setlocale(LC_ALL, "C");
+ #ifdef _GLIBCPP_USE_C99
+ if (__prec >= 0)
+ __ret = snprintf(__out, __size, __fmt, __prec, __v);
+ else
+ __ret = snprintf(__out, __size, __fmt, __v);
+ #else
+ if (__prec >= 0)
+ __ret = sprintf(__out, __fmt, __prec, __v);
+ else
+ __ret = sprintf(__out, __fmt, __v);
+ #endif
+ setlocale(LC_ALL, __sav);
+ free(__sav);
+ return __ret;
+ }
}
Index: config/locale/gnu/c_locale.h
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/config/locale/gnu/c_locale.h,v
retrieving revision 1.1
diff -c -p -r1.1 c_locale.h
*** config/locale/gnu/c_locale.h 9 Mar 2002 02:16:33 -0000 1.1
--- config/locale/gnu/c_locale.h 11 Sep 2002 04:20:18 -0000
***************
*** 1,6 ****
// Wrapper for underlying C-language localization -*- C++ -*-
! // Copyright (C) 2001 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
--- 1,6 ----
// Wrapper for underlying C-language localization -*- C++ -*-
! // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
***************
*** 43,46 ****
--- 43,83 ----
namespace std
{
typedef __locale_t __c_locale;
+
+ template<typename _Tv>
+ int
+ __convert_from_v(char* __out, const int __size, const char* __fmt,
+ _Tv __v, const __c_locale& __cloc, int __prec = -1)
+ {
+ int __ret;
+ #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
+ __c_locale __old = __uselocale(__cloc);
+ #else
+ char* __old = setlocale(LC_ALL, NULL);
+ char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
+ if (__sav)
+ strcpy(__sav, __old);
+ setlocale(LC_ALL, "C");
+ #endif
+
+ #ifdef _GLIBCPP_USE_C99
+ if (__prec >= 0)
+ __ret = snprintf(__out, __size, __fmt, __prec, __v);
+ else
+ __ret = snprintf(__out, __size, __fmt, __v);
+ #else
+ if (__prec >= 0)
+ __ret = sprintf(__out, __fmt, __prec, __v);
+ else
+ __ret = sprintf(__out, __fmt, __v);
+ #endif
+
+ #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ > 2)
+ __uselocale(__old);
+ #else
+ setlocale(LC_ALL, __sav);
+ free(__sav);
+ #endif
+ return __ret;
+ }
}
Index: include/bits/locale_facets.tcc
===================================================================
RCS file: /cvs/gcc/gcc/libstdc++-v3/include/bits/locale_facets.tcc,v
retrieving revision 1.79
diff -c -p -r1.79 locale_facets.tcc
*** include/bits/locale_facets.tcc 10 Sep 2002 02:41:54 -0000 1.79
--- include/bits/locale_facets.tcc 11 Sep 2002 04:20:22 -0000
*************** namespace std
*** 1969,2015 ****
// Convert numeric value of type _Tv to string and return length of string.
// If snprintf is available use it, otherwise fall back to the unsafe sprintf
// which, in general, can be dangerous and should be avoided.
- #ifdef _GLIBCPP_USE_C99
template<typename _Tv>
int
__convert_from_v(char* __out, const int __size, const char* __fmt,
! _Tv __v, const __c_locale&, int __prec = -1)
! {
! int __ret;
! char* __old = setlocale(LC_ALL, NULL);
! char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
! if (__sav)
! strcpy(__sav, __old);
! setlocale(LC_ALL, "C");
! if (__prec >= 0)
! __ret = snprintf(__out, __size, __fmt, __prec, __v);
! else
! __ret = snprintf(__out, __size, __fmt, __v);
! setlocale(LC_ALL, __sav);
! free(__sav);
! return __ret;
! }
! #else
! template<typename _Tv>
! int
! __convert_from_v(char* __out, const int, const char* __fmt, _Tv __v,
! const __c_locale&, int __prec = -1)
! {
! int __ret;
! char* __old = setlocale(LC_ALL, NULL);
! char* __sav = static_cast<char*>(malloc(strlen(__old) + 1));
! if (__sav)
! strcpy(__sav, __old);
! setlocale(LC_ALL, "C");
! if (__prec >= 0)
! __ret = sprintf(__out, __fmt, __prec, __v);
! else
! __ret = sprintf(__out, __fmt, __v);
! setlocale(LC_ALL, __sav);
! free(__sav);
! return __ret;
! }
! #endif
// Construct correctly padded string, as per 22.2.2.2.2
// Assumes
--- 1969,1978 ----
// Convert numeric value of type _Tv to string and return length of string.
// If snprintf is available use it, otherwise fall back to the unsafe sprintf
// which, in general, can be dangerous and should be avoided.
template<typename _Tv>
int
__convert_from_v(char* __out, const int __size, const char* __fmt,
! _Tv __v, const __c_locale&, int __prec = -1);
// Construct correctly padded string, as per 22.2.2.2.2
// Assumes