src/ucx/buffer.c

Mon, 04 Nov 2013 10:55:27 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 04 Nov 2013 10:55:27 +0100
changeset 96
0185b13bf41f
parent 91
fac51f87def0
child 99
b9a6af0ae41a
permissions
-rw-r--r--

added document-root and pfx2dir nametrans safs

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

mercurial