-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrarray.c
355 lines (311 loc) · 10.1 KB
/
strarray.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
/* ################################################################### */
/* Copyright 2022, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "xmalloc.h"
#include "strarray.h"
/* ========================================================================= */
/* Constructs the array out by splitting the strings 'in' according to the */
/* separators in 'seps'. */
/* if TRIM is set in options, leading and trailing spaces as recognized by */
/* isspace are removed before processing the string 'in'. */
/* the splitting process obey the following rules: */
/* if MERGESAMESEPS is set in options, a sequence of the same separator is */
/* considered as just one instance of this separator. */
/* if NOOCTAL is set in options, Numbers with a leading 0 will not be */
/* considered as octel. */
/* if MERGESEPS is set in options, a sequence of separators as defined */
/* in 'seps' is considered as just one instance of this separator. */
/* - in 'in' and "seps' \x obeys the C rules. */
/* - Sequences of characters in 'in' surrounded by " or ' are protected and */
/* passed as is. */
/* - a NULL entry is always added as the latest entry of the array of stings */
/* in 'out'. */
/* - the number of strings in 'out' is returned in 'count' (excluding the */
/* latest NULL entry). */
/* if KEEPQUOTES is set in options, The quotes are processed but not removed */
/* in the resulting strings. */
/* */
/* the return code is 0 if the function did its job without problem. */
/* -1 if there were a problem allocating memory. */
/* -2 if a " or a ' was not terminated. */
/* -3 if the first argument is NULL. */
/* */
/* It's the responsibility of the user to free the array and the strings */
/* allocated by the function. */
/* ========================================================================= */
int
strarray(char *in, char ***out, int *count, char *seps, int options)
{
int rc = 0;
char *buf;
char quote = ' ';
int len;
char *ptr;
char **p;
int in_special = 0;
int number;
char in_chars[] = "\\\'\"\?abfnrtv";
char out_chars[] = "\\\'\"\?\a\b\f\n\r\t\v";
/* initialize the strings counter */
/* """""""""""""""""""""""""""""" */
*count = 0;
/* determine the input length after possibly having to trim the leading and */
/* trailing spaces */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (in == NULL)
return -3;
len = strlen(in);
if (options & TRIM)
{
/* skip leading spaces */
while (*in != '\0' && isspace(*in))
{
len--;
in++;
}
/* cut trailing spaces */
while (len && isspace(*(in + len - 1)))
len--;
}
/* Allocate space for the first string pointer and and a buffer. */
/* the buffer length cannot be more athan the input length so we set its */
/* size to that */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
(*count)++;
*out = (char **)malloc(*count * sizeof(char *));
if (*out == NULL)
return -1;
buf = (*out)[*count - 1] = calloc(strlen(in) + 1, 1);
if (buf == NULL)
return -1;
/* manage the case of an empty input */
/* """"""""""""""""""""""""""""""""" */
if (len == 0)
{
(*count)--;
rc = 0;
}
/* process input string, mainly character after character */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""" */
while (len-- > 0 && *in != '\0')
{
char c = *in;
/* manage the start/end of protected sequence (between " and ') */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (*in == '"' || *in == '\'')
{
if (quote == ' ')
{
quote = c;
if (options & KEEPQUOTES)
*buf++ = c;
}
else if (c == quote)
{
quote = ' ';
if (options & KEEPQUOTES)
*buf++ = c;
}
else
*buf++ = c;
}
/* manage the start/end of protected characters and special octal and */
/* hexadecimal sequence */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
else if (c == '\\' && quote == ' ')
{
if (*(in + 1) != '\0')
in_special = 1;
else
*buf++ = c;
}
else if (in_special)
{
char *pos;
int n;
number = 0; /* will receive the converted value from octal or *
| hexadecimal sequence */
/* octal sequences (\n, \b ...) */
/* '''''''''''''''''''''''''''' */
if (!(options & NOOCTAL) && c >= '0' && c <= '7')
{
/* octal */
for (n = 2; n >= 0 && len-- > 0; n--)
{
if (c < '0' || c > '7')
{
in_special = 0;
break;
}
number += (c - '0') << (n * 3);
c = *++in;
}
in--;
if (in_special)
{
*buf++ = (char)number;
in_special = 0;
}
}
/* hexadecimal sequences (\x...) */
/* ''''''''''''''''''''''''''''' */
else if (c == 'x')
{
/* hexadecimal */
/* ''''''''''' */
for (n = 1; n >= 0 && len-- > 0; n--)
{
int base;
int incr;
c = *++in;
if (!isxdigit(c))
{
in_special = 0;
break;
}
incr = 0;
if (c >= 'a') /* assuming ASCII */
{
base = 'a';
incr = 10;
}
else if (c >= 'A')
{
base = 'A';
incr = 10;
}
else
base = '0';
number += (c - base + incr) << (n * 4);
}
if (in_special)
{
*buf++ = (char)number;
in_special = 0;
}
}
/* special c sequences (\n, \b ...) */
/* '''''''''''''''''''''''''''''''' */
else if ((pos = strchr(in_chars, c)) != NULL)
{
in_special = 0;
*buf++ = *(out_chars + (pos - in_chars));
}
else if ((pos = strchr(seps, c)) == NULL)
{
in_special = 0;
*buf++ = c;
}
else
{
in_special = 0;
*buf++ = '\\';
*buf++ = c;
}
}
/* manage the case where we hit a separator */
/* """""""""""""""""""""""""""""""""""""""" */
else if (strchr(seps, c) != NULL && quote == ' ')
{
*buf = '\0';
/* free the extra spaces */
ptr = xstrdup((*out)[*count - 1]);
if (ptr == NULL)
return -1;
free((*out)[*count - 1]);
(*out)[*count - 1] = ptr;
(*count)++;
p = (char **)realloc(*out, *count * sizeof(char *));
if (p == NULL)
{
rc = -1;
break;
}
else
{
*out = p;
buf = (*out)[*count - 1] = (char *)calloc(strlen(in) + 1, 1);
if (buf == NULL)
{
rc = -1;
break;
}
*buf = '\0';
}
/* in the case of the multi char separator option equals to 1 */
/* ignore the other occurrences of this separator in the input string */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (options & MERGESAMESEPS)
while (*(in + 1) == c && *(in + 1) != '\0' && len-- > 0)
in++;
else if (options & MERGESEPS)
while (strchr(seps, *(in + 1)) != NULL && *(in + 1) != '\0'
&& len-- > 0)
in++;
}
/* a normal character is read */
/* """""""""""""""""""""""""" */
else
*buf++ = c;
in++;
}
/* free the extra spaces in the last string if at least a string */
/* has been read */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (*count > 0)
{
ptr = xstrdup((*out)[*count - 1]);
if (ptr == NULL)
return -1;
free((*out)[*count - 1]);
(*out)[*count - 1] = ptr;
}
else
free((*out)[*count]);
/* add a NULL pointer at the end of the array */
/* """""""""""""""""""""""""""""""""""""""""" */
p = (char **)realloc(*out, ((*count) + 1) * sizeof(char *));
if (p == NULL)
return -1;
else
*out = p;
(*out)[*count] = NULL;
/* manage the case of unbalanced quotes */
/* """""""""""""""""""""""""""""""""""" */
if (quote == ' ')
return rc;
else
return -2;
}
#ifdef TEST
int
main(int argc, char *argv[])
{
char **tokens;
int rc, i, count;
if (argc != 3)
{
printf("usage: %s strings TRIM(1)|MERGESAMESEPS(2)|MERGESEPS(4)\n",
argv[0]);
exit(1);
}
printf("in='%s'\n", argv[1]);
rc = strarray(argv[1], &tokens, &count, " \t", atoi(argv[2]));
printf("rc=%d, count=%d\n", rc, count);
for (i = 0; i < count; i++)
printf("token[%d]='%s'\n", i, tokens[i]);
for (i = 0; i < count; i++)
free(tokens[i]);
free(tokens);
}
#endif