From 2a7e1667f70c5381d3b939324cc647e51134b15c Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 17 Aug 2025 13:18:15 +0900 Subject: refactor: direct array access with infile_*() helper functions --- io.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index ae02a0d..c6e94d1 100644 --- a/io.c +++ b/io.c @@ -1,10 +1,17 @@ -struct InFile { +struct SourceLocation { const char* filename; - char* buf; + int line; +}; +typedef struct SourceLocation SourceLocation; + +struct InFile { + const char* buf; + int pos; + SourceLocation loc; }; typedef struct InFile InFile; -InFile* read_all(const char* filename) { +InFile* infile_open(const char* filename) { FILE* in; if (strcmp(filename, "-") == 0) { in = stdin; @@ -38,7 +45,38 @@ InFile* read_all(const char* filename) { fclose(in); InFile* in_file = calloc(1, sizeof(InFile)); - in_file->filename = filename; in_file->buf = buf; + in_file->loc.filename = filename; + in_file->loc.line = 1; return in_file; } + +BOOL infile_eof(InFile* f) { + return f->buf[f->pos] == '\0'; +} + +char infile_peek_char(InFile* f) { + char c = f->buf[f->pos]; + // Normalize new-line. + // TODO: crlf + if (c == '\r') + c = '\n'; + return c; +} + +char infile_next_char(InFile* f) { + char c = infile_peek_char(f); + ++f->pos; + if (c == '\n') + ++f->loc.line; + return c; +} + +BOOL infile_consume_if(InFile* f, char expected) { + if (infile_peek_char(f) == expected) { + infile_next_char(f); + return TRUE; + } else { + return FALSE; + } +} -- cgit v1.2.3-70-g09d2