/* See filefmt.h */ /* Last edited on 2005-01-16 15:05:15 by stolfi */ /* Copyright © 2005 Jorge Stolfi, UNICAMP. See note at end of file. */ #include #include #include #include #include void filefmt_write_header(FILE *wr, char *type, char *version) { fprintf(wr, "begin %s (format of %s)\n", type, version); } void filefmt_write_footer(FILE *wr, char *type) { fprintf(wr, "end %s\n", type); } char *filefmt_make_header(char *type, char *version) { char *h1 = txtcat("begin ", type); char *h2 = txtcat(" (format of ", version); char *h3 = txtcat(h1, h2); char *h = txtcat(h2, ")\n"); free(h1); free(h2); free(h3); return h; } char *filefmt_make_footer(char *type) { char *h1 = txtcat("end ", type); char *h = txtcat(h1, "\n"); free(h1); return h; } void filefmt_read_header(FILE *rd, char *type, char *version) { fget_skip_formatting_chars(rd); fget_match(rd, "begin"); fget_skip_spaces(rd); fget_match(rd, type); fget_skip_spaces(rd); fget_match(rd, "(format of"); fget_skip_spaces(rd); fget_match(rd, version); fget_skip_spaces(rd); fget_match(rd, ")"); fget_skip_spaces(rd); fget_match(rd, "\n"); } void filefmt_read_footer(FILE *rd, char *type) { fget_skip_formatting_chars(rd); fget_match(rd, "end "); fget_skip_spaces(rd); fget_match(rd, type); fget_skip_spaces(rd); fget_match(rd, "\n"); } void filefmt_write_comment(FILE *wr, char *comment, char prefix) { char *ptr = comment; while(*ptr != '\000') { /* Write a new line, advance {ptr} to start of next one or to '\000' */ char c = (*ptr); fputc(prefix, wr); fputc(' ', wr); fputc(c, wr); while (c != '\n') { ptr++; c = (*ptr); if (c == '\000') { c = '\n'; } fputc(c, wr); } if (*ptr == '\n') { ptr++; } } } char *filefmt_read_comment(FILE *rd, char prefix) { char_vec_t cmt = char_vec_new(200); int ncmt = 0; /* Number of characters read */ while (TRUE) { int c; c = fgetc(rd); if (c == EOF) { break; } if (c != prefix) { ungetc(c, rd); break; } c = fgetc(rd); affirm(c != EOF, "missing final newline in comment"); do { c = fgetc(rd); affirm(c != EOF, "missing final newline in comment"); char_vec_expand(&cmt, ncmt); cmt.el[ncmt] = c; ncmt ++; } while (c != '\n'); } char_vec_expand(&cmt, ncmt); cmt.el[ncmt] = '\000'; ncmt ++; char_vec_trim(&cmt, ncmt); return cmt.el; } /* COPYRIGHT AND AUTHORSHIP NOTICE Copyright © 2005 Jorge Stolfi, Universidade Estadual de Campinas (UNICAMP). Created by Jorge Stolfi in 1992--2005. This source file can be freely distributed, used, and modified, provided that this copyright and authorship notice is preserved in all copies, and that any modified versions of this file are clearly marked as such. This software has NO WARRANTY of correctness or applicability for any purpose. Neither the author nor his employers shall be held responsible for any losses or damages that may result from its use. END OF NOTICE */