src/buffer.c

changeset 251
fae240d633fc
parent 250
b7d1317b138e
child 259
2f5dea574a75
equal deleted inserted replaced
250:b7d1317b138e 251:fae240d633fc
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "ucx/buffer.h"
30
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 UcxBuffer *ucx_buffer_new(void *space, size_t capacity, int flags) {
36 UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer));
37 if (buffer) {
38 buffer->flags = flags;
39 if (!space) {
40 buffer->space = (char*)malloc(capacity);
41 if (!buffer->space) {
42 free(buffer);
43 return NULL;
44 }
45 memset(buffer->space, 0, capacity);
46 buffer->flags |= UCX_BUFFER_AUTOFREE;
47 } else {
48 buffer->space = (char*)space;
49 }
50 buffer->capacity = capacity;
51 buffer->size = 0;
52
53 buffer->pos = 0;
54 }
55
56 return buffer;
57 }
58
59 void ucx_buffer_free(UcxBuffer *buffer) {
60 if ((buffer->flags & UCX_BUFFER_AUTOFREE) == UCX_BUFFER_AUTOFREE) {
61 free(buffer->space);
62 }
63 free(buffer);
64 }
65
66 UcxBuffer* ucx_buffer_extract(
67 UcxBuffer *src, size_t start, size_t length, int flags) {
68 if (src->size == 0 || length == 0 ||
69 ((size_t)-1) - start < length || start+length > src->capacity)
70 {
71 return NULL;
72 }
73
74 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
75 if (dst) {
76 dst->space = (char*)malloc(length);
77 if (!dst->space) {
78 free(dst);
79 return NULL;
80 }
81 dst->capacity = length;
82 dst->size = length;
83 dst->flags = flags | UCX_BUFFER_AUTOFREE;
84 dst->pos = 0;
85 memcpy(dst->space, src->space+start, length);
86 }
87 return dst;
88 }
89
90 int ucx_buffer_seek(UcxBuffer *buffer, off_t offset, int whence) {
91 size_t npos;
92 switch (whence) {
93 case SEEK_CUR:
94 npos = buffer->pos;
95 break;
96 case SEEK_END:
97 npos = buffer->size;
98 break;
99 case SEEK_SET:
100 npos = 0;
101 break;
102 default:
103 return -1;
104 }
105
106 size_t opos = npos;
107 npos += offset;
108
109 if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) {
110 return -1;
111 }
112
113 if (npos >= buffer->size) {
114 return -1;
115 } else {
116 buffer->pos = npos;
117 return 0;
118 }
119
120 }
121
122 int ucx_buffer_eof(UcxBuffer *buffer) {
123 return buffer->pos >= buffer->size;
124 }
125
126 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) {
127 size_t newcap = buffer->capacity;
128
129 if (buffer->capacity + len < buffer->capacity) {
130 return -1;
131 }
132
133 while (buffer->capacity + len > newcap) {
134 newcap <<= 1;
135 if (newcap < buffer->capacity) {
136 return -1;
137 }
138 }
139
140 char *newspace = (char*)realloc(buffer->space, newcap);
141 if (newspace) {
142 memset(newspace+buffer->size, 0, newcap-buffer->size);
143 buffer->space = newspace;
144 buffer->capacity = newcap;
145 } else {
146 return -1;
147 }
148
149 return 0;
150 }
151
152 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
153 UcxBuffer *buffer) {
154 size_t len = size * nitems;
155 size_t required = buffer->pos + len;
156 if (buffer->pos > required) {
157 return 0;
158 }
159
160 if (required > buffer->capacity) {
161 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
162 if (ucx_buffer_extend(buffer, required - buffer->capacity)) {
163 return 0;
164 }
165 } else {
166 len = buffer->capacity - buffer->pos;
167 if (size > 1) {
168 len -= len%size;
169 }
170 }
171 }
172
173 if (len == 0) {
174 return len;
175 }
176
177 memcpy(buffer->space + buffer->pos, ptr, len);
178 buffer->pos += len;
179 if(buffer->pos > buffer->size) {
180 buffer->size = buffer->pos;
181 }
182
183 return len / size;
184 }
185
186 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
187 UcxBuffer *buffer) {
188 size_t len = size * nitems;
189 if (buffer->pos + len > buffer->size) {
190 len = buffer->size - buffer->pos;
191 if (size > 1) len -= len%size;
192 }
193
194 if (len <= 0) {
195 return len;
196 }
197
198 memcpy(ptr, buffer->space + buffer->pos, len);
199 buffer->pos += len;
200
201 return len / size;
202 }
203
204 int ucx_buffer_putc(UcxBuffer *buffer, int c) {
205 if(buffer->pos >= buffer->capacity) {
206 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
207 if(ucx_buffer_extend(buffer, 1)) {
208 return EOF;
209 }
210 } else {
211 return EOF;
212 }
213 }
214
215 c &= 0xFF;
216 buffer->space[buffer->pos] = (char) c;
217 buffer->pos++;
218 if(buffer->pos > buffer->size) {
219 buffer->size = buffer->pos;
220 }
221 return c;
222 }
223
224 int ucx_buffer_getc(UcxBuffer *buffer) {
225 if (ucx_buffer_eof(buffer)) {
226 return EOF;
227 } else {
228 int c = buffer->space[buffer->pos];
229 buffer->pos++;
230 return c;
231 }
232 }
233
234 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) {
235 return ucx_buffer_write((const void*)str, 1, strlen(str), buffer);
236 }

mercurial