Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to write a function to wrap snprintf?

Reply
Thread Tools

How to write a function to wrap snprintf?

 
 
miloody
Guest
Posts: n/a
 
      02-06-2011
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);
}
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-06-2011
On 2/6/2011 9:10 AM, miloody wrote:
> [...]
> 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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
miloody
Guest
Posts: n/a
 
      02-06-2011
hi

On Feb 6, 11:08*pm, Eric Sosman <(E-Mail Removed)> wrote:
> On 2/6/2011 9:10 AM, miloody wrote:
>
> > [...]
> > 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
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      02-06-2011
On 2/6/2011 8:13 AM, miloody wrote:
> hi
>
> On Feb 6, 11:08 pm, Eric Sosman<(E-Mail Removed)> wrote:
>> On 2/6/2011 9:10 AM, miloody wrote:
>>
>>> [...]
>>> 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.

--
Thad
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      02-06-2011
On Feb 6, 5:56*pm, Thad Smith <(E-Mail Removed)> wrote:
>
> 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.



 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      02-06-2011
In article <(E-Mail Removed)>,
Malcolm McLean <(E-Mail Removed)> wrote:
>On Feb 6, 5:56*pm, Thad Smith <(E-Mail Removed)> wrote:
>>
>> 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.


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".

--
"We should always be disposed to believe that which appears to us to be
white is really black, if the hierarchy of the church so decides."

- Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
To wrap or not to wrap? Aaron Fude Java 12 05-10-2008 06:33 PM
Wrap computer components in bubble wrap? Ickshka Computer Support 7 05-05-2006 05:54 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 1 09-25-2003 06:15 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 0 09-22-2003 02:36 PM



Advertisments