ia64/xen-unstable

view tools/libxutil/iostream.h @ 2621:9402048e2325

bitkeeper revision 1.1159.1.218 (416a8128OiHXHyk_Sy8FsA0YUQcEnA)

Merge freefall.cl.cam.ac.uk:/auto/groups/xeno/users/cl349/BK/xeno.bk-26dom0
into freefall.cl.cam.ac.uk:/local/scratch/cl349/xeno.bk-26dom0
author cl349@freefall.cl.cam.ac.uk
date Mon Oct 11 12:48:40 2004 +0000 (2004-10-11)
parents 236a9f2698a3
children beb02da3f69f 42bdac6c8985 0a4b76b6b5a0
line source
1 #ifndef _XC_LINUX_SAVE_H_
2 #define _XC_LINUX_SAVE_H_
4 #include <stdarg.h>
5 #include <stdint.h>
6 #include <stddef.h>
8 #ifdef __KERNEL__
9 #include <linux/errno.h>
10 #else
11 #include <errno.h>
12 #endif
14 #include "allocate.h"
16 /** End of input return value. */
17 #define IOSTREAM_EOF -1
19 /** An input/output abstraction.
20 */
21 typedef struct IOStream IOStream;
23 /** Record of the functions to use for operations on an
24 * IOStream implementation.
25 */
26 typedef struct IOMethods {
27 /** Read function. Called with the user data, buffer to read into
28 * and number of bytes to read. Must return number of bytes read
29 * on success, less than zero on error.
30 */
31 int (*read)(IOStream *stream, void *buf, size_t n);
33 /** Write function. Called with user data, buffer to write and
34 * number of bytes to write. Must return number of bytes written on
35 * success, less than zero otherwise.
36 */
37 int (*write)(IOStream *stream, const void *buf, size_t n);
39 int (*flush)(IOStream *s);
41 int (*error)(IOStream *s);
43 int (*close)(IOStream *s);
45 void (*free)(IOStream *s);
47 void (*lock)(IOStream *s);
48 void (*unlock)(IOStream *s);
50 } IOMethods;
52 /** Abstract i/o object.
53 */
54 struct IOStream {
55 /** Methods to use to implement operations. */
56 const IOMethods *methods;
57 /** Private state for the implementation. */
58 const void *data;
59 /** Flag indicating whether the stream is closed. */
60 int closed;
61 /** Number of bytes written. */
62 int written;
63 /** Number of bytes read. */
64 int read;
65 };
68 /** IOStream version of stdin. */
69 extern IOStream *iostdin;
71 /** IOStream version of stdout, */
72 extern IOStream *iostdout;
74 /** IOStream version of stderr. */
75 extern IOStream *iostderr;
77 extern int IOStream_print(IOStream *io, const char *format, ...);
78 extern int IOStream_vprint(IOStream *io, const char *format, va_list args);
80 /** Read from a stream.
81 *
82 * @param stream input
83 * @param buf where to put input
84 * @param n number of bytes to read
85 * @return if ok, number of bytes read, otherwise negative error code
86 */
87 static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
88 int result = 0;
89 if(stream->closed) goto exit;
90 if(!stream->methods || !stream->methods->read){
91 result = -EINVAL;
92 goto exit;
93 }
94 result = stream->methods->read(stream, buf, n);
95 if(result > 0){
96 stream->read += result;
97 }
98 exit:
99 return result;
100 }
102 /** Write to a stream.
103 *
104 * @param stream input
105 * @param buf where to put input
106 * @param n number of bytes to write
107 * @return if ok, number of bytes read, otherwise negative error code
108 */
109 static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
110 int result = 0;
111 if(stream->closed) goto exit;
112 if(!stream->methods || !stream->methods->write){
113 result = -EINVAL;
114 goto exit;
115 }
116 result = stream->methods->write(stream, buf, n);
117 if(result > 0){
118 stream->written += result;
119 }
120 exit:
121 return result;
122 }
124 /** Flush the stream.
125 *
126 * @param stream stream
127 * @return 0 on success, IOSTREAM_EOF otherwise
128 */
129 static inline int IOStream_flush(IOStream *stream){
130 int result = 0;
131 if(stream->closed){
132 result = IOSTREAM_EOF;
133 } else if(stream->methods->flush){
134 result = stream->methods->flush(stream);
135 if(result < 0) result = IOSTREAM_EOF;
136 }
137 return result;
138 }
140 /** Check whether the stream has an error.
141 *
142 * @param stream to check
143 * @return 1 for error, 0 otherwise
144 */
145 static inline int IOStream_error(IOStream *stream){
146 int err = 0;
147 if(stream->methods && stream->methods->error){
148 err = stream->methods->error(stream);
149 }
150 return err;
151 }
153 /** Close the stream.
154 *
155 * @param stream to close
156 * @return 1 for error, 0 otherwise
157 */
158 static inline int IOStream_close(IOStream *stream){
159 int err = 1;
160 if(stream->methods && stream->methods->close){
161 err = stream->methods->close(stream);
162 }
163 return err;
164 }
166 /** Test if the stream has been closed.
167 *
168 * @param stream to check
169 * @return 1 if closed, 0 otherwise
170 */
171 static inline int IOStream_is_closed(IOStream *stream){
172 return stream->closed;
173 }
175 /** Free the memory used by the stream.
176 *
177 * @param stream to free
178 */
179 static inline void IOStream_free(IOStream *stream){
180 if(stream->methods && stream->methods->free){
181 stream->methods->free(stream);
182 }
183 *stream = (IOStream){};
184 deallocate(stream);
185 }
188 /** Print a character to a stream, like fputc().
189 *
190 * @param stream to print to
191 * @param c character to print
192 * @return result code from the print
193 */
194 static inline int IOStream_putc(IOStream *stream, int c){
195 int err;
196 unsigned char b = (unsigned char)c;
197 err = IOStream_write(stream, &b, 1);
198 if(err < 1){
199 err = IOSTREAM_EOF;
200 } else {
201 err = b;
202 }
203 return err;
204 }
206 /** Read from a stream, like fgetc().
207 *
208 * @param stream to read from
209 * @return IOSTREAM_EOF on error, character read otherwise
210 */
211 static inline int IOStream_getc(IOStream *stream){
212 int err, rc;
213 unsigned char b;
215 err = IOStream_read(stream, &b, 1);
216 if(err < 1){
217 rc = IOSTREAM_EOF;
218 } else {
219 rc = b;
220 }
221 return rc;
222 }
224 /** Get number of bytes read.
225 *
226 * @param stream to get from
227 * @return number of bytes read
228 */
229 static inline int IOStream_get_read(IOStream *stream){
230 return stream->read;
231 }
233 /** Get number of bytes written.
234 *
235 * @param stream to get from
236 * @return number of bytes written
237 */
238 static inline int IOStream_get_written(IOStream *stream){
239 return stream->written;
240 }
243 #endif /* ! _XC_LINUX_SAVE_H_ */