1 #include <stdio.h>
2 #include <string.h>
3
4 /*
5 - split - divide a string into fields, like awk split()
6 = int split(char *string, char *fields[], int nfields, char *sep);
7 */
8 int /* number of fields, including overflow */
split(string,fields,nfields,sep)9 split(string, fields, nfields, sep)
10 char *string;
11 char *fields[]; /* list is not NULL-terminated */
12 int nfields; /* number of entries available in fields[] */
13 char *sep; /* "" white, "c" single char, "ab" [ab]+ */
14 {
15 char *p = string;
16 char c; /* latest character */
17 char sepc = sep[0];
18 char sepc2;
19 int fn;
20 char **fp = fields;
21 char *sepp;
22 int trimtrail;
23
24 /* white space */
25 if (sepc == '\0') {
26 while ((c = *p++) == ' ' || c == '\t')
27 continue;
28 p--;
29 trimtrail = 1;
30 sep = (char*) " \t"; /* note, code below knows this is 2 long */
31 sepc = ' ';
32 } else
33 trimtrail = 0;
34 sepc2 = sep[1]; /* now we can safely pick this up */
35
36 /* catch empties */
37 if (*p == '\0')
38 return(0);
39
40 /* single separator */
41 if (sepc2 == '\0') {
42 fn = nfields;
43 for (;;) {
44 *fp++ = p;
45 fn--;
46 if (fn == 0)
47 break;
48 while ((c = *p++) != sepc)
49 if (c == '\0')
50 return(nfields - fn);
51 *(p-1) = '\0';
52 }
53 /* we have overflowed the fields vector -- just count them */
54 fn = nfields;
55 for (;;) {
56 while ((c = *p++) != sepc)
57 if (c == '\0')
58 return(fn);
59 fn++;
60 }
61 /* not reached */
62 }
63
64 /* two separators */
65 if (sep[2] == '\0') {
66 fn = nfields;
67 for (;;) {
68 *fp++ = p;
69 fn--;
70 while ((c = *p++) != sepc && c != sepc2)
71 if (c == '\0') {
72 if (trimtrail && **(fp-1) == '\0')
73 fn++;
74 return(nfields - fn);
75 }
76 if (fn == 0)
77 break;
78 *(p-1) = '\0';
79 while ((c = *p++) == sepc || c == sepc2)
80 continue;
81 p--;
82 }
83 /* we have overflowed the fields vector -- just count them */
84 fn = nfields;
85 while (c != '\0') {
86 while ((c = *p++) == sepc || c == sepc2)
87 continue;
88 p--;
89 fn++;
90 while ((c = *p++) != '\0' && c != sepc && c != sepc2)
91 continue;
92 }
93 /* might have to trim trailing white space */
94 if (trimtrail) {
95 p--;
96 while ((c = *--p) == sepc || c == sepc2)
97 continue;
98 p++;
99 if (*p != '\0') {
100 if (fn == nfields+1)
101 *p = '\0';
102 fn--;
103 }
104 }
105 return(fn);
106 }
107
108 /* n separators */
109 fn = 0;
110 for (;;) {
111 if (fn < nfields)
112 *fp++ = p;
113 fn++;
114 for (;;) {
115 c = *p++;
116 if (c == '\0')
117 return(fn);
118 sepp = sep;
119 while ((sepc = *sepp++) != '\0' && sepc != c)
120 continue;
121 if (sepc != '\0') /* it was a separator */
122 break;
123 }
124 if (fn < nfields)
125 *(p-1) = '\0';
126 for (;;) {
127 c = *p++;
128 sepp = sep;
129 while ((sepc = *sepp++) != '\0' && sepc != c)
130 continue;
131 if (sepc == '\0') /* it wasn't a separator */
132 break;
133 }
134 p--;
135 }
136
137 /* not reached */
138 }
139
140 #ifdef TEST_SPLIT
141
142
143 /*
144 * test program
145 * pgm runs regression
146 * pgm sep splits stdin lines by sep
147 * pgm str sep splits str by sep
148 * pgm str sep n splits str by sep n times
149 */
150 int
main(argc,argv)151 main(argc, argv)
152 int argc;
153 char *argv[];
154 {
155 char buf[512];
156 int n;
157 # define MNF 10
158 char *fields[MNF];
159
160 if (argc > 4)
161 for (n = atoi(argv[3]); n > 0; n--) {
162 if(sizeof(buf)-1 < strlen(argv[1]))
163 {
164 exit(EXIT_FAILURE);
165 }
166 (void) strcpy(buf, argv[1]);
167 }
168 else if (argc > 3)
169 for (n = atoi(argv[3]); n > 0; n--) {
170 if(sizeof(buf)-1 < strlen(argv[1]))
171 {
172 exit(EXIT_FAILURE);
173 }
174 (void) strcpy(buf, argv[1]);
175 (void) split(buf, fields, MNF, argv[2]);
176 }
177 else if (argc > 2)
178 dosplit(argv[1], argv[2]);
179 else if (argc > 1)
180 while (fgets(buf, sizeof(buf), stdin) != NULL) {
181 buf[strlen(buf)-1] = '\0'; /* stomp newline */
182 dosplit(buf, argv[1]);
183 }
184 else
185 regress();
186
187 exit(0);
188 }
189
dosplit(string,seps)190 dosplit(string, seps)
191 char *string;
192 char *seps;
193 {
194 # define NF 5
195 char *fields[NF];
196 int nf;
197
198 nf = split(string, fields, NF, seps);
199 print(nf, NF, fields);
200 }
201
print(nf,nfp,fields)202 print(nf, nfp, fields)
203 int nf;
204 int nfp;
205 char *fields[];
206 {
207 int fn;
208 int bound;
209
210 bound = (nf > nfp) ? nfp : nf;
211 printf("%d:\t", nf);
212 for (fn = 0; fn < bound; fn++)
213 printf("\"%s\"%s", fields[fn], (fn+1 < nf) ? ", " : "\n");
214 }
215
216 #define RNF 5 /* some table entries know this */
217 struct {
218 char *str;
219 char *seps;
220 int nf;
221 char *fi[RNF];
222 } tests[] = {
223 "", " ", 0, { "" },
224 " ", " ", 2, { "", "" },
225 "x", " ", 1, { "x" },
226 "xy", " ", 1, { "xy" },
227 "x y", " ", 2, { "x", "y" },
228 "abc def g ", " ", 5, { "abc", "def", "", "g", "" },
229 " a bcd", " ", 4, { "", "", "a", "bcd" },
230 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
231 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
232
233 "", " _", 0, { "" },
234 " ", " _", 2, { "", "" },
235 "x", " _", 1, { "x" },
236 "x y", " _", 2, { "x", "y" },
237 "ab _ cd", " _", 2, { "ab", "cd" },
238 " a_b c ", " _", 5, { "", "a", "b", "c", "" },
239 "a b c_d e f", " _", 6, { "a", "b", "c", "d", "e f" },
240 " a b c d ", " _", 6, { "", "a", "b", "c", "d " },
241
242 "", " _~", 0, { "" },
243 " ", " _~", 2, { "", "" },
244 "x", " _~", 1, { "x" },
245 "x y", " _~", 2, { "x", "y" },
246 "ab _~ cd", " _~", 2, { "ab", "cd" },
247 " a_b c~", " _~", 5, { "", "a", "b", "c", "" },
248 "a b_c d~e f", " _~", 6, { "a", "b", "c", "d", "e f" },
249 "~a b c d ", " _~", 6, { "", "a", "b", "c", "d " },
250
251 "", " _~-", 0, { "" },
252 " ", " _~-", 2, { "", "" },
253 "x", " _~-", 1, { "x" },
254 "x y", " _~-", 2, { "x", "y" },
255 "ab _~- cd", " _~-", 2, { "ab", "cd" },
256 " a_b c~", " _~-", 5, { "", "a", "b", "c", "" },
257 "a b_c-d~e f", " _~-", 6, { "a", "b", "c", "d", "e f" },
258 "~a-b c d ", " _~-", 6, { "", "a", "b", "c", "d " },
259
260 "", " ", 0, { "" },
261 " ", " ", 2, { "", "" },
262 "x", " ", 1, { "x" },
263 "xy", " ", 1, { "xy" },
264 "x y", " ", 2, { "x", "y" },
265 "abc def g ", " ", 4, { "abc", "def", "g", "" },
266 " a bcd", " ", 3, { "", "a", "bcd" },
267 "a b c d e f", " ", 6, { "a", "b", "c", "d", "e f" },
268 " a b c d ", " ", 6, { "", "a", "b", "c", "d " },
269
270 "", "", 0, { "" },
271 " ", "", 0, { "" },
272 "x", "", 1, { "x" },
273 "xy", "", 1, { "xy" },
274 "x y", "", 2, { "x", "y" },
275 "abc def g ", "", 3, { "abc", "def", "g" },
276 "\t a bcd", "", 2, { "a", "bcd" },
277 " a \tb\t c ", "", 3, { "a", "b", "c" },
278 "a b c d e ", "", 5, { "a", "b", "c", "d", "e" },
279 "a b\tc d e f", "", 6, { "a", "b", "c", "d", "e f" },
280 " a b c d e f ", "", 6, { "a", "b", "c", "d", "e f " },
281
282 NULL, NULL, 0, { NULL },
283 };
284
regress()285 regress()
286 {
287 char buf[512];
288 int n;
289 char *fields[RNF+1];
290 int nf;
291 int i;
292 int printit;
293 char *f;
294
295 for (n = 0; tests[n].str != NULL; n++) {
296 (void) strcpy(buf, tests[n].str);
297 fields[RNF] = NULL;
298 nf = split(buf, fields, RNF, tests[n].seps);
299 printit = 0;
300 if (nf != tests[n].nf) {
301 printf("split `%s' by `%s' gave %d fields, not %d\n",
302 tests[n].str, tests[n].seps, nf, tests[n].nf);
303 printit = 1;
304 } else if (fields[RNF] != NULL) {
305 printf("split() went beyond array end\n");
306 printit = 1;
307 } else {
308 for (i = 0; i < nf && i < RNF; i++) {
309 f = fields[i];
310 if (f == NULL)
311 f = "(NULL)";
312 if (strcmp(f, tests[n].fi[i]) != 0) {
313 printf("split `%s' by `%s', field %d is `%s', not `%s'\n",
314 tests[n].str, tests[n].seps,
315 i, fields[i], tests[n].fi[i]);
316 printit = 1;
317 }
318 }
319 }
320 if (printit)
321 print(nf, RNF, fields);
322 }
323 }
324 #endif
325