57 #define ST_TAILSPACE 3
58 #define ST_END_QUOTE 4
59 #define istspace iswspace
78 snextch(
struct sinput *in)
83 if ((ch = fgetc(in->in)) == EOF) {
85 GERB_MESSAGE(
"errno:%d", errno);
94 ch = (
unsigned char) *(in->src)++;
104 wnextch(
struct winput *in)
119 csv_parse_str(
struct sinput *in,
char *buf,
size_t bn,
char *row[],
int rn,
int sep,
int flags)
121 int trim, quotes, ch, state, r, j, t, inquotes;
123 trim = flags & CSV_TRIM;
124 quotes = flags & CSV_QUOTES;
129 memset(row, 0,
sizeof(
char *) * rn);
131 while (rn && bn && (ch = snextch(in)) > 0) {
134 if (ch !=
'\n' && ch != sep && isspace(ch)) {
140 }
else if (quotes && ch ==
'"') {
150 state = ST_END_QUOTE;
153 }
else if (ch == sep || ch ==
'\n') {
154 row[r++] = buf; rn--;
165 }
else if (quotes && ch ==
'"') {
167 GERB_MESSAGE(_(
"%d: unexpected quote in element"),errno);
171 if (!trim || isspace(ch) == 0) {
177 if (ch == sep || ch ==
'\n') {
178 row[r++] = buf; rn--;
188 }
else if (quotes && ch ==
'"' && state != ST_TAILSPACE) {
189 buf[j++] =
'"'; bn--;
193 }
else if (isspace(ch)) {
194 state = ST_TAILSPACE;
198 GERB_MESSAGE(_(
"%d: bad end quote in element"), errno);
205 if(state == ST_TAILSPACE || state == ST_END_QUOTE
206 || (state == ST_COLLECT && ! inquotes)) {
207 row[r++] = buf; rn--;
219 GERB_MESSAGE(
"E2BIG %d ", errno);
225 GERB_MESSAGE(
"EILSEQ %d ", errno);
240 csv_parse_wcs(
struct winput *in,
wchar_t *buf,
size_t bn,
wchar_t *row[],
int rn, wint_t sep,
int flags)
242 int trim, quotes, state, r, j, t, inquotes;
245 trim = flags & CSV_TRIM;
246 quotes = flags & CSV_QUOTES;
251 memset(row, 0,
sizeof(
wchar_t *) * rn);
253 while (rn && bn && (ch = wnextch(in)) > 0) {
256 if (ch != L
'\n' && ch != sep && iswspace(ch)) {
262 }
else if (quotes && ch == L
'"') {
272 state = ST_END_QUOTE;
275 }
else if (ch == sep || ch == L
'\n') {
276 row[r++] = buf; rn--;
277 buf[t] = L
'\0'; bn--;
286 }
else if (quotes && ch == L
'"') {
288 GERB_MESSAGE(_(
"%d: unexpected quote in element"), errno);
292 if (!trim || iswspace(ch) == 0) {
298 if (ch == sep || ch == L
'\n') {
299 row[r++] = buf; rn--;
300 buf[j] = L
'\0'; bn--;
309 }
else if (quotes && ch == L
'"' && state != ST_TAILSPACE) {
310 buf[j++] = L
'"'; bn--;
314 }
else if (iswspace(ch)) {
315 state = ST_TAILSPACE;
319 GERB_MESSAGE(_(
"%d: bad end quote in element "), errno);
326 if(state == ST_TAILSPACE || state == ST_END_QUOTE
327 || (state == ST_COLLECT && ! inquotes)) {
328 row[r++] = buf; rn--;
329 buf[j] = L
'\0'; bn--;
340 GERB_MESSAGE(
"%d", errno);
346 GERB_MESSAGE(
"%d", errno);
358 csv_row_parse_wcs(
const wchar_t *src,
size_t sn,
wchar_t *buf,
size_t bn,
wchar_t *row[],
int rn,
int sep,
int trim)
364 return csv_parse_wcs(&input, buf, bn, row, rn, (wint_t)sep, trim);
369 csv_row_parse_str(
const char *src,
size_t sn,
char *buf,
size_t bn,
char *row[],
int rn,
int sep,
int trim)
376 return csv_parse_str(&input, buf, bn, row, rn, sep, trim);
381 csv_row_fread(FILE *in,
char *buf,
size_t bn,
char *row[],
int numcols,
int sep,
int trim)
386 return csv_parse_str(&input, buf, bn, row, numcols, sep, trim);