23 memset(buffer->space, 0, length); |
23 memset(buffer->space, 0, length); |
24 buffer->flags |= UCX_BUFFER_AUTOFREE; |
24 buffer->flags |= UCX_BUFFER_AUTOFREE; |
25 } else { |
25 } else { |
26 buffer->space = space; |
26 buffer->space = space; |
27 } |
27 } |
28 buffer->length = length; |
28 buffer->size = length; |
29 |
29 |
30 buffer->pos = 0; |
30 buffer->pos = 0; |
31 } |
31 } |
32 |
32 |
33 return buffer; |
33 return buffer; |
34 } |
34 } |
35 |
35 |
36 void ucx_buffer_free(UcxBuffer *buffer) { |
36 void ucx_buffer_free(UcxBuffer *buffer) { |
37 if ((buffer->flags & UCX_BUFFER_AUTOFREE) > 0) { |
37 if (ucx_buffer_testflags(buffer, UCX_BUFFER_AUTOFREE)) { |
38 free(buffer->space); |
38 free(buffer->space); |
39 } |
39 } |
40 free(buffer); |
40 free(buffer); |
|
41 } |
|
42 |
|
43 UcxBuffer *ucx_buffer_extract( |
|
44 UcxBuffer *src, size_t start, size_t length, int flags) { |
|
45 if (length == 0) { |
|
46 length = src->size - start; |
|
47 } |
|
48 if (start+length > src->size) { |
|
49 return NULL; |
|
50 } |
|
51 |
|
52 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer)); |
|
53 if (dst) { |
|
54 dst->space = malloc(length); |
|
55 if (!dst->space) { |
|
56 free(dst); |
|
57 return NULL; |
|
58 } |
|
59 dst->size = length; |
|
60 dst->flags = flags | UCX_BUFFER_AUTOFREE; |
|
61 dst->pos = 0; |
|
62 memcpy(dst->space, (char*)src->space+start, length); |
|
63 } |
|
64 return dst; |
41 } |
65 } |
42 |
66 |
43 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { |
67 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) { |
44 off_t npos; |
68 off_t npos; |
45 switch (whence) { |
69 switch (whence) { |
54 break; |
78 break; |
55 } |
79 } |
56 |
80 |
57 npos += offset; |
81 npos += offset; |
58 |
82 |
59 if (npos < 0 || npos > buffer->length) { |
83 if (npos < 0 || npos > buffer->size) { |
60 return -1; |
84 return -1; |
61 } else { |
85 } else { |
62 buffer->pos = npos; |
86 buffer->pos = npos; |
63 return 0; |
87 return 0; |
64 } |
88 } |
65 |
89 |
66 } |
90 } |
67 |
91 |
68 int ucx_buffer_eof(UcxBuffer *buffer) { |
92 int ucx_buffer_eof(UcxBuffer *buffer) { |
69 return buffer->pos >= buffer->length; |
93 return buffer->pos >= buffer->size; |
70 } |
94 } |
71 |
95 |
72 size_t ucx_buffer_tell(UcxBuffer *buffer) { |
96 size_t ucx_buffer_tell(UcxBuffer *buffer) { |
73 return buffer->pos; |
97 return buffer->pos; |
74 } |
98 } |
75 |
99 |
|
100 size_t ucx_buffer_size(UcxBuffer *buffer) { |
|
101 return buffer->size; |
|
102 } |
|
103 |
|
104 int ucx_buffer_testflags(UcxBuffer *buffer, int flags) { |
|
105 return (buffer->flags & flags) == flags; |
|
106 } |
|
107 |
76 size_t ucx_bufio(void* d, size_t s, size_t n, UcxBuffer *b, _Bool read) { |
108 size_t ucx_bufio(void* d, size_t s, size_t n, UcxBuffer *b, _Bool read) { |
77 size_t len; |
109 size_t len; |
78 if (b->pos + s*n > b->length) { |
110 if (b->pos + s*n > b->size) { |
79 len = b->length - b->pos; |
111 len = b->size - b->pos; |
80 if (s > 1) len -= len%s; |
112 if (s > 1) len -= len%s; |
81 } else { |
113 } else { |
82 len = s*n; |
114 len = s*n; |
83 } |
115 } |
84 |
116 |