aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxutil/iostream.h
blob: 5dbe14a0b418b0bf95321b3957268dd6656e2c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#ifndef _XC_LINUX_SAVE_H_
#define _XC_LINUX_SAVE_H_

#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>

#ifdef __KERNEL__
#include <linux/errno.h>
#else
#include <errno.h>
#endif

#include "allocate.h"

/** End of input return value. */
#define IOSTREAM_EOF -1

/** An input/output abstraction.
 */
typedef struct IOStream IOStream;

/** Record of the functions to use for operations on an
 * IOStream implementation.
 */
typedef struct IOMethods {
    /** Read function.  Called with the user data, buffer to read into
     * and number of bytes to read.  Must return number of bytes read
     * on success, less than zero on error.
     */
    int (*read)(IOStream *stream, void *buf, size_t n);

    /** Write function. Called with user data, buffer to write and
     * number of bytes to write. Must return number of bytes written on
     * success, less than zero otherwise.
     */
    int (*write)(IOStream *stream, const void *buf, size_t n);

    int (*flush)(IOStream *s);

    int (*error)(IOStream *s);

    int (*close)(IOStream *s);

    void (*free)(IOStream *s);

    void (*lock)(IOStream *s);
    void (*unlock)(IOStream *s);

} IOMethods;

/** Abstract i/o object.
 */
struct IOStream {
    /** Methods to use to implement operations. */
    const IOMethods *methods;
    /** Private state for the implementation. */
    const void *data;
    /** Flag indicating whether the stream is closed. */
    int closed;
    /** Number of bytes written. */
    int written;
    /** Number of bytes read. */
    int read;
};


/** IOStream version of stdin. */
extern IOStream *iostdin;

/** IOStream version of stdout, */
extern IOStream *iostdout;

/** IOStream version of stderr. */
extern IOStream *iostderr;

extern int IOStream_print(IOStream *io, const char *format, ...);
extern int IOStream_vprint(IOStream *io, const char *format, va_list args);

/** Read from a stream.
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to read
 * @return if ok, number of bytes read, otherwise negative error code
 */
static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
    int result = 0;
    if(stream->closed) goto exit;
    if(!stream->methods || !stream->methods->read){
        result = -EINVAL;
        goto exit;
    }
    result = stream->methods->read(stream, buf, n);
    if(result > 0){
        stream->read += result;
    }
  exit:
    return result;
}

/** Write to a stream.
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to write
 * @return if ok, number of bytes read, otherwise negative error code
 */
static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
    int result = 0;
    if(stream->closed) goto exit;
    if(!stream->methods || !stream->methods->write){
        result = -EINVAL;
        goto exit;
    }
    result = stream->methods->write(stream, buf, n);
    if(result > 0){
        stream->written += result;
    }
  exit:
    return result;
}

/** Flush the stream.
 *
 * @param stream stream
 * @return 0 on success, IOSTREAM_EOF otherwise
 */
static inline int IOStream_flush(IOStream *stream){
    int result = 0;
    if(stream->closed){
        result = IOSTREAM_EOF;
    } else if(stream->methods->flush){
        result = stream->methods->flush(stream);
        if(result < 0) result = IOSTREAM_EOF;
    }
    return result;
}

/** Check whether the stream has an error.
 *
 * @param stream to check
 * @return 1 for error, 0 otherwise
 */
static inline int IOStream_error(IOStream *stream){
    int err = 0;
    if(stream->methods && stream->methods->error){
       err = stream->methods->error(stream);
    }
    return err;
}

/** Close the stream.
 *
 * @param stream to close
 * @return 1 for error, 0 otherwise
 */
static inline int IOStream_close(IOStream *stream){
    int err = 1;
    if(stream->methods && stream->methods->close){
        err = stream->methods->close(stream);
    }
    return err;
}

/** Test if the stream has been closed.
 *
 * @param stream to check
 * @return 1 if closed, 0 otherwise
 */
static inline int IOStream_is_closed(IOStream *stream){
  return stream->closed;
}

/** Free the memory used by the stream.
 *
 * @param stream to free
 */
static inline void IOStream_free(IOStream *stream){
  if(stream->methods && stream->methods->free){
    stream->methods->free(stream);
  }
  *stream = (IOStream){};
  deallocate(stream);
}


/** Print a character to a stream, like fputc().
 *
 * @param stream to print to
 * @param c character to print
 * @return result code from the print
 */
static inline int IOStream_putc(IOStream *stream, int c){
    int err;
    unsigned char b = (unsigned char)c;
    err = IOStream_write(stream, &b, 1);
    if(err < 1){
        err = IOSTREAM_EOF;
    } else {
        err = b;
    }
    return err;
}

/** Read from a stream, like fgetc().
 *
 * @param stream to read from
 * @return IOSTREAM_EOF on error, character read otherwise
 */
static inline int IOStream_getc(IOStream *stream){
    int err, rc;
    unsigned char b;

    err = IOStream_read(stream, &b, 1);
    if(err < 1){
        rc = IOSTREAM_EOF;
    } else {
        rc = b;
    }
    return rc;
}

/** Get number of bytes read.
 *
 * @param stream to get from
 * @return number of bytes read
 */
static inline int IOStream_get_read(IOStream *stream){
    return stream->read;
}

/** Get number of bytes written.
 *
 * @param stream to get from
 * @return number of bytes written
 */
static inline int IOStream_get_written(IOStream *stream){
    return stream->written;
}


#endif /* ! _XC_LINUX_SAVE_H_ */