How to write a function to wrap snprintf?

M

miloody

Dear all:
I kept a buffer to handle the strings that output by snprintf and
tried to write a function to let the buffer works like a ring buffer.
That means the data will put at the head of buffer when buffer
overflow.
I think the prototype of SnPrintBuffer is correct but I have no idea
what should I pass to snprintf.
"snprintf (point, var1,fmt);" should be wrong.
If anyone knows the correct parameter to pass to snprintf, please let
me know.
Appreciate your help,
miloody

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

static unsigned int var1;
static unsigned int index2;
static char * point;
void SnPrintBuffer(const char *fmt, ...)
{
index2 = snprintf (point, var1,fmt);
var1 -= index2;
point += index2;

if(var1 < 2)
{ //handle overflow
}
}

int main(void)
{
point = malloc(4096);
var1 = 4096;
SnPrintBuffer("test 123 %d\n",__LINE__);
printf("%s",point);
}
 
E

Eric Sosman

[...]
I think the prototype of SnPrintBuffer is correct but I have no idea
what should I pass to snprintf.
"snprintf (point, var1,fmt);" should be wrong.
If anyone knows the correct parameter to pass to snprintf, please let
me know.

Use va_start() in SnPrintBuffer(), pass the va_list object to
vsnprintf() (note the "v"), and call va_end() when it returns.
 
M

miloody

hi

[...]
I think the prototype of SnPrintBuffer is correct but I have no idea
what should I pass to snprintf.
"snprintf (point, var1,fmt);" should be wrong.
If anyone knows the correct parameter to pass to snprintf, please let
me know.

     Use va_start() in SnPrintBuffer(), pass the va_list object to
vsnprintf() (note the "v"), and call va_end() when it returns.
Is it possible use some ways like the Marco that directly pass the
parameter from SnPrintBuffer without using va_start() ?
thanks for your help,
miloody
 
T

Thad Smith

hi

[...]
I think the prototype of SnPrintBuffer is correct but I have no idea
what should I pass to snprintf.
"snprintf (point, var1,fmt);" should be wrong.
If anyone knows the correct parameter to pass to snprintf, please let
me know.

Use va_start() in SnPrintBuffer(), pass the va_list object to
vsnprintf() (note the "v"), and call va_end() when it returns.
Is it possible use some ways like the Marco that directly pass the
parameter from SnPrintBuffer without using va_start() ?
thanks for your help,
miloody

No, macros don't help. Eric's method is the way provided to wrap functions
taking a variable number of arguments.
 
M

Malcolm McLean

No, macros don't help.  Eric's method is the way provided to wrap functions
taking a variable number of arguments.
Generally C allows you to get under the bonnet and do almost anything.
Constructing variable argument lists on the fly is an exception.
 
K

Kenny McCormack

Generally C allows you to get under the bonnet and do almost anything.
Constructing variable argument lists on the fly is an exception.

That used to be true - until the "what's in the standard and only what's
in the standard" Nazis took over this newsgroup. Now, all of the fun
stuff - the stuff that attracted us to C in the first place - is out of
bounds.

FWIW, you can certainly construct variable arg lists if you are willing
to go outside of the standard - Google for "avcall".
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top