1 /* Abstract output stream data type.
2 Copyright (C) 2006, 2019 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _OSTREAM_H
19 #define _OSTREAM_H
20
21 #include <stdarg.h>
22 #include <stddef.h>
23 #include <string.h>
24
25 #include "moo.h"
26
27
28 /* Describes the scope of a flush operation. */
29 typedef enum
30 {
31 /* Flushes buffers in this ostream_t.
32 Use this value if you want to write to the underlying ostream_t. */
33 FLUSH_THIS_STREAM = 0,
34 /* Flushes all buffers in the current process.
35 Use this value if you want to write to the same target through a
36 different file descriptor or a FILE stream. */
37 FLUSH_THIS_PROCESS = 1,
38 /* Flushes buffers in the current process and attempts to flush the buffers
39 in the kernel.
40 Use this value so that some other process (or the kernel itself)
41 may write to the same target. */
42 FLUSH_ALL = 2
43 } ostream_flush_scope_t;
44
45
46 /* An output stream is an object to which one can feed a sequence of bytes. */
47
48 struct ostream
49 {
50 methods:
51
52 /* Write a sequence of bytes to a stream. */
53 void write_mem (ostream_t stream, const void *data, size_t len);
54
55 /* Bring buffered data to its destination. */
56 void flush (ostream_t stream, ostream_flush_scope_t scope);
57
58 /* Close and free a stream. */
59 void free (ostream_t stream);
60 };
61
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65
66 /* Write a string's contents to a stream. */
67 extern void ostream_write_str (ostream_t stream, const char *string);
68
69 /* Writes formatted output to a stream.
70 Returns the size of formatted output, or a negative value in case of an
71 error. */
72 extern ptrdiff_t ostream_printf (ostream_t stream, const char *format, ...)
73 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3
74 __attribute__ ((__format__ (__printf__, 2, 3)))
75 #endif
76 ;
77 extern ptrdiff_t ostream_vprintf (ostream_t stream,
78 const char *format, va_list args)
79 #if (__GNUC__ == 3 && __GNUC_MINOR__ >= 1) || __GNUC__ > 3
80 __attribute__ ((__format__ (__printf__, 2, 0)))
81 #endif
82 ;
83
84 #if HAVE_INLINE
85
86 #define ostream_write_str ostream_write_str_inline
87 static inline void
ostream_write_str(ostream_t stream,const char * string)88 ostream_write_str (ostream_t stream, const char *string)
89 {
90 ostream_write_mem (stream, string, strlen (string));
91 }
92
93 #endif
94
95 #ifdef __cplusplus
96 }
97 #endif
98
99 #endif /* _OSTREAM_H */
100