2 weeks ago
move ui_customwidget_create to separate file (GTK)
174 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, 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 "cx/buffer.h" | |
30 | ||
31 | #include <stdio.h> | |
32 | #include <string.h> | |
440 | 33 | #include <errno.h> |
34 | ||
35 | static int buffer_copy_on_write(CxBuffer* buffer) { | |
36 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0; | |
37 | void *newspace = cxMalloc(buffer->allocator, buffer->capacity); | |
38 | if (NULL == newspace) return -1; | |
39 | memcpy(newspace, buffer->space, buffer->size); | |
40 | buffer->space = newspace; | |
41 | buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE; | |
42 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
43 | return 0; | |
44 | } | |
174 | 45 | |
46 | int cxBufferInit( | |
47 | CxBuffer *buffer, | |
48 | void *space, | |
49 | size_t capacity, | |
324 | 50 | const CxAllocator *allocator, |
174 | 51 | int flags |
52 | ) { | |
440 | 53 | if (allocator == NULL) { |
54 | allocator = cxDefaultAllocator; | |
55 | } | |
56 | if (flags & CX_BUFFER_COPY_ON_EXTEND) { | |
57 | flags |= CX_BUFFER_AUTO_EXTEND; | |
58 | } | |
174 | 59 | buffer->allocator = allocator; |
60 | buffer->flags = flags; | |
61 | if (!space) { | |
62 | buffer->bytes = cxMalloc(allocator, capacity); | |
63 | if (buffer->bytes == NULL) { | |
440 | 64 | return -1; // LCOV_EXCL_LINE |
174 | 65 | } |
66 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
67 | } else { | |
68 | buffer->bytes = space; | |
69 | } | |
70 | buffer->capacity = capacity; | |
71 | buffer->size = 0; | |
72 | buffer->pos = 0; | |
73 | ||
440 | 74 | buffer->flush = NULL; |
174 | 75 | |
76 | return 0; | |
77 | } | |
78 | ||
440 | 79 | int cxBufferEnableFlushing( |
80 | CxBuffer *buffer, | |
81 | CxBufferFlushConfig config | |
82 | ) { | |
83 | buffer->flush = malloc(sizeof(CxBufferFlushConfig)); | |
84 | if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE | |
85 | memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig)); | |
86 | return 0; | |
87 | } | |
88 | ||
174 | 89 | void cxBufferDestroy(CxBuffer *buffer) { |
440 | 90 | if (buffer->flags & CX_BUFFER_FREE_CONTENTS) { |
174 | 91 | cxFree(buffer->allocator, buffer->bytes); |
92 | } | |
440 | 93 | free(buffer->flush); |
94 | memset(buffer, 0, sizeof(CxBuffer)); | |
174 | 95 | } |
96 | ||
97 | CxBuffer *cxBufferCreate( | |
98 | void *space, | |
99 | size_t capacity, | |
324 | 100 | const CxAllocator *allocator, |
174 | 101 | int flags |
102 | ) { | |
440 | 103 | if (allocator == NULL) { |
104 | allocator = cxDefaultAllocator; | |
105 | } | |
174 | 106 | CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); |
107 | if (buf == NULL) return NULL; | |
108 | if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { | |
109 | return buf; | |
110 | } else { | |
440 | 111 | // LCOV_EXCL_START |
174 | 112 | cxFree(allocator, buf); |
113 | return NULL; | |
440 | 114 | // LCOV_EXCL_STOP |
174 | 115 | } |
116 | } | |
117 | ||
118 | void cxBufferFree(CxBuffer *buffer) { | |
440 | 119 | if (buffer == NULL) return; |
120 | const CxAllocator *allocator = buffer->allocator; | |
121 | cxBufferDestroy(buffer); | |
122 | cxFree(allocator, buffer); | |
174 | 123 | } |
124 | ||
125 | int cxBufferSeek( | |
126 | CxBuffer *buffer, | |
127 | off_t offset, | |
128 | int whence | |
129 | ) { | |
130 | size_t npos; | |
131 | switch (whence) { | |
132 | case SEEK_CUR: | |
133 | npos = buffer->pos; | |
134 | break; | |
135 | case SEEK_END: | |
136 | npos = buffer->size; | |
137 | break; | |
138 | case SEEK_SET: | |
139 | npos = 0; | |
140 | break; | |
141 | default: | |
142 | return -1; | |
143 | } | |
144 | ||
145 | size_t opos = npos; | |
146 | npos += offset; | |
147 | ||
148 | if ((offset > 0 && npos < opos) || (offset < 0 && npos > opos)) { | |
440 | 149 | errno = EOVERFLOW; |
174 | 150 | return -1; |
151 | } | |
152 | ||
440 | 153 | if (npos > buffer->size) { |
174 | 154 | return -1; |
155 | } else { | |
156 | buffer->pos = npos; | |
157 | return 0; | |
158 | } | |
159 | ||
160 | } | |
161 | ||
162 | void cxBufferClear(CxBuffer *buffer) { | |
440 | 163 | if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) { |
164 | memset(buffer->bytes, 0, buffer->size); | |
165 | } | |
174 | 166 | buffer->size = 0; |
167 | buffer->pos = 0; | |
168 | } | |
169 | ||
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
170 | void cxBufferReset(CxBuffer *buffer) { |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
171 | buffer->size = 0; |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
172 | buffer->pos = 0; |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
173 | } |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
174 | |
440 | 175 | bool cxBufferEof(const CxBuffer *buffer) { |
174 | 176 | return buffer->pos >= buffer->size; |
177 | } | |
178 | ||
179 | int cxBufferMinimumCapacity( | |
180 | CxBuffer *buffer, | |
181 | size_t newcap | |
182 | ) { | |
183 | if (newcap <= buffer->capacity) { | |
184 | return 0; | |
185 | } | |
186 | ||
440 | 187 | const int force_copy_flags = CX_BUFFER_COPY_ON_WRITE | CX_BUFFER_COPY_ON_EXTEND; |
188 | if (buffer->flags & force_copy_flags) { | |
189 | void *newspace = cxMalloc(buffer->allocator, newcap); | |
190 | if (NULL == newspace) return -1; | |
191 | memcpy(newspace, buffer->space, buffer->size); | |
192 | buffer->space = newspace; | |
193 | buffer->capacity = newcap; | |
194 | buffer->flags &= ~force_copy_flags; | |
195 | buffer->flags |= CX_BUFFER_FREE_CONTENTS; | |
196 | return 0; | |
197 | } else if (cxReallocate(buffer->allocator, | |
174 | 198 | (void **) &buffer->bytes, newcap) == 0) { |
199 | buffer->capacity = newcap; | |
200 | return 0; | |
201 | } else { | |
440 | 202 | return -1; // LCOV_EXCL_LINE |
174 | 203 | } |
204 | } | |
205 | ||
440 | 206 | static size_t cx_buffer_flush_helper( |
207 | const CxBuffer *buffer, | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
208 | const unsigned char *src, |
174 | 209 | size_t size, |
210 | size_t nitems | |
211 | ) { | |
440 | 212 | // flush data from an arbitrary source |
213 | // does not need to be the buffer's contents | |
214 | size_t max_items = buffer->flush->blksize / size; | |
215 | size_t fblocks = 0; | |
216 | size_t flushed_total = 0; | |
217 | while (nitems > 0 && fblocks < buffer->flush->blkmax) { | |
218 | fblocks++; | |
219 | size_t items = nitems > max_items ? max_items : nitems; | |
220 | size_t flushed = buffer->flush->wfunc( | |
221 | src, size, items, buffer->flush->target); | |
174 | 222 | if (flushed > 0) { |
440 | 223 | flushed_total += flushed; |
224 | src += flushed * size; | |
225 | nitems -= flushed; | |
174 | 226 | } else { |
227 | // if no bytes can be flushed out anymore, we give up | |
228 | break; | |
229 | } | |
230 | } | |
440 | 231 | return flushed_total; |
232 | } | |
233 | ||
234 | static size_t cx_buffer_flush_impl(CxBuffer *buffer, size_t size) { | |
235 | // flush the current contents of the buffer | |
236 | unsigned char *space = buffer->bytes; | |
237 | size_t remaining = buffer->pos / size; | |
238 | size_t flushed_total = cx_buffer_flush_helper( | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
239 | buffer, space, size, remaining); |
440 | 240 | |
241 | // shift the buffer left after flushing | |
242 | // IMPORTANT: up to this point, copy on write must have been | |
243 | // performed already, because we can't do error handling here | |
244 | cxBufferShiftLeft(buffer, flushed_total*size); | |
245 | ||
246 | return flushed_total; | |
247 | } | |
248 | ||
249 | size_t cxBufferFlush(CxBuffer *buffer) { | |
250 | if (buffer_copy_on_write(buffer)) return 0; | |
251 | return cx_buffer_flush_impl(buffer, 1); | |
174 | 252 | } |
253 | ||
254 | size_t cxBufferWrite( | |
324 | 255 | const void *ptr, |
174 | 256 | size_t size, |
257 | size_t nitems, | |
258 | CxBuffer *buffer | |
259 | ) { | |
260 | // optimize for easy case | |
261 | if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) { | |
440 | 262 | if (buffer_copy_on_write(buffer)) return 0; |
174 | 263 | memcpy(buffer->bytes + buffer->pos, ptr, nitems); |
264 | buffer->pos += nitems; | |
265 | if (buffer->pos > buffer->size) { | |
266 | buffer->size = buffer->pos; | |
267 | } | |
268 | return nitems; | |
269 | } | |
270 | ||
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
271 | size_t len, total_flushed = 0; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
272 | cx_buffer_write_retry: |
174 | 273 | if (cx_szmul(size, nitems, &len)) { |
440 | 274 | errno = EOVERFLOW; |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
275 | return total_flushed; |
440 | 276 | } |
277 | if (buffer->pos > SIZE_MAX - len) { | |
278 | errno = EOVERFLOW; | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | return total_flushed; |
174 | 280 | } |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
281 | |
174 | 282 | size_t required = buffer->pos + len; |
283 | bool perform_flush = false; | |
284 | if (required > buffer->capacity) { | |
440 | 285 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
286 | if (buffer->flush != NULL && required > buffer->flush->threshold) { | |
174 | 287 | perform_flush = true; |
288 | } else { | |
289 | if (cxBufferMinimumCapacity(buffer, required)) { | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
290 | return total_flushed; // LCOV_EXCL_LINE |
174 | 291 | } |
292 | } | |
293 | } else { | |
440 | 294 | if (buffer->flush != NULL) { |
174 | 295 | perform_flush = true; |
296 | } else { | |
440 | 297 | // truncate data, if we can neither extend nor flush |
174 | 298 | len = buffer->capacity - buffer->pos; |
299 | if (size > 1) { | |
300 | len -= len % size; | |
301 | } | |
440 | 302 | nitems = len / size; |
174 | 303 | } |
304 | } | |
305 | } | |
306 | ||
440 | 307 | // check here and not above because of possible truncation |
174 | 308 | if (len == 0) { |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
309 | return total_flushed; |
174 | 310 | } |
311 | ||
440 | 312 | // check if we need to copy |
313 | if (buffer_copy_on_write(buffer)) return 0; | |
174 | 314 | |
440 | 315 | // perform the operation |
316 | if (perform_flush) { | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
317 | size_t items_flushed; |
440 | 318 | if (buffer->pos == 0) { |
319 | // if we don't have data in the buffer, but are instructed | |
320 | // to flush, it means that we are supposed to relay the data | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
321 | items_flushed = cx_buffer_flush_helper(buffer, ptr, size, nitems); |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
322 | if (items_flushed == 0) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
323 | // we needed to relay data, but could not flush anything |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
324 | // i.e. we have to give up to avoid endless trying |
440 | 325 | return 0; |
174 | 326 | } |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
327 | nitems -= items_flushed; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
328 | total_flushed += items_flushed; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
329 | if (nitems > 0) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
330 | ptr = ((unsigned char*)ptr) + items_flushed * size; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
331 | goto cx_buffer_write_retry; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
332 | } |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
333 | return total_flushed; |
440 | 334 | } else { |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
335 | items_flushed = cx_buffer_flush_impl(buffer, size); |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
336 | if (items_flushed == 0) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
337 | // flush target is full, let's try to truncate |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
338 | size_t remaining_space; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
339 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
340 | remaining_space = buffer->flush->threshold > buffer->pos |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
341 | ? buffer->flush->threshold - buffer->pos |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
342 | : 0; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
343 | } else { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
344 | remaining_space = buffer->capacity > buffer->pos |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
345 | ? buffer->capacity - buffer->pos |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
346 | : 0; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
347 | } |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
348 | nitems = remaining_space / size; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
349 | if (nitems == 0) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
350 | return total_flushed; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
351 | } |
174 | 352 | } |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
353 | goto cx_buffer_write_retry; |
174 | 354 | } |
355 | } else { | |
356 | memcpy(buffer->bytes + buffer->pos, ptr, len); | |
357 | buffer->pos += len; | |
358 | if (buffer->pos > buffer->size) { | |
359 | buffer->size = buffer->pos; | |
360 | } | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
361 | return total_flushed + nitems; |
174 | 362 | } |
363 | } | |
364 | ||
440 | 365 | size_t cxBufferAppend( |
366 | const void *ptr, | |
367 | size_t size, | |
368 | size_t nitems, | |
369 | CxBuffer *buffer | |
370 | ) { | |
371 | size_t pos = buffer->pos; | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
372 | size_t append_pos = buffer->size; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
373 | buffer->pos = append_pos; |
440 | 374 | size_t written = cxBufferWrite(ptr, size, nitems, buffer); |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
375 | // the buffer might have been flushed |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
376 | // we must compute a possible delta for the position |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
377 | // expected: pos = append_pos + written |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
378 | // -> if this is not the case, there is a delta |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
379 | size_t delta = append_pos + written*size - buffer->pos; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
380 | if (delta > pos) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
381 | buffer->pos = 0; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
382 | } else { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
383 | buffer->pos = pos - delta; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
384 | } |
440 | 385 | return written; |
386 | } | |
387 | ||
174 | 388 | int cxBufferPut( |
389 | CxBuffer *buffer, | |
390 | int c | |
391 | ) { | |
392 | c &= 0xFF; | |
393 | unsigned char const ch = c; | |
394 | if (cxBufferWrite(&ch, 1, 1, buffer) == 1) { | |
395 | return c; | |
396 | } else { | |
397 | return EOF; | |
398 | } | |
399 | } | |
400 | ||
440 | 401 | int cxBufferTerminate(CxBuffer *buffer) { |
402 | bool success = 0 == cxBufferPut(buffer, 0); | |
403 | if (success) { | |
404 | buffer->pos--; | |
405 | buffer->size--; | |
406 | return 0; | |
407 | } else { | |
408 | return -1; | |
409 | } | |
410 | } | |
411 | ||
174 | 412 | size_t cxBufferPutString( |
413 | CxBuffer *buffer, | |
414 | const char *str | |
415 | ) { | |
416 | return cxBufferWrite(str, 1, strlen(str), buffer); | |
417 | } | |
418 | ||
419 | size_t cxBufferRead( | |
420 | void *ptr, | |
421 | size_t size, | |
422 | size_t nitems, | |
423 | CxBuffer *buffer | |
424 | ) { | |
425 | size_t len; | |
426 | if (cx_szmul(size, nitems, &len)) { | |
440 | 427 | errno = EOVERFLOW; |
174 | 428 | return 0; |
429 | } | |
430 | if (buffer->pos + len > buffer->size) { | |
431 | len = buffer->size - buffer->pos; | |
432 | if (size > 1) len -= len % size; | |
433 | } | |
434 | ||
435 | if (len <= 0) { | |
436 | return len; | |
437 | } | |
438 | ||
439 | memcpy(ptr, buffer->bytes + buffer->pos, len); | |
440 | buffer->pos += len; | |
441 | ||
442 | return len / size; | |
443 | } | |
444 | ||
445 | int cxBufferGet(CxBuffer *buffer) { | |
446 | if (cxBufferEof(buffer)) { | |
447 | return EOF; | |
448 | } else { | |
449 | int c = buffer->bytes[buffer->pos]; | |
450 | buffer->pos++; | |
451 | return c; | |
452 | } | |
453 | } | |
454 | ||
455 | int cxBufferShiftLeft( | |
456 | CxBuffer *buffer, | |
457 | size_t shift | |
458 | ) { | |
459 | if (shift >= buffer->size) { | |
460 | buffer->pos = buffer->size = 0; | |
461 | } else { | |
440 | 462 | if (buffer_copy_on_write(buffer)) return -1; |
174 | 463 | memmove(buffer->bytes, buffer->bytes + shift, buffer->size - shift); |
464 | buffer->size -= shift; | |
465 | ||
466 | if (buffer->pos >= shift) { | |
467 | buffer->pos -= shift; | |
468 | } else { | |
469 | buffer->pos = 0; | |
470 | } | |
471 | } | |
472 | return 0; | |
473 | } | |
474 | ||
475 | int cxBufferShiftRight( | |
476 | CxBuffer *buffer, | |
477 | size_t shift | |
478 | ) { | |
440 | 479 | if (buffer->size > SIZE_MAX - shift) { |
480 | errno = EOVERFLOW; | |
481 | return -1; | |
482 | } | |
174 | 483 | size_t req_capacity = buffer->size + shift; |
484 | size_t movebytes; | |
485 | ||
486 | // auto extend buffer, if required and enabled | |
487 | if (buffer->capacity < req_capacity) { | |
440 | 488 | if (buffer->flags & CX_BUFFER_AUTO_EXTEND) { |
174 | 489 | if (cxBufferMinimumCapacity(buffer, req_capacity)) { |
440 | 490 | return -1; // LCOV_EXCL_LINE |
174 | 491 | } |
492 | movebytes = buffer->size; | |
493 | } else { | |
494 | movebytes = buffer->capacity - shift; | |
495 | } | |
496 | } else { | |
497 | movebytes = buffer->size; | |
498 | } | |
499 | ||
440 | 500 | if (movebytes > 0) { |
501 | if (buffer_copy_on_write(buffer)) return -1; | |
502 | memmove(buffer->bytes + shift, buffer->bytes, movebytes); | |
503 | buffer->size = shift + movebytes; | |
504 | } | |
174 | 505 | |
506 | buffer->pos += shift; | |
507 | if (buffer->pos > buffer->size) { | |
508 | buffer->pos = buffer->size; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | int cxBufferShift( | |
515 | CxBuffer *buffer, | |
516 | off_t shift | |
517 | ) { | |
518 | if (shift < 0) { | |
519 | return cxBufferShiftLeft(buffer, (size_t) (-shift)); | |
520 | } else if (shift > 0) { | |
521 | return cxBufferShiftRight(buffer, (size_t) shift); | |
522 | } else { | |
523 | return 0; | |
524 | } | |
525 | } |