Wed, 27 Feb 2013 16:59:02 +0100
happy 100th commit + removed deprecated sstrcat + fixed sstrncat
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
1 | #ifndef BUFFER_H |
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
2 | #define BUFFER_H |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
67
diff
changeset
|
4 | #include "ucx.h" |
62 | 5 | #include <sys/types.h> |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | #include <stdio.h> |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | #ifdef __cplusplus |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | extern "C" { |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | #endif |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | |
85
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
12 | /* no autoextend or autofree behaviour */ |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
13 | #define UCX_BUFFER_DEFAULT 0x00 |
85
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
14 | /* the buffer shall free the occupied memory space */ |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
15 | #define UCX_BUFFER_AUTOFREE 0x01 |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
16 | /* the buffer may automatically double its size on write operations */ |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
17 | #define UCX_BUFFER_AUTOEXTEND 0x02 |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | |
64
16590c9c497c
added autoextend feature to buffer
Mike Becker <universe@uap-core.de>
parents:
63
diff
changeset
|
19 | /* the user shall not modify values, but may get the latest pointer */ |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
20 | typedef struct { |
76 | 21 | char *space; |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
22 | size_t pos; |
76 | 23 | size_t capacity; |
63
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
24 | size_t size; |
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
25 | int flags; |
1d3500806565
discarded fascism from buffer
Mike Becker <universe@uap-core.de>
parents:
62
diff
changeset
|
26 | } UcxBuffer; |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | |
61
fb07a0ab9a17
added flag field to buffer
Mike Becker <universe@uap-core.de>
parents:
60
diff
changeset
|
28 | /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
76 | 29 | UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
30 | void ucx_buffer_free(UcxBuffer* buffer); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | /* |
62 | 33 | * the autofree flag is enforced for the new buffer |
34 | * if length is zero, the whole remaining buffer shall be extracted | |
35 | * the position of the new buffer is set to zero | |
36 | */ | |
76 | 37 | UcxBuffer* ucx_buffer_extract(UcxBuffer *src, |
62 | 38 | size_t start, size_t length, int flags); |
39 | #define ucx_buffer_clone(src,flags) \ | |
40 | ucx_buffer_extract(src, 0, 0, flags) | |
41 | ||
42 | /* | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
43 | * Moves the position of the buffer to a new position relative to whence. |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | * |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | * SEEK_SET marks the start of the buffer |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | * SEEK_CUR marks the current position |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | * SEEK_END marks the first 0-byte in the buffer |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | * |
85
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
49 | * ucx_buffer_seek returns 0 on success and -1 if the new position is beyond the |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
50 | * bounds of the allocated buffer. In that case the position of the buffer |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | * remains unchanged. |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | * |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | */ |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
54 | int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | |
85
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
56 | #define ucx_buffer_clear(buffer) memset(buffer->space, 0, buffer->size); \ |
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
57 | buffer->size = 0; buffer->pos = 0; |
0ef0df7aa2c2
fixed comments + added ucx_buffer_clear
Mike Becker <universe@uap-core.de>
parents:
81
diff
changeset
|
58 | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | /* |
76 | 60 | * returns non-zero, if the current buffer position has exceeded the last |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | * available byte of the underlying buffer |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | * |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | */ |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
64 | int ucx_buffer_eof(UcxBuffer *buffer); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
76 | 66 | |
67 | int ucx_buffere_extend(UcxBuffer *buffer, size_t len); | |
68 | ||
69 | size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, | |
70 | UcxBuffer *buffer); | |
71 | ||
72 | size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, | |
73 | UcxBuffer *buffer); | |
74 | ||
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
75 | int ucx_buffer_putc(UcxBuffer *b, int c); |
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
76 | int ucx_buffer_getc(UcxBuffer *b); |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | |
76 | 78 | |
79 | /* | |
80 | * copies all bytes from s1 to s2 | |
81 | * uses the read function r to read from s1 und writes the data using the | |
82 | * write function w to s2 | |
83 | * returns the number of bytes copied | |
84 | */ | |
85 | size_t ucx_buffer_generic_copy(void *s1, void *s2, read_func r, write_func w, | |
86 | size_t bufsize); | |
87 | ||
86
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
88 | size_t ucx_buffer_generic_ncopy(void *s1, void *s2, read_func r, write_func w, |
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
89 | size_t bufsize, size_t n); |
76 | 90 | |
86
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
91 | #define UCX_DEFAULT_BUFFER_SIZE 0x1000 |
76 | 92 | |
93 | #define ucx_buffer_copy(s1,s2,r,w) \ | |
94 | ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ | |
95 | UCX_DEFAULT_BUFFER_SIZE) | |
96 | ||
86
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
97 | #define ucx_buffer_ncopy(s1,s2,r,w, n) \ |
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
98 | ucx_buffer_generic_ncopy(s1, s2, (read_func)r, (write_func)w, \ |
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
99 | UCX_DEFAULT_BUFFER_SIZE, n) |
55bf819cbc88
added generic ncopy
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
85
diff
changeset
|
100 | |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | #ifdef __cplusplus |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | } |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | #endif |
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
60
abae4669fba7
renamed UcxMemstream to UcxBuffer
Mike Becker <universe@uap-core.de>
parents:
58
diff
changeset
|
105 | #endif /* BUFFER_H */ |
56
76caac0da4a0
added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 |