Tue, 09 Oct 2012 15:02:40 +0200
added memstream to ucx - still little work to do
universe@56 | 1 | #ifndef MEMSTREAM_H |
universe@56 | 2 | #define MEMSTREAM_H |
universe@56 | 3 | |
universe@56 | 4 | #include <stdio.h> |
universe@56 | 5 | |
universe@56 | 6 | /* as fmemopen is a C extension we provide our cross plattform stuff here */ |
universe@56 | 7 | |
universe@56 | 8 | #ifdef __cplusplus |
universe@56 | 9 | extern "C" { |
universe@56 | 10 | #endif |
universe@56 | 11 | |
universe@56 | 12 | |
universe@56 | 13 | /* as FILE is opaque, we don't do evil hacks but provide an alternative */ |
universe@56 | 14 | struct _UcxMemstream; /* cauz we are mad about it, we make it opaque, too */ |
universe@56 | 15 | typedef struct _UcxMemstream UcxMemstream; |
universe@56 | 16 | |
universe@56 | 17 | UcxMemstream *ucx_memopen(void *space, size_t length); |
universe@56 | 18 | void ucx_memclose(UcxMemstream* stream); |
universe@56 | 19 | |
universe@56 | 20 | /* |
universe@56 | 21 | * Moves the position of the stream to a new position relative to whence. |
universe@56 | 22 | * |
universe@56 | 23 | * SEEK_SET marks the start of the buffer |
universe@56 | 24 | * SEEK_CUR marks the current position |
universe@56 | 25 | * SEEK_END marks the first 0-byte in the buffer |
universe@56 | 26 | * |
universe@56 | 27 | * ucx_memseek returns 0 on success and -1 if the new position is beyond the |
universe@56 | 28 | * bounds of the allocated buffer. In that case the position of the stream |
universe@56 | 29 | * remains unchanged. |
universe@56 | 30 | * |
universe@56 | 31 | */ |
universe@56 | 32 | int ucx_memseek(UcxMemstream *stream, long offset, int whence); |
universe@56 | 33 | size_t ucx_memtell(UcxMemstream *stream); |
universe@56 | 34 | |
universe@56 | 35 | /* |
universe@56 | 36 | * returns non-zero, iff the current stream position has exceeded the last |
universe@56 | 37 | * available byte of the underlying buffer |
universe@56 | 38 | * |
universe@56 | 39 | */ |
universe@56 | 40 | int ucx_memeof(UcxMemstream *stream); |
universe@56 | 41 | /* |
universe@56 | 42 | * returns non-zero, iff the current stream position has exceeded the length |
universe@56 | 43 | * of the underlying buffer |
universe@56 | 44 | * |
universe@56 | 45 | * in contrast to ucx_memeof this function will return zero, if the current |
universe@56 | 46 | * position exactly matches the buffer length |
universe@56 | 47 | * |
universe@56 | 48 | * this function should be called after any ucx_memprintf/ucx_memscanf call |
universe@56 | 49 | */ |
universe@56 | 50 | int ucx_memoverflow(UcxMemstream *stream); |
universe@56 | 51 | |
universe@56 | 52 | /* memwrite, memread, memputc and memreadc shall not generate overflows */ |
universe@56 | 53 | size_t ucx_memio(void *d, size_t s, size_t n, UcxMemstream* m, _Bool read); |
universe@56 | 54 | #define ucx_memwrite(data, itemsize, nitems, memstream) \ |
universe@56 | 55 | ucx_memio(data, itemsize, nitems, memstream, 0) |
universe@56 | 56 | #define ucx_memread(data, itemsize, nitems, memstream) \ |
universe@56 | 57 | ucx_memio(data, itemsize, nitems, memstream, 1) |
universe@56 | 58 | int ucx_memputc(UcxMemstream *stream, int c); |
universe@56 | 59 | int ucx_memgetc(UcxMemstream *stream); |
universe@56 | 60 | |
universe@56 | 61 | /* printf / scanf may generate overflows */ |
universe@56 | 62 | int ucx_memprintf(UcxMemstream *stream, const char* format, ...); |
universe@56 | 63 | int ucx_memscanf(UcxMemstream *stream, const char* format, ...); |
universe@56 | 64 | |
universe@56 | 65 | #ifdef __cplusplus |
universe@56 | 66 | } |
universe@56 | 67 | #endif |
universe@56 | 68 | |
universe@56 | 69 | #endif /* MEMSTREAM_H */ |
universe@56 | 70 |