ucx/buffer.c

Tue, 19 Sep 2017 15:32:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 19 Sep 2017 15:32:53 +0200
changeset 249
fb21858ad8da
parent 239
1634c3ea89da
child 250
b7d1317b138e
permissions
-rw-r--r--

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
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
65 UcxBuffer* ucx_buffer_extract(
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
70 return NULL;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
71 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
72
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
73 UcxBuffer *dst = (UcxBuffer*) malloc(sizeof(UcxBuffer));
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
74 if (dst) {
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
75 dst->space = (char*)malloc(length);
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
76 if (!dst->space) {
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
77 free(dst);
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
78 return NULL;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
79 }
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
80 dst->capacity = length;
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
81 dst->size = length;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
82 dst->flags = flags | UCX_BUFFER_AUTOFREE;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
83 dst->pos = 0;
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
84 memcpy(dst->space, src->space+start, length);
62
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
85 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
86 return dst;
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
87 }
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
6814aea8462d added extract function
Mike Becker <universe@uap-core.de>
parents: 61
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
125 int ucx_buffer_extend(UcxBuffer *buffer, size_t len) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
138
78
af355652f271 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 76
diff changeset
139 char *newspace = (char*)realloc(buffer->space, newcap);
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
140 if (newspace) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
141 memset(newspace+buffer->size, 0, newcap-buffer->size);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
142 buffer->space = newspace;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
143 buffer->capacity = newcap;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
144 } else {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
145 return -1;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
146 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
147
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
148 return 0;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
149 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
150
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
151 size_t ucx_buffer_write(const void *ptr, size_t size, size_t nitems,
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
152 UcxBuffer *buffer) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
175
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
176 memcpy(buffer->space + buffer->pos, ptr, len);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
177 buffer->pos += len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
178 if(buffer->pos > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
179 buffer->size = buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
180 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
181
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
182 return len / size;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
183 }
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
185 size_t ucx_buffer_read(void *ptr, size_t size, size_t nitems,
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
186 UcxBuffer *buffer) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
187 size_t len = size * nitems;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
188 if (buffer->pos + len > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
189 len = buffer->size - buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
192
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
193 if (len <= 0) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
194 return len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
195 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
196
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
197 memcpy(ptr, buffer->space + buffer->pos, len);
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
198 buffer->pos += len;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
199
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
204 if(buffer->pos >= buffer->capacity) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
205 if ((buffer->flags & UCX_BUFFER_AUTOEXTEND) == UCX_BUFFER_AUTOEXTEND) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
206 if(ucx_buffer_extend(buffer, 1)) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
207 return EOF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
208 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
209 } else {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
210 return EOF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
211 }
56
76caac0da4a0 added memstream to ucx - still little work to do
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 }
76
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
213
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
214 c &= 0xFF;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
215 buffer->space[buffer->pos] = (char) c;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
216 buffer->pos++;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
217 if(buffer->pos > buffer->size) {
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
218 buffer->size = buffer->pos;
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
219 }
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
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
655020a30e77 fixed buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
235 }

mercurial