Creating an argument list for sprintf

O

OneLG13

I have a program that uses a scripting language that does not support formating
strings with a printf syntax. Thus, I am writing a plugin to add the
functionality. The entry point and argument passing of the scripting language
to my plugin does not use standard c va_arg/list arguments for arbitrary number
of arguments but a c++ container object to achieve a similar approach.
I thought I could just construct my own array with the arguments (by looking
for the % specifications in the format string provided) and then just pass it
to sprintf, like:

char ret[1024];
sprintf( ret, fmt, args );

Where fmt is the string the user provided to be used as format, and args is the
list of arguments (a mix of ints, doubles, char*, etc. stored in a char* array
(?) ).
However, I am unsure how to create this list of options. That is, is there a
way I can safely create a va_list of arguments manually? And do it portably so
that the code works on linux/win32?
I can hack something that will work for a limited number of arguments, but I am
looking for something nicer that would allow any arbitrary number of arguments.
Any suggestions appreciated.
 
B

Ben Pfaff

However, I am unsure how to create this list of options. That
is, is there a way I can safely create a va_list of arguments
manually? And do it portably so that the code works on
linux/win32?

There's no way to do this portably. You're probably better off
writing your own function to parse sprintf()-like format
specifiers. It shouldn't be too hard, because you can call down
to the actual sprintf() function to format each individual format
specification.
 
G

Grumble

OneLG13 said:
I have a program that uses a scripting language that does not support formating
strings with a printf syntax. Thus, I am writing a plugin to add the
functionality. The entry point and argument passing of the scripting language
to my plugin does not use standard c va_arg/list arguments for arbitrary number
of arguments but a c++ container object to achieve a similar approach.
I thought I could just construct my own array with the arguments (by looking
for the % specifications in the format string provided) and then just pass it
to sprintf, like:

char ret[1024];
sprintf( ret, fmt, args );

Where fmt is the string the user provided to be used as format, and args is the
list of arguments (a mix of ints, doubles, char*, etc. stored in a char* array
(?) ).
However, I am unsure how to create this list of options. That is, is there a
way I can safely create a va_list of arguments manually? And do it portably so
that the code works on linux/win32?
I can hack something that will work for a limited number of arguments, but I am
looking for something nicer that would allow any arbitrary number of arguments.
Any suggestions appreciated.

I asked a related question recently.
http://www.google.com/[email protected]

Perhaps you can glean useful information from the subsequent thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top