00001
00038 #ifndef STREAM_H_INCLUDED
00039 #define STREAM_H_INCLUDED
00040
00041 #include <assert.h>
00042 #include <ring.h>
00043 #include <stdarg.h>
00044 #include <types.h>
00045
00070 struct stream {
00072 const struct stream_ops *ops;
00074 struct ring_head ring;
00076 const unsigned int ring_mask;
00078 char *data;
00079 };
00080
00081 int stream_vprintf(struct stream *stream, const char *format, va_list ap);
00082 int stream_printf(struct stream *stream, const char *format, ...)
00083 __printf_format(2, 3);
00084 int stream_putstr(struct stream *stream, const char *str);
00085 int stream_putchar(struct stream *stream, int c);
00086
00087 int snprintf(char *str, size_t size, const char *format, ...)
00088 __printf_format(3, 4);
00089 int sprintf(char *str, const char *format, ...) __printf_format(2, 3);
00090
00102 struct stream_ops {
00114 void (*commit)(struct stream *stream);
00139 bool (*make_room)(struct stream *stream, unsigned int goal);
00140 };
00141
00143 static inline unsigned int stream_buf_size(struct stream *stream)
00144 {
00145 return stream->ring_mask + 1;
00146 }
00147
00149 static inline bool stream_buf_has_data(struct stream *stream)
00150 {
00151 return !ring_is_empty(&stream->ring);
00152 }
00153
00155 static inline bool stream_buf_is_full(struct stream *stream)
00156 {
00157 return ring_is_full(&stream->ring, stream_buf_size(stream));
00158 }
00159
00161 static inline unsigned int stream_buf_unused(struct stream *stream)
00162 {
00163 return ring_entries_unused(&stream->ring, stream_buf_size(stream));
00164 }
00165
00167 static inline unsigned int stream_buf_used(struct stream *stream)
00168 {
00169 return ring_entries_used(&stream->ring);
00170 }
00171
00173 static inline unsigned int stream_buf_unused_before_end(struct stream *stream)
00174 {
00175 return ring_entries_unused_before_end(&stream->ring,
00176 stream_buf_size(stream));
00177 }
00178
00183 static inline unsigned int stream_buf_used_before_end(struct stream *stream)
00184 {
00185 return ring_entries_used_before_end(&stream->ring,
00186 stream_buf_size(stream));
00187 }
00188
00193 static inline unsigned int stream_buf_head(struct stream *stream)
00194 {
00195 return ring_get_head(&stream->ring, stream_buf_size(stream));
00196 }
00197
00202 static inline unsigned int stream_buf_tail(struct stream *stream)
00203 {
00204 return ring_get_tail(&stream->ring, stream_buf_size(stream));
00205 }
00206
00211 static inline char stream_buf_insert_char(struct stream *stream, char c)
00212 {
00213 assert(!stream_buf_is_full(stream));
00214
00215 stream->data[stream_buf_head(stream)] = c;
00216 ring_insert_entries(&stream->ring, 1);
00217
00218 return c;
00219 }
00220
00225 static inline char stream_buf_extract_char(struct stream *stream)
00226 {
00227 char c;
00228
00229 assert(stream_buf_has_data(stream));
00230
00231 c = stream->data[stream_buf_tail(stream)];
00232 ring_extract_entries(&stream->ring, 1);
00233
00234 return c;
00235 }
00236
00239
00240 #endif