473,414 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Creating an argument list for sprintf

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.

Nov 14 '05 #1
2 2546
on*****@aol.com (OneLG13) writes:
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.
--
Just another C hacker.
Nov 14 '05 #2
OneLG13 wrote:
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/gr*************************@news-rocq.inria.fr

Perhaps you can glean useful information from the subsequent thread.

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ANDERS FLODERUS | last post by:
Is it possible to transfer a variable length argument list? What I want to do is just void myPrintf( char *pFormat, ... ) { char buf; sprintf( buf, pFormat, ... );
2
by: George Marsaglia | last post by:
I have a set of, say, 2000 points in the 8-dimensional simplex S={(x_1,x_2,...,x_8),x_1+x_2+...+x_8=1, x's>=0}. To help analyze that 8-dimensional set, I wish to project the set of points onto...
5
by: P. Hari Krishna | last post by:
Hi, I have a situation where I do not know the number of arguments I pass to a function at compile time. So is there any way, that we can programmatically create a "va_list" and then pass it to...
1
by: Ben Kial | last post by:
I'd like to write a wrapper function "mysprintf(char *buffer, char *format, ....)" which calls sprintf(). My question is how can I pass variable argument in mysprintf() to sprintf(). Thanks in...
5
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
6
by: Adam | last post by:
Hey, Just a quick appeal for suggestions re some code, able to pass varying numbers of arguments... I have a function which can accept varying numbers of arguments. It in turn calls a...
19
by: Spiros Bousbouras | last post by:
Every time I've seen an example of a variable argument list function its functionality was to print formatted output. Does anyone have examples where the function is not some variation of printf ?
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.