#include "stream.h" #include void text_reset(struct text *t) { t->length = 0; t->failed = 0; t->bytes[0] = 0; } int text_char(struct text *t, char c) { if (t->failed) { return -1; } if (t->length + 1 >= TEXT_LIMIT) { t->failed = 1; return -1; } t->bytes[t->length] = c; t->length += 1; t->bytes[t->length] = 0; return 0; } int text_word(struct text *t, const char *s) { if (s == NULL) { t->failed = 1; return -1; } while (*s != 0) { if (text_char(t, *s) != 0) { return -1; } s += 1; } return 0; } int text_unsigned(struct text *t, unsigned long long n) { char reverse[32]; size_t count = 0; do { reverse[count] = (char)('0' + n % 10); count += 1; n /= 10; } while (n != 0); while (count != 0) { count -= 1; if (text_char(t, reverse[count]) != 0) { return -1; } } return 0; } int text_signed(struct text *t, long long n) { unsigned long long magnitude; if (n < 0) { if (text_char(t, '-') != 0) { return -1; } magnitude = (unsigned long long)(-(n + 1)); magnitude += 1; } else { magnitude = (unsigned long long)n; } return text_unsigned(t, magnitude); } int text_field(struct text *t, long long n) { if (text_char(t, ' ') != 0) { return -1; } return text_signed(t, n); } int text_end(struct text *t) { return text_char(t, '\n'); } static int start_line(struct text *t, char type, int serial) { text_reset(t); if (text_char(t, type) != 0) { return -1; } return text_field(t, serial); } static int finish_line(struct text *t) { if (t->failed) { return -1; } return text_end(t); } int format_data(struct text *t, const struct job *j, int ordinal, const struct record *r) { if (start_line(t, 'D', j->serial) != 0) { return -1; } if (text_field(t, ordinal) != 0) { return -1; } if (text_field(t, r->channel) != 0) { return -1; } if (text_field(t, r->tick) != 0) { return -1; } if (text_field(t, r->value) != 0) { return -1; } return finish_line(t); } int format_audit(struct text *t, const struct job *j, int ordinal, int status) { if (start_line(t, 'A', j->serial) != 0) { return -1; } if (text_field(t, ordinal) != 0) { return -1; } if (text_field(t, status) != 0) { return -1; } return finish_line(t); } int format_index(struct text *t, const struct job *j, int ordinal, size_t offset, size_t length) { if (start_line(t, 'I', j->serial) != 0) { return -1; } if (text_field(t, ordinal) != 0) { return -1; } if (text_field(t, (long long)offset) != 0) { return -1; } if (text_field(t, (long long)length) != 0) { return -1; } return finish_line(t); } int format_barrier(struct text *t, const struct job *j, int ordinal, int tick) { if (start_line(t, 'B', j->serial) != 0) { return -1; } if (text_field(t, ordinal) != 0) { return -1; } if (text_field(t, tick) != 0) { return -1; } return finish_line(t); } int format_total(struct text *t, const struct job *j, const struct report *r) { if (start_line(t, 'T', j->serial) != 0) { return -1; } if (text_field(t, r->accepted) != 0) { return -1; } if (text_field(t, r->filtered) != 0) { return -1; } if (text_field(t, r->invalid) != 0) { return -1; } if (text_field(t, r->notes) != 0) { return -1; } if (text_field(t, r->barriers) != 0) { return -1; } if (text_field(t, r->sum) != 0) { return -1; } if (text_field(t, r->checksum) != 0) { return -1; } return finish_line(t); } int format_end(struct text *t, const struct job *j, int index, size_t bytes) { if (start_line(t, index ? 'E' : 'Z', j->serial) != 0) { return -1; } if (text_field(t, index ? (long long)bytes : j->count) != 0) { return -1; } return finish_line(t); }