--- muppet wrote: 
> writing your own printf-style macros is actually a very common idiom 
> for getting around this sort of technical problem.  they, of course, 
> raise the further problem that GCC's vararg macros are not portable, 
> and C99's vararg macros are not yet universally supported.

Agreed. I missed that __VA_ARGS__ earlier; that won't work on many
current C compilers.

In an attempt to do varargs ok() without the magical __VA_ARGS__,
I've come up with two little example solutions shown below.
Improvements welcome.

/* Sound version but with ugly XX symbol ----------------- */

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

#define XX  __FILE__,__LINE__

void ok(int cond, const char* file, unsigned long line, const char* fmt, ...) {
    va_list t;
    printf("cond:%d file:%s line:%lu, ", cond, file, line);
    va_start(t, fmt);
    vprintf(fmt, t);
    va_end(t);
}

int main() {
    ok(1==1, XX, "test %s %d\n", "one eq one",  42);
    ok(0==1, XX, "test %s %d\n", "zero eq one", 43);
    return 0;
}

/* Horrible hacky thread-unsafe version but no XX -------- */

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

#define ok  ok_save(__FILE__, __LINE__)
typedef void (*ok_t)(int, const char*, ...);

static const char* g_file;
static unsigned long g_line;

void gr(int cond, const char* fmt, ...) {
    va_list t;
    printf("cond:%d file:%s line:%lu, ", cond, g_file, g_line);
    va_start(t, fmt);
    vprintf(fmt, t);
    va_end(t);
}
ok_t ok_save(const char* file, unsigned long line) {
    g_file = file;
    g_line = line;
    return gr;
}

int main() {
    ok(1==1, "test %s %d\n", "one eq one",  42);
    ok(0==1, "test %s %d\n", "zero eq one", 43);
    return 0;
}

/* ------------------------------------------------------- */

/-\


Find local movie times and trailers on Yahoo! Movies.
http://au.movies.yahoo.com

Reply via email to