This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [PATCH,RFC] Change __convert_from_v to use snprintf and use it
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: Benjamin Kosnik <bkoz at redhat dot com>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Tue, 19 Mar 2002 02:18:42 +0100
- Subject: Re: [PATCH,RFC] Change __convert_from_v to use snprintf and use it
- References: <Pine.SOL.3.91.1020318165409.10216A-100000@taarna.cygnus.com>
Benjamin Kosnik wrote:
> > Thanks once more. I admit to be very ignorant about those matters. I'm indeed /very/ happy
> > to know that snprintf can be used when available (in the form standardized as part of C99,
> > indeed!)
>
> Yep.
Indeed an AC_TRY_COMPILE for snprintf is already part of the set of tests in acinclude.m4 for
_GLIBCPP_USE_C99.
Good.
However, I'm a bit nervous about this: what if _GLIBCPP_USE_C99 becomes true but in fact
snprintf is /not/ C99-conforming? In fact, I'm aware of pre-C99 snprintf which returned -1 in
case of too small buffer. This would be really bad.
Is it safe to assume that if _GLIBCPP_USE_C99 is defined then the libc is C99 conforming and
therefore snprintf will behave correctly at run time? (vs the -1 problem, I mean)
> Great! I look forward to seeing this.
One more bit of advice :-) ...
In my tree __convert_from_v is now tentatively as below. Notice the dummy if(__size); to
silence warnings about an unused parameter in case !C99. What do you suggest about this? Is
there a much cleaner way to solve the trouble with __size when !C99 ???
Thanks,
Paolo.
//////////////
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;
const char* __old = setlocale(LC_ALL, "C");
if (__prec >= 0)
#if defined(_GLIBCPP_USE_C99)
__ret = snprintf(__out, __size, __fmt, __prec, __v);
#else
__ret = sprintf(__out, __fmt, __prec, __v);
#endif
else
#if defined(_GLIBCPP_USE_C99)
__ret = snprintf(__out, __size, __fmt, __v);
#else
__ret = sprintf(__out, __fmt, __v);
#endif
if (__size); // Only to silence warnings.
setlocale(LC_ALL, __old);
return __ret;
}