src/buffer.c

changeset 500
eb9e7bd40a8e
parent 489
af6be1e123aa
child 501
9a08f5e515cc
equal deleted inserted replaced
499:3dc9075df822 500:eb9e7bd40a8e
30 #include "cx/utils.h" 30 #include "cx/utils.h"
31 31
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <string.h> 33 #include <string.h>
34 34
35 CxBuffer cxBufferCreate( 35 CxBuffer *cxBufferCreate(
36 void *space, 36 void *space,
37 size_t capacity, 37 size_t capacity,
38 int flags 38 int flags
39 ) { 39 ) {
40 CxBuffer buffer = (CxBuffer) malloc(sizeof(cx_buffer_s)); 40 CxBuffer *buffer = (CxBuffer *) malloc(sizeof(cx_buffer_s));
41 if (buffer) { 41 if (buffer) {
42 buffer->flags = flags; 42 buffer->flags = flags;
43 if (!space) { 43 if (!space) {
44 buffer->bytes = malloc(capacity); 44 buffer->bytes = malloc(capacity);
45 if (!buffer->bytes) { 45 if (!buffer->bytes) {
58 } 58 }
59 59
60 return buffer; 60 return buffer;
61 } 61 }
62 62
63 void cxBufferDestroy(CxBuffer buffer) { 63 void cxBufferDestroy(CxBuffer *buffer) {
64 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) { 64 if ((buffer->flags & CX_BUFFER_FREE_CONTENTS) == CX_BUFFER_FREE_CONTENTS) {
65 free(buffer->bytes); 65 free(buffer->bytes);
66 } 66 }
67 free(buffer); 67 free(buffer);
68 } 68 }
69 69
70 CxBuffer cxBufferExtract( 70 CxBuffer *cxBufferExtract(
71 CxBuffer src, 71 CxBuffer *src,
72 size_t start, 72 size_t start,
73 size_t length, 73 size_t length,
74 int flags 74 int flags
75 ) { 75 ) {
76 if (src->size == 0 || length == 0 || 76 if (src->size == 0 || length == 0 ||
77 ((size_t) -1) - start < length || start + length > src->capacity) { 77 ((size_t) -1) - start < length || start + length > src->capacity) {
78 return NULL; 78 return NULL;
79 } 79 }
80 80
81 CxBuffer dst = (CxBuffer) malloc(sizeof(cx_buffer_s)); 81 CxBuffer *dst = (CxBuffer *) malloc(sizeof(cx_buffer_s));
82 if (dst) { 82 if (dst) {
83 dst->bytes = malloc(length); 83 dst->bytes = malloc(length);
84 if (!dst->bytes) { 84 if (!dst->bytes) {
85 free(dst); 85 free(dst);
86 return NULL; 86 return NULL;
93 } 93 }
94 return dst; 94 return dst;
95 } 95 }
96 96
97 int cxBufferSeek( 97 int cxBufferSeek(
98 CxBuffer buffer, 98 CxBuffer *buffer,
99 off_t offset, 99 off_t offset,
100 int whence 100 int whence
101 ) { 101 ) {
102 size_t npos; 102 size_t npos;
103 switch (whence) { 103 switch (whence) {
128 return 0; 128 return 0;
129 } 129 }
130 130
131 } 131 }
132 132
133 int cxBufferEof(CxBuffer buffer) { 133 int cxBufferEof(CxBuffer *buffer) {
134 return buffer->pos >= buffer->size; 134 return buffer->pos >= buffer->size;
135 } 135 }
136 136
137 int cxBufferMinimumCapacity( 137 int cxBufferMinimumCapacity(
138 CxBuffer buffer, 138 CxBuffer *buffer,
139 size_t additional_bytes 139 size_t additional_bytes
140 ) { 140 ) {
141 size_t newcap = buffer->capacity + additional_bytes; 141 size_t newcap = buffer->capacity + additional_bytes;
142 142
143 // overflow protection 143 // overflow protection
159 159
160 size_t cxBufferWrite( 160 size_t cxBufferWrite(
161 void const *ptr, 161 void const *ptr,
162 size_t size, 162 size_t size,
163 size_t nitems, 163 size_t nitems,
164 CxBuffer buffer 164 CxBuffer *buffer
165 ) { 165 ) {
166 size_t len; 166 size_t len;
167 if (cx_szmul(size, nitems, &len)) { 167 if (cx_szmul(size, nitems, &len)) {
168 return 0; 168 return 0;
169 } 169 }
200 200
201 size_t cxBufferRead( 201 size_t cxBufferRead(
202 void *ptr, 202 void *ptr,
203 size_t size, 203 size_t size,
204 size_t nitems, 204 size_t nitems,
205 CxBuffer buffer 205 CxBuffer *buffer
206 ) { 206 ) {
207 size_t len; 207 size_t len;
208 if (cx_szmul(size, nitems, &len)) { 208 if (cx_szmul(size, nitems, &len)) {
209 return 0; 209 return 0;
210 } 210 }
222 222
223 return len / size; 223 return len / size;
224 } 224 }
225 225
226 int cxBufferPut( 226 int cxBufferPut(
227 CxBuffer buffer, 227 CxBuffer *buffer,
228 int c 228 int c
229 ) { 229 ) {
230 if (buffer->pos >= buffer->capacity) { 230 if (buffer->pos >= buffer->capacity) {
231 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) { 231 if ((buffer->flags & CX_BUFFER_AUTO_EXTEND) == CX_BUFFER_AUTO_EXTEND) {
232 if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) { 232 if (cxBufferMinimumCapacity(buffer, buffer->capacity + 1)) {
244 buffer->size = buffer->pos; 244 buffer->size = buffer->pos;
245 } 245 }
246 return c; 246 return c;
247 } 247 }
248 248
249 int cxBufferGet(CxBuffer buffer) { 249 int cxBufferGet(CxBuffer *buffer) {
250 if (cxBufferEof(buffer)) { 250 if (cxBufferEof(buffer)) {
251 return EOF; 251 return EOF;
252 } else { 252 } else {
253 int c = buffer->bytes[buffer->pos]; 253 int c = buffer->bytes[buffer->pos];
254 buffer->pos++; 254 buffer->pos++;
255 return c; 255 return c;
256 } 256 }
257 } 257 }
258 258
259 size_t cxBufferPutString( 259 size_t cxBufferPutString(
260 CxBuffer buffer, 260 CxBuffer *buffer,
261 const char *str 261 const char *str
262 ) { 262 ) {
263 return cxBufferWrite(str, 1, strlen(str), buffer); 263 return cxBufferWrite(str, 1, strlen(str), buffer);
264 } 264 }
265 265
266 int cxBufferShiftLeft( 266 int cxBufferShiftLeft(
267 CxBuffer buffer, 267 CxBuffer *buffer,
268 size_t shift 268 size_t shift
269 ) { 269 ) {
270 if (shift >= buffer->size) { 270 if (shift >= buffer->size) {
271 buffer->pos = buffer->size = 0; 271 buffer->pos = buffer->size = 0;
272 } else { 272 } else {
281 } 281 }
282 return 0; 282 return 0;
283 } 283 }
284 284
285 int cxBufferShiftRight( 285 int cxBufferShiftRight(
286 CxBuffer buffer, 286 CxBuffer *buffer,
287 size_t shift 287 size_t shift
288 ) { 288 ) {
289 size_t req_capacity = buffer->size + shift; 289 size_t req_capacity = buffer->size + shift;
290 size_t movebytes; 290 size_t movebytes;
291 291
313 313
314 return 0; 314 return 0;
315 } 315 }
316 316
317 int cxBufferShift( 317 int cxBufferShift(
318 CxBuffer buffer, 318 CxBuffer *buffer,
319 off_t shift 319 off_t shift
320 ) { 320 ) {
321 if (shift < 0) { 321 if (shift < 0) {
322 return cxBufferShiftLeft(buffer, (size_t) (-shift)); 322 return cxBufferShiftLeft(buffer, (size_t) (-shift));
323 } else if (shift > 0) { 323 } else if (shift > 0) {

mercurial