diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-08-17 13:18:15 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-08-18 03:52:44 +0900 |
| commit | 2a7e1667f70c5381d3b939324cc647e51134b15c (patch) | |
| tree | e6d6d5bc285aadbae20c99dbcf0a653f78dda4c3 /io.c | |
| parent | d90a9c83a253b71e7731a44657f998a361a41b97 (diff) | |
| download | ducc-2a7e1667f70c5381d3b939324cc647e51134b15c.tar.gz ducc-2a7e1667f70c5381d3b939324cc647e51134b15c.tar.zst ducc-2a7e1667f70c5381d3b939324cc647e51134b15c.zip | |
refactor: direct array access with infile_*() helper functions
Diffstat (limited to 'io.c')
| -rw-r--r-- | io.c | 46 |
1 files changed, 42 insertions, 4 deletions
@@ -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; + } +} |
