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
|
/* str.c -- A C library for string manipulation
*
* Copyright (C) 2009-2011 Francesco Abbate
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "str.h"
static inline size_t
str_sz_round (size_t sz)
{
#define MINSIZE (1 << 4)
size_t res = MINSIZE;
while (res < sz)
{
res *= 2;
/* integer overflow ? */
assert (res > 0);
}
return res;
}
static inline void
str_init_raw (str_ptr s, size_t len)
{
s->size = str_sz_round (len+1);
s->heap = malloc (s->size);
}
void
str_init (str_ptr s, int len)
{
if (len < 0)
len = 0;
str_init_raw (s, (size_t)len);
s->heap[0] = 0;
s->length = 0;
}
str_ptr
str_new (void)
{
str_ptr s = malloc (sizeof(str_t));
str_init_raw (s, 16);
s->heap[0] = 0;
s->length = 0;
return s;
}
void
str_free (str_ptr s)
{
if (s->size > 0)
free (s->heap);
s->heap = 0;
}
static void
str_init_from_c_raw (str_ptr s, const char *sf, size_t len)
{
str_init_raw (s, len);
memcpy (s->heap, sf, len+1);
s->length = len;
}
void
str_init_from_c (str_ptr s, const char *sf)
{
size_t len = strlen (sf);
str_init_from_c_raw (s, sf, len);
}
void
str_init_from_str (str_ptr s, const str_t sf)
{
str_init_from_c_raw (s, CSTR(sf), STR_LENGTH(sf));
}
void
str_size_check (str_t s, size_t reqlen)
{
char *old_heap;
if (reqlen+1 < s->size)
return;
old_heap = (s->size > 0 ? s->heap : NULL);
str_init_raw (s, reqlen);
if (old_heap)
{
memcpy (s->heap, old_heap, s->length+1);
free (old_heap);
}
}
void
str_copy (str_t d, const str_t s)
{
const size_t len = s->length;
str_size_check (d, len);
memcpy (d->heap, s->heap, len+1);
d->length = len;
}
void
str_copy_c (str_t d, const char *s)
{
const size_t len = strlen (s);
str_size_check (d, len);
memcpy (d->heap, s, len+1);
d->length = len;
}
void
str_copy_c_substr (str_t d, const char *s, int len)
{
len = (len >= 0 ? len : 0);
str_size_check (d, (size_t)len);
memcpy (d->heap, s, (size_t)len);
d->heap[len] = 0;
d->length = len;
}
void
str_append_c (str_t to, const char *from, int sep)
{
size_t flen = strlen (from);
int use_sep = (sep != 0 && to->length > 0);
size_t newlen = STR_LENGTH(to) + flen + (use_sep ? 1 : 0);
int idx = STR_LENGTH(to);
str_size_check (to, newlen);
if (use_sep)
to->heap[idx++] = sep;
memcpy (to->heap + idx, from, flen+1);
to->length = newlen;
}
void
str_append (str_t to, const str_t from, int sep)
{
str_append_c (to, CSTR(from), sep);
}
void
str_trunc (str_t s, int len)
{
if (len < 0 || (size_t)len >= STR_LENGTH(s))
return;
s->heap[len] = 0;
s->length = len;
}
void
str_get_basename (str_t to, const str_t from, int dirsep)
{
const char * ptr = strrchr (CSTR(from), dirsep);
if (ptr == NULL)
str_copy_c (to, CSTR(from));
else
str_copy_c (to, ptr + 1);
}
void
str_dirname (str_t to, const str_t from, int dirsep)
{
const char * ptr = strrchr (CSTR(from), dirsep);
if (ptr == NULL)
str_copy_c (to, CSTR(from));
else
str_copy_c_substr (to, CSTR(from), ptr - CSTR(from));
}
int
str_getline (str_t d, FILE *f)
{
char *res, *ptr;
int szres, pending = 0;
str_size_check (d, 0);
str_trunc (d, 0);
for (;;)
{
int j;
ptr = d->heap + d->length;
szres = d->size - d->length;
res = fgets (ptr, szres, f);
if (res == NULL)
{
if (feof (f) && pending)
return 0;
return (-1);
}
for (j = 0; j < szres; j++)
{
if (ptr[j] == '\n' || ptr[j] == 0)
{
ptr[j] = 0;
d->length += j;
break;
}
}
if (j < szres - 1)
break;
pending = 1;
str_size_check (d, 2 * d->length);
}
if (d->length > 0)
{
if (d->heap[d->length - 1] == '015円')
{
d->heap[d->length - 1] = 0;
d->length --;
}
}
return 0;
}
void
str_vprintf (str_t d, const char *fmt, int append, va_list ap)
{
#define STR_BUFSIZE 64
char buffer[STR_BUFSIZE];
char *xbuf;
int xbuf_size;
int ns;
va_list aq;
va_copy (aq, ap);
ns = vsnprintf (buffer, STR_BUFSIZE, fmt, ap);
if (ns >= STR_BUFSIZE)
{
xbuf_size = ns+1;
xbuf = malloc (xbuf_size);
vsnprintf (xbuf, xbuf_size, fmt, aq);
}
else
{
xbuf = buffer;
xbuf_size = 0;
}
va_end (aq);
if (append)
{
str_append_c (d, xbuf, 0);
if (xbuf_size > 0)
free (xbuf);
}
else
{
if (xbuf_size > 0)
{
free (d->heap);
d->heap = xbuf;
d->size = xbuf_size;
d->length = ns;
}
else
{
str_copy_c_substr (d, xbuf, ns);
}
}
#undef STR_BUFSIZE
}
void
str_printf (str_t d, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
str_vprintf (d, fmt, 0, ap);
va_end (ap);
}
void
str_printf_add (str_t d, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
str_vprintf (d, fmt, 1, ap);
va_end (ap);
}
void
str_pad (str_t s, int len, char sep)
{
int diff = len - s->length;
if (diff <= 0)
return;
str_size_check (s, (size_t)len-1);
memmove (s->heap + diff, s->heap, (s->length + 1) * sizeof(char));
memset (s->heap, sep, diff * sizeof(char));
s->length += diff;
}
|