Wed, 10 Oct 2012 09:54:57 +0200
renamed UcxMemstream to UcxBuffer
test/Makefile | file | annotate | diff | comparison | revisions | |
test/buffer_tests.c | file | annotate | diff | comparison | revisions | |
test/buffer_tests.h | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions | |
test/memstream_tests.c | file | annotate | diff | comparison | revisions | |
test/memstream_tests.h | file | annotate | diff | comparison | revisions | |
ucx/Makefile | file | annotate | diff | comparison | revisions | |
ucx/buffer.c | file | annotate | diff | comparison | revisions | |
ucx/buffer.h | file | annotate | diff | comparison | revisions | |
ucx/memstream.c | file | annotate | diff | comparison | revisions | |
ucx/memstream.h | file | annotate | diff | comparison | revisions |
1.1 --- a/test/Makefile Wed Oct 10 09:34:13 2012 +0200 1.2 +++ b/test/Makefile Wed Oct 10 09:54:57 2012 +0200 1.3 @@ -35,7 +35,7 @@ 1.4 SRC += map_tests.c 1.5 SRC += string_tests.c 1.6 SRC += logging_tests.c 1.7 -SRC += memstream_tests.c 1.8 +SRC += buffer_tests.c 1.9 1.10 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT)) 1.11
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/buffer_tests.c Wed Oct 10 09:54:57 2012 +0200 2.3 @@ -0,0 +1,186 @@ 2.4 +/* 2.5 + * 2.6 + */ 2.7 + 2.8 +#include "buffer_tests.h" 2.9 + 2.10 +UCX_TEST_IMPLEMENT(test_ucx_buffer_seektell) { 2.11 + char *buffer = malloc(16); 2.12 + memset(buffer, 32, 7); 2.13 + buffer[7] = 0; 2.14 + 2.15 + UcxBuffer *b = ucx_buffer_new(buffer, 16); 2.16 + int r; 2.17 + 2.18 + UCX_TEST_BEGIN 2.19 + 2.20 + r = ucx_buffer_seek(b, 5, SEEK_SET); 2.21 + UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); 2.22 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5, "seek SET+5 set wrong position"); 2.23 + 2.24 + r = ucx_buffer_seek(b, 20, SEEK_SET); 2.25 + UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); 2.26 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 5, 2.27 + "failed seek shall leave pos unchanged"); 2.28 + 2.29 + r = ucx_buffer_seek(b, 5, SEEK_CUR); 2.30 + UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); 2.31 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10, "seek CUR+5 set wrong position"); 2.32 + 2.33 + r = ucx_buffer_seek(b, 10, SEEK_CUR); 2.34 + UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); 2.35 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 10, 2.36 + "failed seek shall leave pos unchanged"); 2.37 + 2.38 + r = ucx_buffer_seek(b, -5, SEEK_END); 2.39 + UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); 2.40 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2, "seek END-5 set wrong position"); 2.41 + 2.42 + r = ucx_buffer_seek(b, -10, SEEK_END); 2.43 + UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); 2.44 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 2, 2.45 + "failed seek shall leave pos unchanged"); 2.46 + 2.47 + UCX_TEST_END 2.48 + 2.49 + ucx_buffer_free(b); 2.50 + free(buffer); 2.51 +} 2.52 + 2.53 +UCX_TEST_IMPLEMENT(test_ucx_buffer_putc) { 2.54 + char *buffer = malloc(16); 2.55 + memset(buffer, 32, 16); 2.56 + 2.57 + UcxBuffer *b = ucx_buffer_new(buffer, 16); 2.58 + int r; 2.59 + 2.60 + UCX_TEST_BEGIN 2.61 + 2.62 + ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); 2.63 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 3, "pos wrong after first 3 puts"); 2.64 + ucx_buffer_seek(b, 10, SEEK_CUR); 2.65 + ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); ucx_buffer_putc(b, 48); 2.66 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after last 3 puts"); 2.67 + UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof not set"); 2.68 + UCX_TEST_ASSERT(ucx_buffer_putc(b, 48) == EOF, "put shall return EOF on memof"); 2.69 + UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, 2.70 + "buffer contains incorrect content"); 2.71 + 2.72 + UCX_TEST_END 2.73 + 2.74 + ucx_buffer_free(b); 2.75 + free(buffer); 2.76 +} 2.77 + 2.78 +UCX_TEST_IMPLEMENT(test_ucx_buffer_getc) { 2.79 + char *buffer = malloc(16); 2.80 + memset(buffer, 32, 8); 2.81 + for (int i = 8; i < 16 ; i++) { 2.82 + buffer[i] = 40+i; 2.83 + } 2.84 + 2.85 + UcxBuffer *b = ucx_buffer_new(buffer, 16); 2.86 + int r; 2.87 + 2.88 + UCX_TEST_BEGIN 2.89 + 2.90 + char rb[16]; 2.91 + for (int i = 0 ; i < 16 ; i++) { 2.92 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == i, "pos wrong during read loop"); 2.93 + UCX_TEST_ASSERT(!ucx_buffer_eof(b), 2.94 + "EOF shall not be set during read loop"); 2.95 + rb[i] = ucx_buffer_getc(b); 2.96 + } 2.97 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 16, "pos wrong after read loop"); 2.98 + UCX_TEST_ASSERT(ucx_buffer_eof(b), "EOF not set"); 2.99 + UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, 2.100 + "read data incorrect"); 2.101 + 2.102 + UCX_TEST_END 2.103 + 2.104 + ucx_buffer_free(b); 2.105 + free(buffer); 2.106 +} 2.107 + 2.108 +UCX_TEST_IMPLEMENT(test_ucx_buffer_write) { 2.109 + char *buffer = malloc(16); 2.110 + memset(buffer, 32, 8); 2.111 + for (int i = 8; i < 16 ; i++) { 2.112 + buffer[i] = 40+i; 2.113 + } 2.114 + 2.115 + UcxBuffer *b = ucx_buffer_new(buffer, 16); 2.116 + int r; 2.117 + 2.118 + UCX_TEST_BEGIN 2.119 + 2.120 + char* teststring = "this is way too much"; 2.121 + r = ucx_buffer_write(teststring, 1, 20, b); 2.122 + UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); 2.123 + UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, 2.124 + "buffer data incorrect"); 2.125 + UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); 2.126 + 2.127 + ucx_buffer_seek(b, 8, SEEK_SET); 2.128 + r = ucx_buffer_write("not", 1, 3, b); 2.129 + UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); 2.130 + UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, 2.131 + "modified buffer is incorrect"); 2.132 + 2.133 + char* threebytestring = " t h r e e "; 2.134 + memset(buffer, 49, 16); 2.135 + ucx_buffer_seek(b, 0, SEEK_SET); 2.136 + r = ucx_buffer_write(threebytestring, 3, 6, b); 2.137 + UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); 2.138 + UCX_TEST_ASSERT(ucx_buffer_tell(b) == 15, 2.139 + "position after write of three byte string incorrect"); 2.140 + UCX_TEST_ASSERT(!ucx_buffer_eof(b), "eof shall not be set"); 2.141 + UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, 2.142 + "bufer is incorrect after three byte string has been written"); 2.143 + 2.144 + UCX_TEST_END 2.145 + 2.146 + ucx_buffer_free(b); 2.147 + free(buffer); 2.148 +} 2.149 + 2.150 +UCX_TEST_IMPLEMENT(test_ucx_buffer_read) { 2.151 + char *buffer = malloc(16); 2.152 + memset(buffer, 56, 8); 2.153 + for (int i = 8; i < 16 ; i++) { 2.154 + buffer[i] = 40+i; 2.155 + } 2.156 + 2.157 + UcxBuffer *b = ucx_buffer_new(buffer, 16); 2.158 + int r; 2.159 + 2.160 + UCX_TEST_BEGIN 2.161 + 2.162 + char rb[16]; 2.163 + memset(rb, 32, 16); 2.164 + 2.165 + ucx_buffer_seek(b, 8, SEEK_SET); 2.166 + r = ucx_buffer_read(rb, 1, 16, b); 2.167 + UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end"); 2.168 + UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0, 2.169 + "buffer incorrect after first read"); 2.170 + UCX_TEST_ASSERT(ucx_buffer_eof(b), "eof shall be set"); 2.171 + 2.172 + ucx_buffer_seek(b, 0, SEEK_SET); 2.173 + r = ucx_buffer_read(rb+8, 1, 8, b); 2.174 + UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes"); 2.175 + UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0, 2.176 + "buffer incorrect after second read"); 2.177 + 2.178 + ucx_buffer_seek(b, 0, SEEK_SET); 2.179 + r = ucx_buffer_read(rb, 3, 6, b); 2.180 + UCX_TEST_ASSERT(r == 15, 2.181 + "three byte read did not read the desired amount of bytes"); 2.182 + UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0, 2.183 + "buffer incorrect after three byte read"); 2.184 + 2.185 + UCX_TEST_END 2.186 + 2.187 + ucx_buffer_free(b); 2.188 + free(buffer); 2.189 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/buffer_tests.h Wed Oct 10 09:54:57 2012 +0200 3.3 @@ -0,0 +1,28 @@ 3.4 +/* 3.5 + * 3.6 + */ 3.7 + 3.8 +#ifndef MEMSTREAM_TEST_H 3.9 +#define MEMSTREAM_TEST_H 3.10 + 3.11 +#include "ucx/test.h" 3.12 +#include "ucx/buffer.h" 3.13 + 3.14 +#ifdef __cplusplus 3.15 +extern "C" { 3.16 +#endif 3.17 + 3.18 +/* assume open and close to be correct */ 3.19 + 3.20 +UCX_TEST_DECLARE(test_ucx_buffer_seektell); 3.21 +UCX_TEST_DECLARE(test_ucx_buffer_putc); 3.22 +UCX_TEST_DECLARE(test_ucx_buffer_getc); 3.23 +UCX_TEST_DECLARE(test_ucx_buffer_write); 3.24 +UCX_TEST_DECLARE(test_ucx_buffer_read); 3.25 + 3.26 +#ifdef __cplusplus 3.27 +} 3.28 +#endif 3.29 + 3.30 +#endif /* MEMSTREAM_TEST_H */ 3.31 +
4.1 --- a/test/main.c Wed Oct 10 09:34:13 2012 +0200 4.2 +++ b/test/main.c Wed Oct 10 09:54:57 2012 +0200 4.3 @@ -39,7 +39,7 @@ 4.4 #include "string_tests.h" 4.5 #include "mpool_tests.h" 4.6 #include "map_tests.h" 4.7 -#include "memstream_tests.h" 4.8 +#include "buffer_tests.h" 4.9 4.10 int cmp_string(void* o1, void* o2, void* data) { 4.11 return strcmp((char*)o1, (char*)o2); 4.12 @@ -164,11 +164,11 @@ 4.13 ucx_test_register(suite, test_sstrsplit); 4.14 4.15 /* UcxMemstream Tests */ 4.16 - ucx_test_register(suite, test_ucx_memseektell); 4.17 - ucx_test_register(suite, test_ucx_memputc); 4.18 - ucx_test_register(suite, test_ucx_memgetc); 4.19 - ucx_test_register(suite, test_ucx_memwrite); 4.20 - ucx_test_register(suite, test_ucx_memread); 4.21 + ucx_test_register(suite, test_ucx_buffer_seektell); 4.22 + ucx_test_register(suite, test_ucx_buffer_putc); 4.23 + ucx_test_register(suite, test_ucx_buffer_getc); 4.24 + ucx_test_register(suite, test_ucx_buffer_write); 4.25 + ucx_test_register(suite, test_ucx_buffer_read); 4.26 4.27 ucx_test_run(suite, stdout); 4.28 fflush(stdout);
5.1 --- a/test/memstream_tests.c Wed Oct 10 09:34:13 2012 +0200 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,186 +0,0 @@ 5.4 -/* 5.5 - * 5.6 - */ 5.7 - 5.8 -#include "memstream_tests.h" 5.9 - 5.10 -UCX_TEST_IMPLEMENT(test_ucx_memseektell) { 5.11 - char *buffer = malloc(16); 5.12 - memset(buffer, 32, 7); 5.13 - buffer[7] = 0; 5.14 - 5.15 - UcxMemstream *m = ucx_memopen(buffer, 16); 5.16 - int r; 5.17 - 5.18 - UCX_TEST_BEGIN 5.19 - 5.20 - r = ucx_memseek(m, 5, SEEK_SET); 5.21 - UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); 5.22 - UCX_TEST_ASSERT(ucx_memtell(m) == 5, "seek SET+5 set wrong position"); 5.23 - 5.24 - r = ucx_memseek(m, 20, SEEK_SET); 5.25 - UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); 5.26 - UCX_TEST_ASSERT(ucx_memtell(m) == 5, 5.27 - "failed seek shall leave pos unchanged"); 5.28 - 5.29 - r = ucx_memseek(m, 5, SEEK_CUR); 5.30 - UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); 5.31 - UCX_TEST_ASSERT(ucx_memtell(m) == 10, "seek CUR+5 set wrong position"); 5.32 - 5.33 - r = ucx_memseek(m, 10, SEEK_CUR); 5.34 - UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); 5.35 - UCX_TEST_ASSERT(ucx_memtell(m) == 10, 5.36 - "failed seek shall leave pos unchanged"); 5.37 - 5.38 - r = ucx_memseek(m, -5, SEEK_END); 5.39 - UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); 5.40 - UCX_TEST_ASSERT(ucx_memtell(m) == 2, "seek END-5 set wrong position"); 5.41 - 5.42 - r = ucx_memseek(m, -10, SEEK_END); 5.43 - UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); 5.44 - UCX_TEST_ASSERT(ucx_memtell(m) == 2, 5.45 - "failed seek shall leave pos unchanged"); 5.46 - 5.47 - UCX_TEST_END 5.48 - 5.49 - ucx_memclose(m); 5.50 - free(buffer); 5.51 -} 5.52 - 5.53 -UCX_TEST_IMPLEMENT(test_ucx_memputc) { 5.54 - char *buffer = malloc(16); 5.55 - memset(buffer, 32, 16); 5.56 - 5.57 - UcxMemstream *m = ucx_memopen(buffer, 16); 5.58 - int r; 5.59 - 5.60 - UCX_TEST_BEGIN 5.61 - 5.62 - ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); 5.63 - UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts"); 5.64 - ucx_memseek(m, 10, SEEK_CUR); 5.65 - ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); 5.66 - UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after last 3 puts"); 5.67 - UCX_TEST_ASSERT(ucx_memeof(m), "eof not set"); 5.68 - UCX_TEST_ASSERT(ucx_memputc(m, 48) == EOF, "put shall return EOF on memof"); 5.69 - UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, 5.70 - "buffer contains incorrect content"); 5.71 - 5.72 - UCX_TEST_END 5.73 - 5.74 - ucx_memclose(m); 5.75 - free(buffer); 5.76 -} 5.77 - 5.78 -UCX_TEST_IMPLEMENT(test_ucx_memgetc) { 5.79 - char *buffer = malloc(16); 5.80 - memset(buffer, 32, 8); 5.81 - for (int i = 8; i < 16 ; i++) { 5.82 - buffer[i] = 40+i; 5.83 - } 5.84 - 5.85 - UcxMemstream *m = ucx_memopen(buffer, 16); 5.86 - int r; 5.87 - 5.88 - UCX_TEST_BEGIN 5.89 - 5.90 - char rb[16]; 5.91 - for (int i = 0 ; i < 16 ; i++) { 5.92 - UCX_TEST_ASSERT(ucx_memtell(m) == i, "pos wrong during read loop"); 5.93 - UCX_TEST_ASSERT(!ucx_memeof(m), 5.94 - "EOF shall not be set during read loop"); 5.95 - rb[i] = ucx_memgetc(m); 5.96 - } 5.97 - UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after read loop"); 5.98 - UCX_TEST_ASSERT(ucx_memeof(m), "EOF not set"); 5.99 - UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, 5.100 - "read data incorrect"); 5.101 - 5.102 - UCX_TEST_END 5.103 - 5.104 - ucx_memclose(m); 5.105 - free(buffer); 5.106 -} 5.107 - 5.108 -UCX_TEST_IMPLEMENT(test_ucx_memwrite) { 5.109 - char *buffer = malloc(16); 5.110 - memset(buffer, 32, 8); 5.111 - for (int i = 8; i < 16 ; i++) { 5.112 - buffer[i] = 40+i; 5.113 - } 5.114 - 5.115 - UcxMemstream *m = ucx_memopen(buffer, 16); 5.116 - int r; 5.117 - 5.118 - UCX_TEST_BEGIN 5.119 - 5.120 - char* teststring = "this is way too much"; 5.121 - r = ucx_memwrite(teststring, 1, 20, m); 5.122 - UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); 5.123 - UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, 5.124 - "buffer data incorrect"); 5.125 - UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); 5.126 - 5.127 - ucx_memseek(m, 8, SEEK_SET); 5.128 - r = ucx_memwrite("not", 1, 3, m); 5.129 - UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); 5.130 - UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, 5.131 - "modified buffer is incorrect"); 5.132 - 5.133 - char* threebytestring = " t h r e e "; 5.134 - memset(buffer, 49, 16); 5.135 - ucx_memseek(m, 0, SEEK_SET); 5.136 - r = ucx_memwrite(threebytestring, 3, 6, m); 5.137 - UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); 5.138 - UCX_TEST_ASSERT(ucx_memtell(m) == 15, 5.139 - "position after write of three byte string incorrect"); 5.140 - UCX_TEST_ASSERT(!ucx_memeof(m), "eof shall not be set"); 5.141 - UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, 5.142 - "bufer is incorrect after three byte string has been written"); 5.143 - 5.144 - UCX_TEST_END 5.145 - 5.146 - ucx_memclose(m); 5.147 - free(buffer); 5.148 -} 5.149 - 5.150 -UCX_TEST_IMPLEMENT(test_ucx_memread) { 5.151 - char *buffer = malloc(16); 5.152 - memset(buffer, 56, 8); 5.153 - for (int i = 8; i < 16 ; i++) { 5.154 - buffer[i] = 40+i; 5.155 - } 5.156 - 5.157 - UcxMemstream *m = ucx_memopen(buffer, 16); 5.158 - int r; 5.159 - 5.160 - UCX_TEST_BEGIN 5.161 - 5.162 - char rb[16]; 5.163 - memset(rb, 32, 16); 5.164 - 5.165 - ucx_memseek(m, 8, SEEK_SET); 5.166 - r = ucx_memread(rb, 1, 16, m); 5.167 - UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end"); 5.168 - UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0, 5.169 - "buffer incorrect after first read"); 5.170 - UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); 5.171 - 5.172 - ucx_memseek(m, 0, SEEK_SET); 5.173 - r = ucx_memread(rb+8, 1, 8, m); 5.174 - UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes"); 5.175 - UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0, 5.176 - "buffer incorrect after second read"); 5.177 - 5.178 - ucx_memseek(m, 0, SEEK_SET); 5.179 - r = ucx_memread(rb, 3, 6, m); 5.180 - UCX_TEST_ASSERT(r == 15, 5.181 - "three byte read did not read the desired amount of bytes"); 5.182 - UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0, 5.183 - "buffer incorrect after three byte read"); 5.184 - 5.185 - UCX_TEST_END 5.186 - 5.187 - ucx_memclose(m); 5.188 - free(buffer); 5.189 -}
6.1 --- a/test/memstream_tests.h Wed Oct 10 09:34:13 2012 +0200 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,28 +0,0 @@ 6.4 -/* 6.5 - * 6.6 - */ 6.7 - 6.8 -#ifndef MEMSTREAM_TEST_H 6.9 -#define MEMSTREAM_TEST_H 6.10 - 6.11 -#include "ucx/test.h" 6.12 -#include "ucx/memstream.h" 6.13 - 6.14 -#ifdef __cplusplus 6.15 -extern "C" { 6.16 -#endif 6.17 - 6.18 -/* assume open and close to be correct */ 6.19 - 6.20 -UCX_TEST_DECLARE(test_ucx_memseektell); 6.21 -UCX_TEST_DECLARE(test_ucx_memputc); 6.22 -UCX_TEST_DECLARE(test_ucx_memgetc); 6.23 -UCX_TEST_DECLARE(test_ucx_memwrite); 6.24 -UCX_TEST_DECLARE(test_ucx_memread); 6.25 - 6.26 -#ifdef __cplusplus 6.27 -} 6.28 -#endif 6.29 - 6.30 -#endif /* MEMSTREAM_TEST_H */ 6.31 -
7.1 --- a/ucx/Makefile Wed Oct 10 09:34:13 2012 +0200 7.2 +++ b/ucx/Makefile Wed Oct 10 09:54:57 2012 +0200 7.3 @@ -37,7 +37,7 @@ 7.4 SRC += test.c 7.5 SRC += allocator.c 7.6 SRC += logging.c 7.7 -SRC += memstream.c 7.8 +SRC += buffer.c 7.9 7.10 OBJ = $(SRC:%.c=../build/%.$(OBJ_EXT)) 7.11
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/ucx/buffer.c Wed Oct 10 09:54:57 2012 +0200 8.3 @@ -0,0 +1,118 @@ 8.4 +#include "buffer.h" 8.5 +#include <stdarg.h> 8.6 +#include <stdlib.h> 8.7 +#include <string.h> 8.8 + 8.9 +struct UcxBuffer { 8.10 + void *space; 8.11 + off_t pos; 8.12 + size_t length; 8.13 + _Bool autofree; 8.14 +}; 8.15 + 8.16 +UcxBuffer *ucx_buffer_new(void *space, size_t length) { 8.17 + UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); 8.18 + if (buffer) { 8.19 + if (!space) { 8.20 + buffer->space = malloc(length); 8.21 + if (!buffer->space) { 8.22 + free(buffer); 8.23 + return NULL; 8.24 + } 8.25 + memset(buffer->space, 0, length); 8.26 + buffer->autofree = 1; 8.27 + } else { 8.28 + buffer->space = space; 8.29 + buffer->autofree = 0; 8.30 + } 8.31 + buffer->length = length; 8.32 + 8.33 + buffer->pos = 0; 8.34 + } 8.35 + 8.36 + return buffer; 8.37 +} 8.38 + 8.39 +void ucx_buffer_free(UcxBuffer *buffer) { 8.40 + if (buffer->autofree) { 8.41 + free(buffer->space); 8.42 + } 8.43 + free(buffer); 8.44 +} 8.45 + 8.46 +int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { 8.47 + off_t npos; 8.48 + switch (whence) { 8.49 + case SEEK_SET: 8.50 + npos = 0; 8.51 + break; 8.52 + case SEEK_CUR: 8.53 + npos = buffer->pos; 8.54 + break; 8.55 + case SEEK_END: 8.56 + npos = strlen(buffer->space); 8.57 + break; 8.58 + } 8.59 + 8.60 + npos += offset; 8.61 + 8.62 + if (npos < 0 || npos > buffer->length) { 8.63 + return -1; 8.64 + } else { 8.65 + buffer->pos = npos; 8.66 + return 0; 8.67 + } 8.68 + 8.69 +} 8.70 + 8.71 +int ucx_buffer_eof(UcxBuffer *buffer) { 8.72 + return buffer->pos >= buffer->length; 8.73 +} 8.74 + 8.75 +size_t ucx_buffer_tell(UcxBuffer *buffer) { 8.76 + return buffer->pos; 8.77 +} 8.78 + 8.79 +size_t ucx_bufio(void* d, size_t s, size_t n, UcxBuffer *b, _Bool read) { 8.80 + size_t len; 8.81 + if (b->pos + s*n > b->length) { 8.82 + len = b->length - b->pos; 8.83 + if (s > 1) len -= len%s; 8.84 + } else { 8.85 + len = s*n; 8.86 + } 8.87 + 8.88 + if (len == 0) { 8.89 + return 0; 8.90 + } 8.91 + 8.92 + if (read) { 8.93 + memcpy(d, (char*)b->space+b->pos, len); 8.94 + } else { 8.95 + memcpy((char*)b->space+b->pos, d, len); 8.96 + } 8.97 + b->pos += len; 8.98 + 8.99 + return len; 8.100 +} 8.101 + 8.102 +int ucx_buffer_putc(UcxBuffer *buffer, int c) { 8.103 + if (ucx_buffer_eof(buffer)) { 8.104 + return EOF; 8.105 + } else { 8.106 + c &= 0xFF; 8.107 + ((char*)(buffer->space))[buffer->pos] = (char) c; 8.108 + buffer->pos++; 8.109 + return c; 8.110 + } 8.111 +} 8.112 + 8.113 +int ucx_buffer_getc(UcxBuffer *buffer) { 8.114 + if (ucx_buffer_eof(buffer)) { 8.115 + return EOF; 8.116 + } else { 8.117 + int c = ((char*)(buffer->space))[buffer->pos]; 8.118 + buffer->pos++; 8.119 + return c; 8.120 + } 8.121 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/ucx/buffer.h Wed Oct 10 09:54:57 2012 +0200 9.3 @@ -0,0 +1,52 @@ 9.4 +#ifndef BUFFER_H 9.5 +#define BUFFER_H 9.6 + 9.7 +#include <stdio.h> 9.8 + 9.9 +#ifdef __cplusplus 9.10 +extern "C" { 9.11 +#endif 9.12 + 9.13 + 9.14 +/* the user shall not modify values */ 9.15 +typedef struct UcxBuffer UcxBuffer; 9.16 + 9.17 +UcxBuffer *ucx_buffer_new(void *space, size_t length); 9.18 +void ucx_buffer_free(UcxBuffer* buffer); 9.19 + 9.20 +/* 9.21 + * Moves the position of the buffer to a new position relative to whence. 9.22 + * 9.23 + * SEEK_SET marks the start of the buffer 9.24 + * SEEK_CUR marks the current position 9.25 + * SEEK_END marks the first 0-byte in the buffer 9.26 + * 9.27 + * ucx_memseek returns 0 on success and -1 if the new position is beyond the 9.28 + * bounds of the allocated buffer. In that case the position of the buffer 9.29 + * remains unchanged. 9.30 + * 9.31 + */ 9.32 +int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); 9.33 +size_t ucx_buffer_tell(UcxBuffer *buffer); 9.34 + 9.35 +/* 9.36 + * returns non-zero, iff the current buffer position has exceeded the last 9.37 + * available byte of the underlying buffer 9.38 + * 9.39 + */ 9.40 +int ucx_buffer_eof(UcxBuffer *buffer); 9.41 + 9.42 +size_t ucx_bufio(void *d, size_t s, size_t n, UcxBuffer* b, _Bool read); 9.43 +#define ucx_buffer_write(data, itemsize, nitems, buffer) \ 9.44 + ucx_bufio(data, itemsize, nitems, buffer, 0) 9.45 +#define ucx_buffer_read(data, itemsize, nitems, buffer) \ 9.46 + ucx_bufio(data, itemsize, nitems, buffer, 1) 9.47 +int ucx_buffer_putc(UcxBuffer *b, int c); 9.48 +int ucx_buffer_getc(UcxBuffer *b); 9.49 + 9.50 +#ifdef __cplusplus 9.51 +} 9.52 +#endif 9.53 + 9.54 +#endif /* BUFFER_H */ 9.55 +
10.1 --- a/ucx/memstream.c Wed Oct 10 09:34:13 2012 +0200 10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 10.3 @@ -1,118 +0,0 @@ 10.4 -#include "memstream.h" 10.5 -#include <stdarg.h> 10.6 -#include <stdlib.h> 10.7 -#include <string.h> 10.8 - 10.9 -struct UcxMemstream { 10.10 - void *space; 10.11 - off_t pos; 10.12 - size_t length; 10.13 - _Bool autofree; 10.14 -}; 10.15 - 10.16 -UcxMemstream *ucx_memopen(void *space, size_t length) { 10.17 - UcxMemstream *stream = (UcxMemstream*) malloc(sizeof(UcxMemstream)); 10.18 - if (stream) { 10.19 - if (!space) { 10.20 - stream->space = malloc(length); 10.21 - if (!stream->space) { 10.22 - free(stream); 10.23 - return NULL; 10.24 - } 10.25 - memset(stream->space, 0, length); 10.26 - stream->autofree = 1; 10.27 - } else { 10.28 - stream->space = space; 10.29 - stream->autofree = 0; 10.30 - } 10.31 - stream->length = length; 10.32 - 10.33 - stream->pos = 0; 10.34 - } 10.35 - 10.36 - return stream; 10.37 -} 10.38 - 10.39 -void ucx_memclose(UcxMemstream *stream) { 10.40 - if (stream->autofree) { 10.41 - free(stream->space); 10.42 - } 10.43 - free(stream); 10.44 -} 10.45 - 10.46 -int ucx_memseek(UcxMemstream *stream, off_t offset, int whence) { 10.47 - off_t npos; 10.48 - switch (whence) { 10.49 - case SEEK_SET: 10.50 - npos = 0; 10.51 - break; 10.52 - case SEEK_CUR: 10.53 - npos = stream->pos; 10.54 - break; 10.55 - case SEEK_END: 10.56 - npos = strlen(stream->space); 10.57 - break; 10.58 - } 10.59 - 10.60 - npos += offset; 10.61 - 10.62 - if (npos < 0 || npos > stream->length) { 10.63 - return -1; 10.64 - } else { 10.65 - stream->pos = npos; 10.66 - return 0; 10.67 - } 10.68 - 10.69 -} 10.70 - 10.71 -int ucx_memeof(UcxMemstream *stream) { 10.72 - return stream->pos >= stream->length; 10.73 -} 10.74 - 10.75 -size_t ucx_memtell(UcxMemstream *stream) { 10.76 - return stream->pos; 10.77 -} 10.78 - 10.79 -size_t ucx_memio(void* d, size_t s, size_t n, UcxMemstream *m, _Bool read) { 10.80 - size_t len; 10.81 - if (m->pos + s*n > m->length) { 10.82 - len = m->length - m->pos; 10.83 - if (s > 1) len -= len%s; 10.84 - } else { 10.85 - len = s*n; 10.86 - } 10.87 - 10.88 - if (len == 0) { 10.89 - return 0; 10.90 - } 10.91 - 10.92 - if (read) { 10.93 - memcpy(d, (char*)m->space+m->pos, len); 10.94 - } else { 10.95 - memcpy((char*)m->space+m->pos, d, len); 10.96 - } 10.97 - m->pos += len; 10.98 - 10.99 - return len; 10.100 -} 10.101 - 10.102 -int ucx_memputc(UcxMemstream *stream, int c) { 10.103 - if (ucx_memeof(stream)) { 10.104 - return EOF; 10.105 - } else { 10.106 - c &= 0xFF; 10.107 - ((char*)(stream->space))[stream->pos] = (char) c; 10.108 - stream->pos++; 10.109 - return c; 10.110 - } 10.111 -} 10.112 - 10.113 -int ucx_memgetc(UcxMemstream *stream) { 10.114 - if (ucx_memeof(stream)) { 10.115 - return EOF; 10.116 - } else { 10.117 - int c = ((char*)(stream->space))[stream->pos]; 10.118 - stream->pos++; 10.119 - return c; 10.120 - } 10.121 -}
11.1 --- a/ucx/memstream.h Wed Oct 10 09:34:13 2012 +0200 11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 11.3 @@ -1,55 +0,0 @@ 11.4 -#ifndef MEMSTREAM_H 11.5 -#define MEMSTREAM_H 11.6 - 11.7 -#include <stdio.h> 11.8 - 11.9 -/* as fmemopen is a C extension we provide our cross plattform stuff here */ 11.10 - 11.11 -#ifdef __cplusplus 11.12 -extern "C" { 11.13 -#endif 11.14 - 11.15 - 11.16 -/* as FILE is opaque, we don't do evil hacks but provide an alternative */ 11.17 -typedef struct UcxMemstream UcxMemstream; 11.18 - 11.19 -UcxMemstream *ucx_memopen(void *space, size_t length); 11.20 -void ucx_memclose(UcxMemstream* stream); 11.21 - 11.22 -/* 11.23 - * Moves the position of the stream to a new position relative to whence. 11.24 - * 11.25 - * SEEK_SET marks the start of the buffer 11.26 - * SEEK_CUR marks the current position 11.27 - * SEEK_END marks the first 0-byte in the buffer 11.28 - * 11.29 - * ucx_memseek returns 0 on success and -1 if the new position is beyond the 11.30 - * bounds of the allocated buffer. In that case the position of the stream 11.31 - * remains unchanged. 11.32 - * 11.33 - */ 11.34 -int ucx_memseek(UcxMemstream *stream, off_t offset, int whence); 11.35 -size_t ucx_memtell(UcxMemstream *stream); 11.36 - 11.37 -/* 11.38 - * returns non-zero, iff the current stream position has exceeded the last 11.39 - * available byte of the underlying buffer 11.40 - * 11.41 - */ 11.42 -int ucx_memeof(UcxMemstream *stream); 11.43 - 11.44 -/* memwrite, memread, memputc and memreadc shall not generate overflows */ 11.45 -size_t ucx_memio(void *d, size_t s, size_t n, UcxMemstream* m, _Bool read); 11.46 -#define ucx_memwrite(data, itemsize, nitems, memstream) \ 11.47 - ucx_memio(data, itemsize, nitems, memstream, 0) 11.48 -#define ucx_memread(data, itemsize, nitems, memstream) \ 11.49 - ucx_memio(data, itemsize, nitems, memstream, 1) 11.50 -int ucx_memputc(UcxMemstream *stream, int c); 11.51 -int ucx_memgetc(UcxMemstream *stream); 11.52 - 11.53 -#ifdef __cplusplus 11.54 -} 11.55 -#endif 11.56 - 11.57 -#endif /* MEMSTREAM_H */ 11.58 -