/
str.c
380 lines (310 loc) · 7.2 KB
/
str.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "lmalloc.h"
#include "str.h"
#include "fd.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ludis_tolower(c) (((c)>='A' && (c) <='Z') ? c|0x20 : c)
/*static char
ludis_tolower(char c)
{
return (c >= 'A' && c <= 'Z') ? c|0x20 : c;
}*/
char *
ludis_find(const char *s, int n, const char *needle, int needlelen)
{
char c1, c2, *haystack = (char *)s;
int len = n;
c2 = *needle++;
do {
do {
if (len-- == 0)
return NULL;
c1 = *haystack++;
} while (c1 != c2);
} while (memcmp(haystack, needle, needlelen) != 0);
return --haystack;
}
char *
ludis_findcase(const char *s, int n, const char *needle, int needlelen)
{
char *a, *b;
int al, bl, i;
for (i = 0; i + needlelen <= n; i++) {
a = (char *) s + i;
al = n - i;
b = (char *) needle;
bl = needlelen;
while (ludis_tolower(*a) == ludis_tolower(*b)) {
a++; b++; al--;
if (--bl == 0) /* end of needle, success */
return (char *) s + i;
}
}
return NULL;
}
/* str_new allocates a new Str buf of size n.
* Returns a pointer Str */
Str *
str_new(size_t n)
{
Str *s;
s = lcalloc(1, offsetof(Str, data) + n + 1);
s->len = 0;
s->cap = n;
s->data[n] = '\0';
memset(s->data, 0, n);
return s;
}
Str *
str_new_str(const char *data, size_t n)
{
Str *s = str_new(n);
memcpy(s->data, data, n);
s->len = n;
return s;
}
/* str_len returns the current len of the Str buf */
int
str_len(Str *s)
{
return s->len;
}
/* str_avail returns the free space in the Str buf */
int
str_avail(Str *s)
{
return s->cap - s->len;
}
/* str_cap returns the total capacity space in the Str buf */
int
str_cap(Str *s)
{
return s->cap;
}
/* str_grow guarantees that the Str buf has n free bytes
* returns a pointer to the new Str */
Str *
str_grow(Str *s, const int n)
{
Str *new_s;
if (str_avail(s) >= n)
return s;
new_s = lrealloc(s, offsetof(Str, data) + s->cap + n + 1);
new_s->cap =+ n;
new_s->data[new_s->cap] = '\0';
return new_s;
}
/* str_append appends n bytes from data to the end of Str
* returns a pointer to Str */
Str *
str_append(Str *s, const void *data, size_t n)
{
assert(n > 0);
s = str_grow(s, n);
memcpy(s->data + s->len, data, n);
s->len += n;
return s;
}
/* str_appends appends strlen(data) bytes from data to the end of Str
* returns a pointer to Str */
Str *
str_appends(Str *s, const char *data)
{
return str_append(s, data, strlen(data));
}
/* Concates b->data to s */
Str *
str_cat(Str *s, Str *b)
{
return str_append(s, b->data, b->len);
}
/* str_free free's Str */
void
str_free(Str *s)
{
free(s);
}
/* str_find finds the first occurance of the substring
* needle in the Str s.
* returns a pointer to the beginning of substr or NULL */
char *
str_find(Str *s, const char *needle, int n)
{
return ludis_find(s->data, s->len, needle, n);
}
/* str_findcase finds the first occurance of the substring
* needle in the Str s. Ignores the case of chars.
* returns a pointer to the beginning of substr or NULL */
char *
str_findcase(Str *s, const char *needle, int n)
{
return ludis_findcase(s->data, s->len, needle, n);
}
/* str_startswith checks if Str s starts with a
* sequence of bytes.
* returns 1 on success or 0 */
int
str_startswith(Str *s, const char *needle, int n)
{
if (n > s->len)
return 0;
while (n--)
if (s->data[n] != needle[n])
return 0;
return 1;
}
/* str_startswithcase checks if Str s starts with a
* sequence of bytes. Ignores the case of chars.
* returns 1 on success or 0 */
int
str_startswithcase(Str *s, const char *needle, int n)
{
if (n > s->len)
return 0;
while (n--)
if (ludis_tolower(s->data[n]) != ludis_tolower(needle[n]))
return 0;
return 1;
}
/* str_truncate discards all but the first unread byte in the
* Str */
/* str_reset discards the str.*/
/* buffer_new returns a new buffer */
Buffer *
buffer_new(size_t n) {
Buffer *b;
b = lmalloc(sizeof(Buffer));
b->off = 0;
b->s = str_new(n);
return b;
}
/* buffer_new_str returns a new buffer for Str */
Buffer *
buffer_new_str(Str *s) {
Buffer *b;
b = lmalloc(sizeof(Buffer));
b->off = 0;
b->s = s;
return b;
}
/* buffer_free free's buffer */
void
buffer_free(Buffer *b)
{
str_free(b->s);
free(b);
}
/* buffer_len returns number of bytes in the unread portion of the buffer */
int
buffer_len(Buffer *b)
{
return str_len(b->s) - b->off;
}
/* buffer_next gives you a pointer to the next byte and advances read offset
* by n. returns n or the number of bytes availible if n is larger than buf len.
* returns LUDIS_EEOF if buffer is empty or drained. */
int
buffer_next(Buffer *b, char **p, int n)
{
int len = buffer_len(b);
if (len <= 0) {
return LUDIS_EEOF;
}
n = MIN(len, n);
*p = b->s->data + b->off;
b->off += n;
return n;
}
/* reads the next n or until the buffer is drained.
* returns n bytes read or LUDIS_EEOF if no data is avail. */
int
buffer_read(Buffer *b, char *dest, int n)
{
int len = buffer_len(b);
if (len <= 0) {
return LUDIS_EEOF;
}
n = MIN(len, n);
memcpy(dest, b->s->data + b->off, n);
b->off += n;
return n;
}
/* reads len(b) bytes from buffer.
* returns n read or LUDIS_EEOF. */
int
buffer_reads(Buffer *b, char *p)
{
return buffer_read(b, p, str_len(b->s));
}
/* buffer_write appends the contents of data to the buffer
* returns n write bytes */
int
buffer_write(Buffer *b, const void *data, size_t n)
{
b->s = str_append(b->s, data, n);
return n;
}
/* buffer_write appends the contents of s to the buffer
* returns n write bytes */
int
buffer_writes(Buffer *b, const char *s)
{
return buffer_write(b, s, strlen(s));
}
/* buffer_read_byte read a single byte of the buffer
* returns a single byte or LUDIS_EEOF */
int
buffer_read_byte(Buffer *b)
{
int c;
if (buffer_len(b) <= 0) {
return LUDIS_EEOF;
}
c = b->s->data[b->off];
b->off++;
return c;
}
/* buffer_read_from reads from fd to buffer *b until EOF
* returns n read bytes or LUDIS_ESYS */
int
buffer_read_from(Buffer *b, int fd)
{
int n, nread = 0;
char buf[IOBUFLEN];
/* TODO: Truncate buffer first, if empty */
do {
n = fd_read(fd, buf, IOBUFLEN);
if (n > 0) {
buffer_write(b, buf, n);
nread += n;
}
} while(n > 0);
if (n == LUDIS_ESYS && errno != EAGAIN)
return n;
return nread;
}
/* buffer_write_to writes to fd until the buffer is drained
* returns n write bytes or LUDIS_ESYS */
int
buffer_write_to(Buffer *b, int fd)
{
int rv, n, nwrite = 0;
while ((n = buffer_len(b)) > 0) {
n = MIN(IOBUFLEN, n);
rv = fd_write(fd, b->s->data + b->off, n);
if (rv == LUDIS_ESYS)
return rv;
assert(rv == n);
b->off += n;
nwrite += n;
}
/* TODO: Truncate buffer */
return nwrite;
}