Initial revision
[libfirm] / ir / common / xp_help.h
1 /* Macros to help writing output handlers.
2    Copyright (C) 1996 Markus Armbruster */
3
4 #ifndef _XP_HELP_H
5 #define _XP_HELP_H
6
7 # ifdef HAVE_STRING_H
8 #  include <string.h>
9 # endif
10 #ifdef USE_PRINTF
11 # define XP_ARG1 stream
12 # define XP_ARGN args
13 # define XP_GETARG(type, index) (*(type *)args[index])
14 # define XPMR(p,n) fwrite ((p), 1, (n), stream)
15 # define XPR(p) fputs ((p), stream)
16 # define XPF1R(fmt, arg) fprintf (stream, (fmt), (arg))
17 # define XPCR(p) (putc (*(p), stream) == EOF ? -1 : 1)
18 #else /* !USE_PRINTF */
19 # define XP_ARG1 f, a
20 # define XP_ARGN ap
21 # define XP_GETARG(type, index) va_arg (*ap, type)
22 # define XPMR(p,n) f (a, (p), (n))
23 # define XPR(p) f (a, (p), strlen((p)))
24 # define XPF1R(fmt, arg) xgprintf (f, a, (fmt), (arg))
25 # define XPCR(p) XPMR (p, 1)
26 #endif /* !USE_PRINTF */
27
28 #define XP(p) XP_CHK (XPR ((p)))
29 #define XPM(p,n) XP_CHK (XPMR ((p), (n)))
30 #define XPSR(p) XPMR ((p), sizeof(p)-1)
31 #define XPS(p) XPM ((p), sizeof(p)-1)
32 #define XPF1(fmt, arg) XP_CHK (XPF1R ((fmt), (arg)))
33 #define XPC(c) XP_CHK (XPCR ((c)))
34 #define XP_CHK(expr)                            \
35   do {                                          \
36     int n = (expr);                             \
37     if (n < 0) return -1; else printed += n;    \
38   } while (0)
39
40 #endif