00001
00012 #ifndef NXS_HEADERS_STREAM
00013 #define NXS_HEADERS_STREAM
00014
00015 #ifdef HAVE_CONFIG_H
00016 #include "config.h"
00017 #endif
00018
00019 #include <assert.h>
00020 #include <stdarg.h>
00021 #include <stdbool.h>
00022 #include <stddef.h>
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025
00026
00031 typedef struct nxs_stream nxs_stream;
00032
00037 typedef union {
00038 fpos_t file;
00039 size_t memory;
00040 } nxs_stream_pos;
00041
00042
00050 static inline
00051 nxs_stream * nxs_stream_create (void);
00052
00060 nxs_stream * nxs_stream_createFromConstMemory (const void *mem, size_t length);
00061
00070 static inline
00071 nxs_stream * nxs_stream_createFromFile (const char *path, const char *mode);
00072
00081 nxs_stream * nxs_stream_createFromFP (FILE *fp, bool shouldClose);
00082
00092 nxs_stream * nxs_stream_createFromMemory ( void *mem, size_t length, bool autorelease, bool shouldExpand);
00093
00101 static inline
00102 nxs_stream * nxs_stream_createFromNewMemory ( size_t length, bool shouldExpand);
00103
00107 void nxs_stream_close ( nxs_stream *ptr);
00108
00116 size_t nxs_stream_copy (nxs_stream * restrict src, nxs_stream * restrict dst, size_t size);
00117
00125 size_t nxs_stream_copy_full (nxs_stream * restrict src, nxs_stream * restrict dst);
00126
00132 bool nxs_stream_eof (const nxs_stream *ptr);
00133
00139 bool nxs_stream_flush ( nxs_stream *ptr);
00140
00146 int nxs_stream_getc ( nxs_stream *ptr);
00147
00153 char * nxs_stream_gets ( nxs_stream * restrict ptr, char * restrict str, size_t size);
00154
00161 static inline
00162 int nxs_stream_printf ( nxs_stream * restrict ptr, const char * restrict format, ...);
00163
00169 int nxs_stream_putc ( nxs_stream *ptr, int c);
00170
00175 bool nxs_stream_puts ( nxs_stream * restrict ptr, const char * restrict str);
00176
00182 size_t nxs_stream_read ( nxs_stream * restrict ptr, void * restrict buffer, size_t size);
00183
00193 bool nxs_stream_seek ( nxs_stream * restrict ptr, const nxs_stream_pos * restrict pos);
00194
00202 bool nxs_stream_seekEnd ( nxs_stream *ptr);
00203
00211 bool nxs_stream_seekStart ( nxs_stream *ptr);
00212
00219 bool nxs_stream_tell (const nxs_stream * restrict ptr, nxs_stream_pos * restrict pos);
00220
00230 int nxs_stream_ungetc ( nxs_stream *ptr, int c);
00231
00238 int nxs_stream_vprintf ( nxs_stream * restrict ptr, const char * restrict format, va_list arg);
00239
00245 size_t nxs_stream_write ( nxs_stream * restrict ptr, const void * restrict buffer, size_t size);
00246
00248
00249 static inline
00250 nxs_stream * nxs_stream_create (void) {
00251 return nxs_stream_createFromMemory(NULL, 0, true, true);
00252 }
00253
00254 static inline
00255 nxs_stream * nxs_stream_createFromFile (const char *path, const char *mode) {
00256 assert(path != NULL);
00257 assert(mode != NULL);
00258
00259 FILE *fp = fopen(path, mode);
00260 nxs_stream *ret = nxs_stream_createFromFP(fp, true);
00261 if (ret == NULL) {
00262 if (fp)
00263 fclose(fp);
00264
00265 return NULL;
00266 }
00267
00268 return ret;
00269 }
00270
00271 static inline
00272 nxs_stream * nxs_stream_createFromNewMemory ( size_t length, bool shouldExpand) {
00273 assert(length > 0);
00274
00275 void *mem = malloc(length);
00276 if (mem == NULL)
00277 return NULL;
00278
00279 nxs_stream *ret = nxs_stream_createFromMemory(mem, length, true,
00280 shouldExpand);
00281 if (ret == NULL) {
00282 free(mem);
00283 return NULL;
00284 }
00285
00286 return ret;
00287 }
00288
00289 static inline
00290 int nxs_stream_printf ( nxs_stream * restrict ptr, const char * restrict format, ...) {
00291 assert(ptr != NULL);
00292 assert(format != NULL);
00293
00294 va_list args;
00295
00296 va_start(args, format);
00297 int result = nxs_stream_vprintf(ptr, format, args);
00298 va_end(args);
00299
00300 return result;
00301 }
00302
00303 #endif