#include "stream.h" #include #include struct token { unsigned long long magnitude; int negative; int digits; int overflow; }; static int separator(int c) { switch (c) { case ' ': case '\t': case '\r': case '\n': return 1; default: return 0; } } static void token_reset(struct token *t) { t->magnitude = 0; t->negative = 0; t->digits = 0; t->overflow = 0; } static void token_digit(struct token *t, int c) { unsigned digit = (unsigned)(c - '0'); unsigned long long limit = (unsigned long long)INT_MAX; if (t->negative) { limit += 1; } t->digits = 1; if (t->overflow) { return; } if (t->magnitude > limit / 10) { t->overflow = 1; return; } if (t->magnitude == limit / 10 && digit > limit % 10) { t->overflow = 1; return; } t->magnitude = t->magnitude * 10 + digit; } static int token_value(const struct token *t, int *value) { if (!t->digits || t->overflow) { return -1; } if (!t->negative) { *value = (int)t->magnitude; return 0; } unsigned long long boundary = (unsigned long long)INT_MAX + 1; if (t->magnitude == boundary) { *value = INT_MIN; } else { *value = -(int)t->magnitude; } return 0; } static int next_token(FILE *input, int *value) { int c; do { c = fgetc(input); if (c == EOF) { return ferror(input) ? -1 : 0; } } while (separator(c)); struct token token; token_reset(&token); if (c == '-' || c == '+') { token.negative = c == '-'; c = fgetc(input); } int malformed = 0; while (c != EOF && !separator(c)) { if (c < '0' || c > '9') { malformed = 1; } else { token_digit(&token, c); } c = fgetc(input); } if (ferror(input)) { return -1; } if (malformed || token_value(&token, value) != 0) { return -1; } return 1; } static int header_value(FILE *input, int *value) { int status = next_token(input, value); if (status != 1) { return -1; } return 0; } static int read_header(FILE *input, struct job *j, int failures[4]) { int first = next_token(input, &j->serial); if (first != 1) { return first; } if (header_value(input, &j->count) != 0) { return -1; } if (header_value(input, &j->channels) != 0) { return -1; } if (header_value(input, &j->lower) != 0) { return -1; } if (header_value(input, &j->upper) != 0) { return -1; } if (header_value(input, &j->flags) != 0) { return -1; } for (int kind = 0; kind < 4; ++kind) { if (header_value(input, &failures[kind]) != 0) { return -1; } if (failures[kind] < 0) { return -1; } } if (j->count < 0 || j->count > ROW_LIMIT) { return -1; } return 1; } static int read_record(FILE *input, struct record *record) { if (header_value(input, &record->kind) != 0) { return -1; } if (header_value(input, &record->channel) != 0) { return -1; } if (header_value(input, &record->value) != 0) { return -1; } if (header_value(input, &record->tick) != 0) { return -1; } return 0; } int read_job(FILE *input, struct job *j, int failures[4]) { if (input == NULL || j == NULL || failures == NULL) { return -1; } memset(j, 0, sizeof(*j)); for (int kind = 0; kind < 4; ++kind) { failures[kind] = 0; } int status = read_header(input, j, failures); if (status != 1) { return status; } for (int ordinal = 0; ordinal < j->count; ++ordinal) { if (read_record(input, &j->rows[ordinal]) != 0) { return -1; } } return 1; }