CARMA C++
output.h
1 #ifndef output_h
2 #define output_h
3 
4 #include <stdarg.h>
5 #include <stdio.h>
6 
7 /* Declare the tag for a generic output stream. */
8 
9 typedef struct OutputStream OutputStream;
10 
11 /* List iterator-specific method function declarations */
12 
13 /*
14  * Write a given number of characters to an output stream.
15  *
16  * Input:
17  * stream OutputStream * The output stream to write to.
18  * text const char * A string to be written to the stream.
19  * Output:
20  * return int 0 - OK.
21  * 1 - Error.
22  */
23 #define OUTPUT_WRITE_FN(fn) int (fn)(OutputStream *stream, const char *text)
24 
25 /*
26  * Delete a stream context (as recorded in OutputStream::data).
27  *
28  * Input:
29  * data void * The context data to be deleted.
30  * Output:
31  * return void * The deleted context data (always NULL).
32  */
33 #define OUTPUT_DEL_FN(fn) void *(fn)(void *data)
34 
35 /* Set the maximum length of any output token */
36 
37 enum {OUTPUT_WORKLEN=1024};
38 
39 /* Declare a generic output stream container */
40 
41 struct OutputStream {
42  OUTPUT_WRITE_FN(*write_fn); /* Function to write a string to the stream */
43  OUTPUT_DEL_FN(*del_fn); /* Iterator destructor */
44  char work[OUTPUT_WORKLEN]; /* Work buffer for encoding lexical components */
45  void *data; /* Type-specific data */
46  bool interactive;
47 };
48 
49 /*
50  * Construct an initially closed output stream, ready to be connected to
51  * an output sink with open_OutputStream().
52  */
53 OutputStream *new_OutputStream(void);
54 
55 /*
56  * Connect an output stream to a specific output sink. This function
57  * should only be called from type-specific stream open_*()
58  * functions (see below).
59  */
60 int open_OutputStream(OutputStream *stream, void *data,
61  OUTPUT_WRITE_FN(*write_fn), OUTPUT_DEL_FN(*del_fn));
62 
63 /*
64  * Close an output stream. Future writes will return with an error
65  * until the stream is successfully re-opened to another output sink.
66  */
67 void close_OutputStream(OutputStream *stream);
68 
69 /*
70  * Declare constructors for provided output stream iterators.
71  */
72 int open_FileOutputStream(OutputStream *stream, char *dir, char *name);
73 int open_StringOutputStream(OutputStream *stream, int truncate,
74  char *buffer, size_t size);
75 int open_StdioOutputStream(OutputStream *stream, int do_close, FILE *fp);
76 int open_LprintfOutputStream(OutputStream *stream, FILE *fp);
77 int open_StdoutStream(OutputStream *stream);
78 int open_StderrStream(OutputStream *stream);
79 
80 /*
81  * Declare a generic stream destructor.
82  */
83 OutputStream *del_OutputStream(OutputStream *stream);
84 
85 /*
86  * Write a '\0' terminated string to an output stream.
87  */
88 int write_OutputStream(OutputStream *stream, const char *s);
89 
90 /*
91  * Write an array of n characters to an output stream.
92  */
93 int nwrite_OutputStream(OutputStream *stream, const char *s, size_t n);
94 
95 /*
96  * Reopen a string output stream to a new line to be composed.
97  */
98 int clr_StringOutputStream(OutputStream *stream);
99 
100 /*
101  * Write a quoted string to the specified stream. This includes
102  * prepending and postpending " characters and the conversion of
103  * unprintable characters and embedded " characters to their equivalent
104  * C escape sequences (eg. tab -> \t).
105  */
106 int output_quoted_string(OutputStream *stream, char *string);
107 
108 typedef enum {
109  ET_BELL=1, /* \a - Bell */
110  ET_BS=2, /* \b - Backspace */
111  ET_FF=4, /* \f - Form feed */
112  ET_NL=8, /* \n - New line */
113  ET_CR=16, /* \r - Carriage return */
114  ET_HT=32, /* \t - Horizontal Tab */
115  ET_VT=64, /* \v - Vertical Tab */
116  ET_ESC=128, /* \\ - Backslash */
117  ET_QUOTE=256, /* \' - Single quotes */
118  ET_SPEECH=512, /* \" - Double quotes */
119  ET_OTHER=1024, /* \0nn - Other unprintable characters */
120 /*
121  * All of the above.
122  */
123  ET_ALL = (ET_BELL | ET_BS | ET_FF | ET_NL | ET_CR | ET_HT | ET_VT | ET_ESC |
124  ET_QUOTE | ET_SPEECH | ET_OTHER),
125 /*
126  * None of the above.
127  */
128  ET_NONE=0
129 } EscapeType;
130 
131 /*
132  * Output an unquoted string, optionally with selected unprintable
133  * and white-space characters displayed as escape sequences. The
134  * filter argument should be a bitwise union of EscapeType enumerators,
135  * with each bit specifying a character code that should be displayed
136  * as a C-style escape sequence. The flags[] argument should be an array
137  * of printf-style flags, of which only the '-' flag is used.
138  * If the output string doesn't take min_width characters then it will
139  * be padded with spaces. If the '-' flag has been specified trailing
140  * spaces will be added, otherwise leading spaces will be used. If the
141  * string takes more than max_width characters before padding then it
142  * will be truncated unless max_width is zero.
143  */
144 int output_string(OutputStream *stream, unsigned filter, char *flags,
145  unsigned min_width, int max_width, int max_char,
146  char *string);
147 
148 /*
149  * The following functions are OutputStream equivalents to fprintf()
150  * and vfprintf().
151  */
152 int output_printf(OutputStream *stream, const char *fmt, ...);
153 int output_vprintf(OutputStream *stream, const char *fmt, va_list ap);
154 
155 /*
156  * Write a long int in a given base.
157  */
158 typedef enum {
159  OUT_BINARY = 2,
160  OUT_OCTAL = 8,
161  OUT_DECIMAL = 10,
162  OUT_HEX = 16
163 } OutputBase;
164 
165 int output_long(OutputStream *stream, OutputBase base, char *flags, int width,
166  int precision, long lval);
167 int output_ulong(OutputStream *stream, OutputBase base, char *flags, int width,
168  int precision, unsigned long ulval);
169 int output_double(OutputStream *stream, char *flags, int width, int precision,
170  char type, double dval);
171 
172 int output_sexagesimal(OutputStream *stream, char *flags, int width,
173  int ninteger, int precision, double number);
174 int output_date(OutputStream *stream, char *flags, int width,
175  int day, int month, int year);
176 int output_spaces(OutputStream *stream, int n);
177 int output_zeros(OutputStream *stream, int n);
178 int output_interval(OutputStream *stream, char *flags, int width,
179  int precision, double interval);
180 
181 #endif