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