Subject: Re: strformat() ?
To: Chris G. Demetriou <cgd@netbsd.org>
From: Manuel Bouyer <bouyer@antioche.lip6.fr>
List: tech-kern
Date: 09/10/1999 10:50:57
On Thu, Sep 09, 1999 at 11:15:13AM -0700, Chris G. Demetriou wrote:
> Manuel Bouyer <bouyer@antioche.lip6.fr> writes:
> > Before adding other formats to the corename stuff I wonder if it's worth
> > implemeting a generic function for this. Something like:
> > strformat(char *dst, size_t dstlen, const char *template, const char *format,
> >     ...);
> > A sample call would be here:
> > strformat(name, MAXPATHLEN, curproc->p_limit->pl_corename, "nup",
> >     "%s", curproc->p_comm,
> >     "%s", curproc->p_pgrp->pg_session->s_login,    
> >     "%d", curproc->p_pid);
> > That is, for each format we provide the snprintf format to convert the value,
> > and the value.
> > Would this be of general interest ?
> 
> it's not clear how useful/pretty this is, as-is, because of the way
> varargs works.
> 
> strformat() itself would have to understand all of the snprintf-format
> specifiers, in order to properly decode the variable arguments passed
> to it (to either handle them itself, or pass them off to snprintf).
> therefore, it makes me think you might have a better/more compact
> representation of the data that would do the right thing...

I confess I'm not a varags specialist, and this was just an idea, I didn't look
at implementation yet. Sure if we need to add knowledge of any snprintf format
in strformat() it's not worth it.
Maybe I should forget about passing a (snprintf format, arg) pair and just
pass strings as varargs.

About how usefull it is ... well it depend wether we need custom templates
at other places  too :)

> 
> it's also not going to be particularly efficient to format strings...
> something like:
> 
> 	i've got a %p in my input!  OK, it's the third type of format
> 	character i know about!
> 
> 	va_start...
> 
> 	get the first sprintf format with va_arg
> 	figure out what kind of argument goes with it
> 	advance past it with va_arg
> 
> 	get the second sprintf format with va_arg
> 	figure out what kind of argument goes with it
> 	advance past it with va_arg
> 	
> 	get the third sprintf format with va_arg
> 	figure out what kind of argument goes with it
> 	fetch it with va_arg
> 	format it directly or pass it to snprintf
> 
> 	va_end
> 
> 	repeat for next input...
> 
> seems to me that there's gotta be a better way, but maybe there's not.  8-)

My idea was to parse varargs at once and store the result in a temp array of
(format, string). I though vsnprintf() could advance va_arg  by itselfs.
But again I've never played with varargs before :)

--
Manuel Bouyer, LIP6, Universite Paris VI.           Manuel.Bouyer@lip6.fr
--