public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to best cope with variadic input
@ 2011-03-01  5:12 richardcavell
  2011-03-01  8:59 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: richardcavell @ 2011-03-01  5:12 UTC (permalink / raw)
  To: gcc-help

Hi everyone.  I've decided to learn C99, and so I'm passing -std=c99 to 
gcc.  I've decided to write a Wikipedia bot in C (don't laugh).

Owing partly to obsessive tendencies and partly due to the need for the 
bot to be robust in working unattended at high speed, I want to wrap 
printf in a wrapper that automatically detects when printf has failed 
and tells the user.  The idea is that it is called like this:

int res = safeprint ( __FILE__ , __LINE__ , "Blah" ) ;
if ( res < 0 ) // uh oh

  But printf has variadic input.  I am scratching my head wondering what 
is my best option:

1.  Make safeprint a variadic function  (I don't know how to do this)
2.  Turn on C++ and overload safeprint with different combinations of 
arguments
3.  Create multiple safeprint functions with different names, that take 
different combinations of arguments

Any advice for me?  TIA.

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

#include "Error.h"

static const unsigned int StringMax = 100 ;

int safeprint ( const char* file , const unsigned int line , const 
char* pstring )
{
    int ilength = strlen ( pstring ) ;
    int icharsprinted = printf ( "%s" , pstring ) ;
    if ( ilength != icharsprinted )
    {
        Error ( file , line ) ;
        return -1 ;
    }

    return 0 ;
}


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to best cope with variadic input
  2011-03-01  5:12 How to best cope with variadic input richardcavell
@ 2011-03-01  8:59 ` Jonathan Wakely
  2011-03-01 11:33   ` Maximilian Schneider
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2011-03-01  8:59 UTC (permalink / raw)
  To: richardcavell; +Cc: gcc-help

On 1 March 2011 05:11,  <richardcavell@mail.com> wrote:
> Hi everyone.  I've decided to learn C99, and so I'm passing -std=c99 to gcc.
>  I've decided to write a Wikipedia bot in C (don't laugh).
>
> Owing partly to obsessive tendencies and partly due to the need for the bot
> to be robust in working unattended at high speed, I want to wrap printf in a
> wrapper that automatically detects when printf has failed and tells the
> user.  The idea is that it is called like this:
>
> int res = safeprint ( __FILE__ , __LINE__ , "Blah" ) ;
> if ( res < 0 ) // uh oh
>
>  But printf has variadic input.  I am scratching my head wondering what is
> my best option:
>
> 1.  Make safeprint a variadic function  (I don't know how to do this)

Use va_list, va_start, va_arg and va_end, defined by:
#include <stdarg.h>

On GNU/Linux you should be able to say "man stdarg.h" to see usage examples.

> 2.  Turn on C++ and overload safeprint with different combinations of
> arguments

A better option would be to use a C++ variadic template.

> 3.  Create multiple safeprint functions with different names, that take
> different combinations of arguments

That would be inconvenient to use.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to best cope with variadic input
  2011-03-01  8:59 ` Jonathan Wakely
@ 2011-03-01 11:33   ` Maximilian Schneider
  2011-03-01 21:41     ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Maximilian Schneider @ 2011-03-01 11:33 UTC (permalink / raw)
  To: richardcavell; +Cc: gcc-help

On Tue, 2011-03-01 at 08:59 +0000, Jonathan Wakely wrote:
> On 1 March 2011 05:11,  <richardcavell@mail.com> wrote:
> > Hi everyone.  I've decided to learn C99, and so I'm passing -std=c99 to gcc.
> >  I've decided to write a Wikipedia bot in C (don't laugh).
> >
> > Owing partly to obsessive tendencies and partly due to the need for the bot
> > to be robust in working unattended at high speed, I want to wrap printf in a
> > wrapper that automatically detects when printf has failed and tells the
> > user.  The idea is that it is called like this:
> >
> > int res = safeprint ( __FILE__ , __LINE__ , "Blah" ) ;
> > if ( res < 0 ) // uh oh
> >
> >  But printf has variadic input.  I am scratching my head wondering what is
> > my best option:
> >
> > 1.  Make safeprint a variadic function  (I don't know how to do this)
> 
> Use va_list, va_start, va_arg and va_end, defined by:
> #include <stdarg.h>
> 
> On GNU/Linux you should be able to say "man stdarg.h" to see usage examples.
> 
> > 2.  Turn on C++ and overload safeprint with different combinations of
> > arguments
> 
> A better option would be to use a C++ variadic template.
> 
> > 3.  Create multiple safeprint functions with different names, that take
> > different combinations of arguments
> 
> That would be inconvenient to use.

I would have to agree with Jonathan here. In my experience with c++ (Qt)
overloading has only caused me trouble.

the va_list method is straight forward. Note that printf() even comes in
a va_list compatible form. vprintf()

Max S.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to best cope with variadic input
  2011-03-01 11:33   ` Maximilian Schneider
@ 2011-03-01 21:41     ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2011-03-01 21:41 UTC (permalink / raw)
  To: Maximilian Schneider; +Cc: richardcavell, gcc-help

On 1 March 2011 11:33, Maximilian Schneider wrote:
> On Tue, 2011-03-01 at 08:59 +0000, Jonathan Wakely wrote:
>> > 2.  Turn on C++ and overload safeprint with different combinations of
>> > arguments
>>
>> A better option would be to use a C++ variadic template.
[snip]
> I would have to agree with Jonathan here. In my experience with c++ (Qt)
> overloading has only caused me trouble.

I'm not sure we agree :)
I have no problem with using overloaded functions, but writing lots of
overloaded functions would be a headache.  A single variadic template
would do a better job than an entire set of overloaded functions, see
the printf template at
http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html

But I do agree that for this case, where the original code is in C,
simply using <stdarg.h> and vprintf is the most straightforward
solution.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-03-01 21:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-01  5:12 How to best cope with variadic input richardcavell
2011-03-01  8:59 ` Jonathan Wakely
2011-03-01 11:33   ` Maximilian Schneider
2011-03-01 21:41     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).