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 ==
'"') {
151 state = ST_END_QUOTE;
154 }
else if (ch == sep || ch ==
'\n') {
155 row[r++] = buf; rn--;
166 }
else if (quotes && ch ==
'"') {
168 GERB_MESSAGE(_(
"%d: unexpected quote in element"),errno);
172 if (!trim || isspace(ch) == 0) {
178 if (ch == sep || ch ==
'\n') {
179 row[r++] = buf; rn--;
189 }
else if (quotes && ch ==
'"' && state != ST_TAILSPACE) {
190 buf[j++] =
'"'; bn--;
194 }
else if (isspace(ch)) {
195 state = ST_TAILSPACE;
199 GERB_MESSAGE(_(
"%d: bad end quote in element"), errno);
206 if(state == ST_TAILSPACE || state == ST_END_QUOTE
207 || (state == ST_COLLECT && ! inquotes)) {
208 row[r++] = buf; rn--;
220 GERB_MESSAGE(
"E2BIG %d ", errno);
226 GERB_MESSAGE(
"EILSEQ %d ", errno);
241 csv_parse_wcs(
struct winput *in,
wchar_t *buf,
size_t bn,
wchar_t *row[],
int rn, wint_t sep,
int flags)
243 int trim, quotes, state, r, j, t, inquotes;
246 trim = flags & CSV_TRIM;
247 quotes = flags & CSV_QUOTES;
252 memset(row, 0,
sizeof(
wchar_t *) * rn);
254 while (rn && bn && (ch = wnextch(in)) > 0) {
257 if (ch != L
'\n' && ch != sep && iswspace(ch)) {
263 }
else if (quotes && ch == L
'"') {
274 state = ST_END_QUOTE;
277 }
else if (ch == sep || ch == L
'\n') {
278 row[r++] = buf; rn--;
279 buf[t] = L
'\0'; bn--;
288 }
else if (quotes && ch == L
'"') {
290 GERB_MESSAGE(_(
"%d: unexpected quote in element"), errno);
294 if (!trim || iswspace(ch) == 0) {
300 if (ch == sep || ch == L
'\n') {
301 row[r++] = buf; rn--;
302 buf[j] = L
'\0'; bn--;
311 }
else if (quotes && ch == L
'"' && state != ST_TAILSPACE) {
312 buf[j++] = L
'"'; bn--;
316 }
else if (iswspace(ch)) {
317 state = ST_TAILSPACE;
321 GERB_MESSAGE(_(
"%d: bad end quote in element "), errno);
328 if(state == ST_TAILSPACE || state == ST_END_QUOTE
329 || (state == ST_COLLECT && ! inquotes)) {
330 row[r++] = buf; rn--;
331 buf[j] = L
'\0'; bn--;
342 GERB_MESSAGE(
"%d", errno);
348 GERB_MESSAGE(
"%d", errno);
360 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)
366 return csv_parse_wcs(&input, buf, bn, row, rn, (wint_t)sep, trim);
371 csv_row_parse_str(
const char *src,
size_t sn,
char *buf,
size_t bn,
char *row[],
int rn,
int sep,
int trim)
378 return csv_parse_str(&input, buf, bn, row, rn, sep, trim);
383 csv_row_fread(FILE *in,
char *buf,
size_t bn,
char *row[],
int numcols,
int sep,
int trim)
388 return csv_parse_str(&input, buf, bn, row, numcols, sep, trim);
Header info for the parsing support functions for the pick and place parser.
Sets up internal definitions for handling csv-style files.
The main header file for the libgerbv library.