CARMA C++
print.h
1 #ifndef print_h
2 #define print_h
3 
4 #ifdef VXW
5 #include <fioLib.h>
6 #endif
7 
8 /*
9  * This module provides a facility that parses printf-style formats and
10  * sequentially passes each converted argument and intervening string
11  * segments to a given caller-provided output function. The caller
12  * can also provide anonymous data to be passed to their output function
13  * via a (void *context) argument. Note that under vxWorks fioFormatV
14  * is substituted for print_format() so don't compile print.c under vxWorks.
15  */
16 
17 /*
18  * Under vxWorks fioFormatV() is substituted for print_format().
19  * fioFormatV() uses an int argument to pass application-specific
20  * context data, whereas print_format() uses (void *). The following
21  * typedef is an attempt to keep the two interfaces relatively compatible.
22  */
23 #ifdef VXW
24 typedef int PrintContext;
25 #else
26 typedef void *PrintContext;
27 #endif
28 
29 /*
30  * The caller-provided output function should be prototyped and declared
31  * with the following macro. The arguments of the function are:
32  *
33  * Input:
34  * buffer char * The rendered output of the latest converted
35  * argument or intervening string segment. This
36  * buffer is NOT '\0' terminated.
37  * nc int The number of characters to extract from nc.
38  * context PrintContext The context argument passed to print_format().
39  * Output:
40  * return int 0 - OK.
41  * 1 - An error occured. This tells print_format()
42  * to abort and return -1.
43  */
44 #define PRT_OUT_FN(fn) int (fn)(char *buffer, int nc, PrintContext context)
45 
46 /*.......................................................................
47  * This is the function that is responsible for parsing a format
48  * string and sending the print output to a caller provided output
49  * function. Its arguments are:
50  *
51  * Input:
52  * fmt char * The printf() format to use to convert the
53  * arguments in 'ap'.
54  * ap va_list The list of arguments to associate with fmt[].
55  * It is the responsibility of the caller to
56  * call va_start() before calling print_format(),
57  * and va_end() after print_format() returns.
58  * out_fn PRT_OUT_FN(*) The function to pass converted output to.
59  * context PrintContext A pointer to any data that the caller wishes
60  * to have access to within out_fn().
61  * Output:
62  * return int The number of bytes passed to out_fn(), or
63  * -1 on error.
64  */
65 #ifdef VXW
66 #define print_format fioFormatV
67 #else
68 int print_format(const char *fmt, va_list ap,
69  PRT_OUT_FN(*out_fn), PrintContext context);
70 #endif
71 
72 #endif