(xuflds): Allow multiple spaces to separate fields or symbols in symbol set.

This commit is contained in:
Ron Koenderink 2005-11-30 17:16:02 +00:00
parent e9ddf6a80b
commit ea1e3b6090

View file

@ -108,7 +108,7 @@ xuesc(char *buf)
static int static int
xuflds(FILE *fp, struct value values[]) xuflds(FILE *fp, struct value values[])
{ {
int i, ch; int i, j, ch;
char sep; char sep;
char buf[1024]; char buf[1024];
@ -125,35 +125,88 @@ xuflds(FILE *fp, struct value values[])
values[i].v_type = VAL_DOUBLE; values[i].v_type = VAL_DOUBLE;
break; break;
case '"': case '"':
if (fscanf(fp, "\"%1023[^ \n]%c", buf, &sep) != 2 ch = getc(fp);
|| buf[strlen(buf)-1] != '"') j = 0;
buf[j] = '\0';
do {
if (j >= 1023)
return gripe("Malformed string in field %d", i + 1); return gripe("Malformed string in field %d", i + 1);
buf[strlen(buf)-1] = '\0'; ch = getc(fp);
switch (ch) {
case '"':
values[i].v_type = VAL_STRING;
if (!j)
values[i].v_field.v_string = NULL;
else {
buf[j] = '\0';
if (!xuesc(buf)) if (!xuesc(buf))
return gripe("Invalid escape sequence in field %d", return gripe("Invalid escape sequence in field %d",
i + 1); i + 1);
values[i].v_type = VAL_STRING;
values[i].v_field.v_string = strdup(buf); values[i].v_field.v_string = strdup(buf);
}
sep = getc(fp);
if (sep == EOF)
return gripe("Unexpected end of the row");
break;
case EOF:
case '\n':
return gripe("Malformed string in field %d", i + 1);
case ' ':
if (!j)
break;
/*
* fall through
*/
default:
buf[j++] = ch;
break;
}
} while (ch != '"');
break; break;
case '(': case '(':
ch = getc(fp); ch = getc(fp);
j = 0;
buf[j] = '\0';
do {
if (j >= 1023)
return gripe("Malformed string in field %d", i + 1);
ch = getc(fp); ch = getc(fp);
if (ch == EOF) switch (ch) {
return gripe("Unexpected end of file while reading a symbol set for field %d", i + 1); case ')':
if (ch == ')') { if (!j) {
values[i].v_field.v_double = 0.0;
values[i].v_type = VAL_DOUBLE; values[i].v_type = VAL_DOUBLE;
sep = getc(fp); values[i].v_field.v_double = 0.0;
if (sep == EOF) } else {
return gripe("Unexpected end of file while reading a symbol set for field %d", i + 1); buf[j] = '\0';
break; if (!xuesc(buf))
} return gripe("Invalid escape sequence in field %d",
ungetc(ch, fp); i + 1);
ungetc('(', fp);
if (fscanf(fp, "(%1023[^)\n])%c", buf, &sep) != 2)
return gripe("Malformed symbol set in field %d", i + 1);
values[i].v_type = VAL_SYMBOL_SET; values[i].v_type = VAL_SYMBOL_SET;
values[i].v_field.v_string = strdup(buf); values[i].v_field.v_string = strdup(buf);
}
sep = getc(fp);
if (sep == EOF)
return gripe("Unexpected end of the row");
break;
case EOF:
case '\n':
return gripe("Malformed string in field %d", i + 1);
case ' ':
if (!j)
break;
else if (buf[j-1] == ' ')
break;
/*
* fall through
*/
default:
buf[j++] = ch;
break;
}
} while (ch != ')');
break;
case ' ':
ch = getc(fp);
break; break;
default: default:
if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) { if (fscanf(fp, "%1023[^ \n]%c", buf, &sep) != 2) {