-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtext.c
More file actions
58 lines (54 loc) · 1.31 KB
/
text.c
File metadata and controls
58 lines (54 loc) · 1.31 KB
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
enum split_state {
INCOL, INQUOTE, INCOLESC, INQUOTEESC, INTERCOL
};
void split(char *line, size_t length, char **cols, int ncols) {
if ((ncols < 1) || (cols == NULL)) return;
for (int i = 0; i < ncols; i++)
cols[i] = NULL;
char *ch = line;
enum split_state state = INCOL;
int curcol= 1;
*(cols++) = ch;
/* fprintf(stderr, "starting parse of \"%.*s\"\n", (int)length, line); */
while((curcol < ncols) && (*ch != '\n') && (ch - line < length)) {
/* fprintf(stderr, "ch %c state %d curcol %d\n", *ch, state, curcol); */
switch (state) {
case INCOL:
if (*ch == '\"')
state = INQUOTE;
else if (isspace(*ch))
state = INTERCOL;
ch++;
break;
case INQUOTE:
if (*ch == '\"')
state = INCOL;
ch++;
break;
case INCOLESC:
if (*ch != 'n' )
fprintf(stderr, "Invalid escape '\\%c'.\n", *ch);
state=INCOL;
ch++;
break;
case INQUOTEESC:
if ((*ch != 'n' ) && (*ch == '\"'))
fprintf(stderr, "Invalid escape '\\%c'.\n", *ch);
state=INQUOTE;
ch++;
break;
case INTERCOL:
if(!isspace(*ch)) {
*(cols++) = ch;
curcol++;
state = INCOL;
} else {
ch++;
}
break;
}
}
}