Tue, 09 Oct 2012 16:46:29 +0200
some fixes
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 */ |
olaf@57 | 14 | typedef struct UcxMemstream UcxMemstream; |
universe@56 | 15 | |
universe@56 | 16 | UcxMemstream *ucx_memopen(void *space, size_t length); |
universe@56 | 17 | void ucx_memclose(UcxMemstream* stream); |
universe@56 | 18 | |
universe@56 | 19 | /* |
universe@56 | 20 | * Moves the position of the stream to a new position relative to whence. |
universe@56 | 21 | * |
universe@56 | 22 | * SEEK_SET marks the start of the buffer |
universe@56 | 23 | * SEEK_CUR marks the current position |
universe@56 | 24 | * SEEK_END marks the first 0-byte in the buffer |
universe@56 | 25 | * |
universe@56 | 26 | * ucx_memseek returns 0 on success and -1 if the new position is beyond the |
universe@56 | 27 | * bounds of the allocated buffer. In that case the position of the stream |
universe@56 | 28 | * remains unchanged. |
universe@56 | 29 | * |
universe@56 | 30 | */ |
olaf@57 | 31 | int ucx_memseek(UcxMemstream *stream, off_t offset, int whence); |
universe@56 | 32 | size_t ucx_memtell(UcxMemstream *stream); |
universe@56 | 33 | |
universe@56 | 34 | /* |
universe@56 | 35 | * returns non-zero, iff the current stream position has exceeded the last |
universe@56 | 36 | * available byte of the underlying buffer |
universe@56 | 37 | * |
universe@56 | 38 | */ |
universe@56 | 39 | int ucx_memeof(UcxMemstream *stream); |
universe@56 | 40 | /* |
universe@56 | 41 | * returns non-zero, iff the current stream position has exceeded the length |
universe@56 | 42 | * of the underlying buffer |
universe@56 | 43 | * |
universe@56 | 44 | * in contrast to ucx_memeof this function will return zero, if the current |
universe@56 | 45 | * position exactly matches the buffer length |
universe@56 | 46 | * |
universe@56 | 47 | * this function should be called after any ucx_memprintf/ucx_memscanf call |
universe@56 | 48 | */ |
universe@56 | 49 | int ucx_memoverflow(UcxMemstream *stream); |
universe@56 | 50 | |
universe@56 | 51 | /* memwrite, memread, memputc and memreadc shall not generate overflows */ |
universe@56 | 52 | size_t ucx_memio(void *d, size_t s, size_t n, UcxMemstream* m, _Bool read); |
universe@56 | 53 | #define ucx_memwrite(data, itemsize, nitems, memstream) \ |
universe@56 | 54 | ucx_memio(data, itemsize, nitems, memstream, 0) |
universe@56 | 55 | #define ucx_memread(data, itemsize, nitems, memstream) \ |
universe@56 | 56 | ucx_memio(data, itemsize, nitems, memstream, 1) |
universe@56 | 57 | int ucx_memputc(UcxMemstream *stream, int c); |
universe@56 | 58 | int ucx_memgetc(UcxMemstream *stream); |
universe@56 | 59 | |
universe@56 | 60 | /* printf / scanf may generate overflows */ |
universe@56 | 61 | int ucx_memprintf(UcxMemstream *stream, const char* format, ...); |
universe@56 | 62 | int ucx_memscanf(UcxMemstream *stream, const char* format, ...); |
universe@56 | 63 | |
universe@56 | 64 | #ifdef __cplusplus |
universe@56 | 65 | } |
universe@56 | 66 | #endif |
universe@56 | 67 | |
universe@56 | 68 | #endif /* MEMSTREAM_H */ |
universe@56 | 69 |