Re: g_printf_string_upper_bound and positional parameters



Thomas Schultz <tststs gmx de> writes:

> Hi,
> 
> I needed a function like g_strdup_printf in slrn (a console mode
> newsreader), and decided to use a modified version of
> g_printf_string_upper_bound to implement it.
> 
> However, I now added gettext support and one of the translators (Byrial
> Jensen <byrial image dk>) complained that the code could not handle
> positional parameters.  

Yes, this is a problem, but note that positional parameters are
not at all portable. They are in the SUSv2, but aren't in POSIX
or ANSI C, and AFAIK aren't likely to go there.

So, portable code can't really use them anyways.

 http://bugzilla.gnome.org/show_bug?id=55106

> He suggested finding out the length of a format
> string by writing it to the null device instead, like this:
> 
> int printf_string_length (const char *format, va_list args)
> {
>    static FILE *null;
> 
>    if (!null)
>    {
>       null = fopen ("/dev/null", "w");
>       if (!null)
>          return -1; /* Add some error handling */
>    }
>    return vfprintf (null, format, args);
>    /* Leave null open. */
> }
> 
> To me, this sounds like a workable solution, but I guess there must be a
> reason why it isn't done this way in glib.  Any hints?

Speed, for one... using a file descriptor and system calls for this
is not something I'd want to do.

On some systems, using vsnprintf(NULL, 0, ...) will work fine, though
you need to do configure checks to make sure that this works. We
plan to this at some point in Glib.

> I tried to find information about this in the mailing list archive, but
> the search at <http://mail.gnome.org/archives/gtk-devel-list/> did not
> seem to work - I did not even get results when trying very basic keywords
> like "widget".

Yes, it's broken. :-(. Fixing it is waiting on a machine upgrade.

                                        Owen




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]