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