src/ucx/buffer.c

Wed, 31 Aug 2016 16:20:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 31 Aug 2016 16:20:58 +0200
changeset 57
eba880c1705c
parent 39
ac35daceb24c
permissions
-rw-r--r--

improves API and adds functions for strings

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

mercurial