|
1 #ifndef BUFFER_H |
|
2 #define BUFFER_H |
|
3 |
|
4 #include "ucx.h" |
|
5 #include <sys/types.h> |
|
6 #include <stdio.h> |
|
7 |
|
8 #ifdef __cplusplus |
|
9 extern "C" { |
|
10 #endif |
|
11 |
|
12 #define UCX_BUFFER_DEFAULT 0x00 |
|
13 #define UCX_BUFFER_AUTOFREE 0x01 |
|
14 /* the buffer may automatically double its size on write operations */ |
|
15 #define UCX_BUFFER_AUTOEXTEND 0x02 |
|
16 |
|
17 /* the user shall not modify values, but may get the latest pointer */ |
|
18 typedef struct { |
|
19 char *space; |
|
20 size_t pos; |
|
21 size_t capacity; |
|
22 size_t size; |
|
23 int flags; |
|
24 } UcxBuffer; |
|
25 |
|
26 /* if space is NULL, new space is allocated and the autofree flag is enforced */ |
|
27 UcxBuffer *ucx_buffer_new(void *space, size_t size, int flags); |
|
28 void ucx_buffer_free(UcxBuffer* buffer); |
|
29 |
|
30 /* |
|
31 * the autofree flag is enforced for the new buffer |
|
32 * if length is zero, the whole remaining buffer shall be extracted |
|
33 * the position of the new buffer is set to zero |
|
34 */ |
|
35 UcxBuffer* ucx_buffer_extract(UcxBuffer *src, |
|
36 size_t start, size_t length, int flags); |
|
37 #define ucx_buffer_clone(src,flags) \ |
|
38 ucx_buffer_extract(src, 0, 0, flags) |
|
39 |
|
40 /* |
|
41 * Moves the position of the buffer to a new position relative to whence. |
|
42 * |
|
43 * SEEK_SET marks the start of the buffer |
|
44 * SEEK_CUR marks the current position |
|
45 * SEEK_END marks the first 0-byte in the buffer |
|
46 * |
|
47 * ucx_memseek returns 0 on success and -1 if the new position is beyond the |
|
48 * bounds of the allocated buffer. In that case the position of the buffer |
|
49 * remains unchanged. |
|
50 * |
|
51 */ |
|
52 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence); |
|
53 |
|
54 /* |
|
55 * returns non-zero, if the current buffer position has exceeded the last |
|
56 * available byte of the underlying buffer |
|
57 * |
|
58 */ |
|
59 int ucx_buffer_eof(UcxBuffer *buffer); |
|
60 |
|
61 |
|
62 int ucx_buffere_extend(UcxBuffer *buffer, size_t len); |
|
63 |
|
64 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems, |
|
65 UcxBuffer *buffer); |
|
66 |
|
67 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems, |
|
68 UcxBuffer *buffer); |
|
69 |
|
70 /* when autoextend is enabled, ensure you get the latest pointer to the data */ |
|
71 //define ucx_buffer_write(data, itemsize, nitems, buffer) \ |
|
72 // ucx_bufio(data, itemsize, nitems, buffer, 0) |
|
73 //define ucx_buffer_read(data, itemsize, nitems, buffer) \ |
|
74 // ucx_bufio(data, itemsize, nitems, buffer, 1) |
|
75 int ucx_buffer_putc(UcxBuffer *b, int c); |
|
76 int ucx_buffer_getc(UcxBuffer *b); |
|
77 |
|
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 |
|
88 |
|
89 #define UCX_DEFAULT_BUFFER_SIZE 0x4000000 |
|
90 |
|
91 #define ucx_buffer_copy(s1,s2,r,w) \ |
|
92 ucx_buffer_generic_copy(s1, s2, (read_func)r, (write_func)w, \ |
|
93 UCX_DEFAULT_BUFFER_SIZE) |
|
94 |
|
95 #ifdef __cplusplus |
|
96 } |
|
97 #endif |
|
98 |
|
99 #endif /* BUFFER_H */ |
|
100 |