src/server/ucx/buffer.c

Mon, 14 Oct 2013 13:36:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 Oct 2013 13:36:28 +0200
changeset 94
6b15a094d996
parent 71
069c152f6272
permissions
-rw-r--r--

minor fixes in snprintf and build system

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

mercurial