-
Notifications
You must be signed in to change notification settings - Fork 5
/
csv.c
182 lines (162 loc) · 4.38 KB
/
csv.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
NOMEM = -2 /* out of memory signal */
};
static char *line = NULL; /* input chars */
static char *sline = NULL; /* line copy used by split */
static int maxline = 0; /* size of line[] and sline[] */
static char **field = NULL; /* field pointers */
static int maxfield = 0; /* size of fields[] */
static int nfield = 0; /* number of fields in field[] */
static int _initial_maxline = 0; /* initial value of maxline configured by the consumer, or 0 by default */
static int _initial_maxfield = 0; /* initial value of maxfield configured by the consumer, or 0 by default */
void init(int initmaxfield, int initmaxline) /* initialize the library with proper starting values */
{
maxline = _initial_maxline = initmaxline;
maxfield = _initial_maxfield = initmaxfield;
}
/* reset: set variables back to starting values */
static void reset(void)
{
free(line);
free(sline);
free(field);
line = NULL;
sline = NULL;
field = NULL;
maxline = _initial_maxline;
maxfield = _initial_maxfield;
nfield = 0;
}
/* endofline: check for and consume \r, \n, \r\n, or EOF */
static int endofline(FILE *fn, int c)
{
int eol = (c == '\r' || c == '\n');
if (c == '\r')
{
c = getc(fn);
if (c != '\n' && c != EOF)
ungetc(c, fn); /* read too far; put c back */
}
return eol;
}
/* advquoted: quoted field: return pointer to next separator*/
static char *advquoted(char *p, char *separator)
{
int i, j;
for (i = j = 0; p[j] != '\0'; i++, j++)
{
if (p[j] == '"' && p[++j] != '"')
{
int k = strcspn(p + j, separator);
memmove(p + i, p + j, k);
i += k;
j += k;
break;
}
p[i] = p[j];
}
p[i] = '\0';
return p + j;
}
/* csvfield: return pointer to n-th field */
char *csvfield(int n)
{
if (n < 0 || n >= nfield)
return NULL;
return field[n];
}
/* csvnfield: return number of fields */
int csvnfield(void)
{
return nfield;
}
/* split: split line into fields */
static int split(char *separator)
{
char *sepp; /* pointer to temporary character */
int sepc;
nfield = 0;
if (line[0] == '\0')
return 0;
strcpy(sline, line);
char *p = sline;
do
{
if (nfield >= maxfield)
{
maxfield *= 2; /* double character size */
char **newf = (char **)realloc(field, maxfield * sizeof(field[0]));
if (newf == NULL)
return NOMEM;
field = newf;
}
if (*p == '"')
sepp = advquoted(++p, separator); /* skip initial quote */
else
sepp = p + strspn(p, separator);
sepc = sepp[0];
sepp[0] = '\0'; /* terminate field */
field[nfield++] = p;
p = sepp + 1;
} while (sepc == ',');
return nfield;
}
/* csvgetline: get one line, grow as needed */
/* sample input: "LU",86.25,"11/4/1998","2:19PM",+4.0625 */
char *csvgetline(FILE *fn, char *separator)
{
int c, i;
char *newl, *news;
if (line == NULL)
{
maxline = maxfield = 1; /* allocate on first call */
line = (char *)malloc(maxline);
sline = (char *)malloc(maxline);
field = (char **)malloc(maxfield * sizeof(field[0]));
if (line == NULL || sline == NULL || field == NULL)
{
reset();
return NULL; /* out of memory */
}
}
for (i = 0; (c = getc(fn)) != EOF && !endofline(fn, c); i++)
{
if (i >= maxline - 1) /* grow line */
{
maxline *= 2;
newl = (char *)realloc(line, maxline);
news = (char *)realloc(sline, maxline);
if (newl == NULL || news == NULL)
{
reset();
return NULL; /* out of memory */
}
line = newl;
sline = news;
}
line[i] = c;
}
line[i] = '\0';
if (split(separator) == NOMEM)
{
reset();
return NULL; /* out of memory */
}
return (c == EOF && i == 0) ? NULL : line;
}
int main(void)
{
char *line;
init(10, 10);
while ((line = csvgetline(stdin, ",")) != NULL)
{
printf("line = '%s'\n", line);
for (int i = 0; i < csvnfield(); i++)
printf("field[%d] = '%s'\n", i, csvfield(i));
}
return 0;
}