473,387 Members | 1,641 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,387 software developers and data experts.

altering an av_list variable

When having optional function arguments, the extra arguments are accessed
via an av_list structure. While traversing is trivial, changing the value
isnt.

<code>
void my_sprintf (char *dest, size_t size, const char *format, ...)
{
<snip>
va_list argptr;
va_start(argptr, format);
while((pos = strcspn(format,"%")) != max) {
switch(format[pos+1]) {
case 'm':
format[pos+1] = 's';
argval = va_arg(cpy,int);
/* change the argval to a char* */
break;
default:
va_arg(cpy,int);
}
}
sprintf(*dest, const char *format, argptr); /* call the std function */
va_end(argptr);
}
</code>

In this example you see an attempt to make the sprintf function also
recognise the "%m" format. It should retrieve the (int) argument and change
its value.

Is it possible to alter the arguments in the va_list at all? Are there other
tricks to do this without rewriting sprintf completely?
Sep 14 '07 #1
4 1713
Punkie wrote:

[...]
Are there other tricks to do this without rewriting sprintf completely?
See

http://www.ijs.si/software/snprintf/
A quick and non-portable way, is getting the length via:

null = fopen("/dev/null", "w"); /* UNIX/Linux*/
null = fopen("NUL", "w"); /* Windows */

and then calling

len = vfprintf(null, format, ap);
Note, vfprintf() return int, not size_t.

--
Tor <torust [at] online [dot] no>
Sep 14 '07 #2
Indeed, a good way to do this without rewriting sprintf is to let *others*
rewrite it. However something i didnt find among them is typechecking.

Any sprintf with the same std formatting can use the attributes to defer
typechecking too. Like:
void my_sprintf (char *dest, size_t size, const char *format, ...)
__attribute__((format(printf, 3, 4)));

Altering the types accepted creates a problem as we cant use the attribute
anymore.
2routes to solve this:
*own sprintf implementation, that also does this checking somehow. hmmm but
how?
*filter the format and va_list. Like in the code I presented earlier,
changing the type or eliminaing the arg from the va_list.

Tor Rustad wrote:
Punkie wrote:

[...]
>Are there other tricks to do this without rewriting sprintf completely?

See

http://www.ijs.si/software/snprintf/
A quick and non-portable way, is getting the length via:

null = fopen("/dev/null", "w"); /* UNIX/Linux*/
null = fopen("NUL", "w"); /* Windows */

and then calling

len = vfprintf(null, format, ap);
Note, vfprintf() return int, not size_t.
Sep 14 '07 #3
In article <QE**********************@phobos.telenet-ops.be>,
Punkie <pu****@hotmail.comwrote:
>When having optional function arguments, the extra arguments are accessed
via an av_list structure. While traversing is trivial, changing the value
isnt.
"av_list" and "va_list" are very different things. The people here will
not talk to you about the former.

Sep 14 '07 #4
Punkie wrote:

Please don't top-post.
Tor Rustad wrote:
>Punkie wrote:

[...]
>>Are there other tricks to do this without rewriting sprintf completely?
See

http://www.ijs.si/software/snprintf/
A quick and non-portable way, is getting the length via:

null = fopen("/dev/null", "w"); /* UNIX/Linux*/
null = fopen("NUL", "w"); /* Windows */

and then calling

len = vfprintf(null, format, ap);
Note, vfprintf() return int, not size_t.
Indeed, a good way to do this without rewriting sprintf is to let *others*
rewrite it. However something i didnt find among them is typechecking.

Any sprintf with the same std formatting can use the attributes to defer
typechecking too. Like:
void my_sprintf (char *dest, size_t size, const char *format, ...)
__attribute__((format(printf, 3, 4)));
It should be noted that __attribute__ is a (very handy) gcc extension.

--
Ian Collins.
Sep 14 '07 #5

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

Similar topics

1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
7
by: vegetax | last post by:
I i need a decorator that adds a local variable in the function it decorates, probably related with nested scopes, for example: def dec(func): def wrapper(obj = None): if not obj : obj = Obj()...
4
by: fake | last post by:
What exactly does the line: rGfxOptionsWnd* m_pGfxOptionsWnd; do? I have chased this bug and narrowed it down to something that I believe should not happen and I don't even know what could...
27
by: C Gillespie | last post by:
Dear All, Hopefully I have a simple problem. Basically, I just want to alter some text with JS. Here is some of my test code: <snip> <script type="text/javascript"> var tmp='a';
3
by: Scott | last post by:
I am trying to alter the ForeColor of a TextBox object so that parts of the displayed text are written in various colors. For example, when writing to the TextBox I wish to display parts of the...
28
by: John Salerno | last post by:
What is the best way of altering something (in my case, a file) while you are iterating over it? I've tried this before by accident and got an error, naturally. I'm trying to read the lines of a...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
1
by: zufie | last post by:
Hi, I want to specifying a foreign key by altering a table. First, I create an ORDERS table without specifying a foreign key. Here is my code: CREATE TABLE ORDERS (Order_ID integer,...
0
by: Jen4003 | last post by:
Is it possible to alter the startUpPosition of a form at runtime?? If not, does any one have any idea how I can solve the following problem... I need a new form to appear in one of two...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.