473,327 Members | 2,112 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,327 software developers and data experts.

vsprintf(char *str, const char *format, va_list ap, ...)?

My last post -
http://groups.google.com/group/comp....df6ab20a6007d0

I have a little bit more challenging question this time. Suppose I
have

write_cmd(struct ast_connection *conn, char *msg, ...);

I want to call it like (forgive the syntax)

write_cmd(conn, str, p1, p2, ..., pn, k1, k2, ..., km);

The parameters p1 to pn must be passed to write_cmd every time. The
parameters k1 to km are not actually constant, but they are accessible
from conn; so, I don't really need to pass them in. It would be better
to call

write_cmd(conn, str, p1, p2, ..., pn);

However, as noted in the last post, I want to print the parameters
using vsprintf, which means they all need to be in the list. I could
follow vsprintf with sprintf to get the rest of k1 to Km, and thus
roughly double both the buffer space and processing time. But this
seems silly given that I could get those parameters into the list by
calling the first write_cmd.

I have some code which obviously doesn't work. But you can probably
see what I'm aiming for. I could probably also do it with a vsprintf
that took a list and a variable number of parameters, if there were
such a thing...

#include <stdio.h>
#include <stdarg.h>

void vvfunc(char *msg, va_list argp, va_list argp2)
{
char buf[128];

vsprintf(buf,msg,argp); //argp2 goes here
puts(buf);
}

void vfunc2(char *msg, va_list argp, ...)
{
va_list argp2;

va_start(argp2,argp);
vvfunc(msg,argp,argp2);
va_end(argp2);
}

void func(char *msg, ...)
{
va_list argp;

va_start(argp,msg);
vfunc2(msg,argp,10); //k1
va_end(argp);
}

int main(void)
{
func("hello %s %d\n","there"); //p1
return 0;
}

May 14 '06 #1
4 3726
I don't think there's any way to do this portably. The only solution I
can think of is to implement your own vvsprintf() function which takes
two va_lists.

May 15 '06 #2

Je*********@gmail.com wrote:
I don't think there's any way to do this portably. The only solution I
can think of is to implement your own vvsprintf() function which takes
two va_lists.


Darn, that's kind of what I figured. I thought about implementing a
vvsprintf, but it's not worth it.

May 15 '06 #3

go******@signalsguru.net wrote:
My last post -
http://groups.google.com/group/comp....df6ab20a6007d0

I have a little bit more challenging question this time. Suppose I
have

write_cmd(struct ast_connection *conn, char *msg, ...);

I want to call it like (forgive the syntax)

write_cmd(conn, str, p1, p2, ..., pn, k1, k2, ..., km);


Variable lists are, well, variable. There would be no way to know where
the first variable list ends and the next one begins.

va_arg lists always have to have "..." as the last argument in the
function argument list.

-kyle

May 15 '06 #4
There is a portable way to do it.
Replace va_arg lists by arrays and use initializers to init. the arrays
before the function call:

write_cmd(Connection_t* conn, char* str,
PParameter_t p[], int p_size,
KParameter_t k[], int k_size) ;
....
KParameter_t k[] = {{INT,1}, {INT,2}};
....
write_cmd(conn, str, p, p_size, k, SIZEOF(k)) ;
....
See example:
#include <stdio.h>

#define SIZEOF(array) \
sizeof(array) / sizeof((array)[0])

/*NOTE: belowed struct/union(s) members' names make no sense.
They have taken just for example.
*/
typedef enum{
FAKE_ARRAY, INT, STRING
} Typecode; /* use the same Typecode type in both
PParameter and KParameter for simplicity */

typedef struct Connection{
/* ... */
int fake_int;
char* fake_pchar;
} Connection_t;
typedef struct PParameter {
Typecode type;
union{
/* ... */
int fake_array[2];
char* fake_pch;
} u;
} PParameter_t;

typedef struct KParameter {
Typecode type;
union{
/* ... */
int int_fake;
char* pchar_fake;
} u;
} KParameter_t;

void
write_cmd(Connection_t* conn, char* str,
PParameter_t p[], int p_size,
KParameter_t k[], int k_size) {
int i;

printf("str: \t%s\n", str);

/* print p[] */
printf("p: ");
for (i = 0; i < p_size; ++i)
switch(p[i].type) {
case STRING:
printf("\tstring: %s\n", p[i].u.fake_pch);
break;
case FAKE_ARRAY:{
int j;
printf("\tarray: ");
for(j = 0; j < SIZEOF(p[i].u.fake_array); ++j)
printf("%d, ", p[i].u.fake_array[j]);
printf("\n");
break;
}
};

/* print k[] */
printf("k: ");
for (i = 0; i < k_size; ++i)
switch(k[i].type) {
case INT:
printf("\tint: %d\n", k[i].u.int_fake);
break;
case STRING:
printf("\tstring: %s\n", k[i].u.pchar_fake);
break;
};
}
int main() {
Connection_t conn;
/* Tested on:

Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for
ONLINE_EVALUATION_BETA1
MODE:strict errors C99

And:

gcc.exe (GCC) 3.4.4 (mingw special)
default compiler settings
*/
PParameter_t p[] = {{STRING, .u.fake_pch = "a string"},
{FAKE_ARRAY, {1,10}},
{STRING, .u.fake_pch = "another string"}
}; /* C99 mode initializer used */
KParameter_t k[] = {{INT,1}, {INT,2}};

write_cmd(&conn, "just a string", p, SIZEOF(p), k, SIZEOF(k));
}

May 15 '06 #5

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

Similar topics

0
by: Chung Leong | last post by:
I was trying to think up a nice, simple solution to SQL injection while pondering my top ten vulnerability list. Here's something I came up with. Tell me what you think. function sql() { $args...
20
by: maria | last post by:
I 've decided it's time to move from fortran to c++, but i cant find out if c++ supposrts I/O format commands like the ones in fortran 77. I would be gratefull for any help. Thank you
5
by: John Guo | last post by:
Hi all, I am trying to nest two functions both with variable length arguments. (The only reason I nested them is that I called the outer function in too many places, I don't want to replace...
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
3
by: January Weiner | last post by:
Hello, I have the following problem. I have a custom fprintf-like function which is supposed to prepend each comment output line with a `%' if the output type is a Postscript document...
5
by: Thomas Rogg | last post by:
Hello NG, in my program I use the following function: void write_log(char *format, ...) { va_list arg; char txt; // Get string
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...
9
by: Craig | last post by:
Hello friends at comp.lang.c, I'm trying to combine 2 strings with a newline character at the end to write to a file. When I view the file, the messages run together and some of the str string...
5
by: www.douglassdavis.com | last post by:
I have an idea for preventing sql injection attacks, however it would have to be implemented by the database vendor. Let me know if I am on the right track, this totally off base, or already...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.