src/buffer.c

changeset 290
d5d6ab809ad3
parent 270
3d80d425543b
equal deleted inserted replaced
289:a5eabd407774 290:d5d6ab809ad3
235 buffer->pos++; 235 buffer->pos++;
236 return c; 236 return c;
237 } 237 }
238 } 238 }
239 239
240 size_t ucx_buffer_puts(UcxBuffer *buffer, char *str) { 240 size_t ucx_buffer_puts(UcxBuffer *buffer, const char *str) {
241 return ucx_buffer_write((const void*)str, 1, strlen(str), buffer); 241 return ucx_buffer_write((const void*)str, 1, strlen(str), buffer);
242 } 242 }
243
244 int ucx_buffer_shift_left(UcxBuffer* buffer, size_t shift) {
245 if (shift >= buffer->size) {
246 buffer->pos = buffer->size = 0;
247 } else {
248 memmove(buffer->space, buffer->space + shift, buffer->size - shift);
249 buffer->size -= shift;
250
251 if (buffer->pos >= shift) {
252 buffer->pos -= shift;
253 } else {
254 buffer->pos = 0;
255 }
256 }
257 return 0;
258 }
259
260 int ucx_buffer_shift_right(UcxBuffer* buffer, size_t shift) {
261 size_t req_capacity = buffer->size + shift;
262 size_t movebytes;
263
264 // auto extend buffer, if required and enabled
265 if (buffer->capacity < req_capacity) {
266 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
267 if (ucx_buffer_extend(buffer, req_capacity - buffer->capacity)) {
268 return 1;
269 }
270 movebytes = buffer->size;
271 } else {
272 movebytes = buffer->capacity - shift;
273 }
274 } else {
275 movebytes = buffer->size;
276 }
277
278 memmove(buffer->space + shift, buffer->space, movebytes);
279 buffer->size = shift+movebytes;
280
281 buffer->pos += shift;
282 if (buffer->pos > buffer->size) {
283 buffer->pos = buffer->size;
284 }
285
286 return 0;
287 }
288
289 int ucx_buffer_shift(UcxBuffer* buffer, off_t shift) {
290 if (shift < 0) {
291 return ucx_buffer_shift_left(buffer, (size_t) (-shift));
292 } else if (shift > 0) {
293 return ucx_buffer_shift_right(buffer, (size_t) shift);
294 } else {
295 return 0;
296 }
297 }

mercurial