From 33876323aa2e3b1994a60b294cfd3f7fd0b35f68 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 15 Sep 2024 15:50:28 -0400 Subject: Update path reading APIs to use optional values instead of erroring --- stdlib/optionals.h | 14 +++++++++----- stdlib/paths.c | 19 ++++++++----------- stdlib/paths.h | 4 ++-- 3 files changed, 19 insertions(+), 18 deletions(-) (limited to 'stdlib') diff --git a/stdlib/optionals.h b/stdlib/optionals.h index e37d5345..b67badc4 100644 --- a/stdlib/optionals.h +++ b/stdlib/optionals.h @@ -9,13 +9,17 @@ #include "util.h" #define OptionalBool_t uint8_t +#define OptionalArray_t Array_t +#define OptionalTable_t Table_t +#define OptionalText_t Text_t +#define OptionalClosure_t Closure_t extern const OptionalBool_t NULL_BOOL; -extern const Table_t NULL_TABLE; -extern const Array_t NULL_ARRAY; -extern const Int_t NULL_INT; -extern const Closure_t NULL_CLOSURE; -extern const Text_t NULL_TEXT; +extern const OptionalTable_t NULL_TABLE; +extern const OptionalArray_t NULL_ARRAY; +extern const OptionalInt_t NULL_INT; +extern const OptionalClosure_t NULL_CLOSURE; +extern const OptionalText_t NULL_TEXT; PUREFUNC bool is_null(const void *obj, const TypeInfo *non_optional_type); Text_t Optional$as_text(const void *obj, bool colorize, const TypeInfo *type); diff --git a/stdlib/paths.c b/stdlib/paths.c index bebb9389..afdd5426 100644 --- a/stdlib/paths.c +++ b/stdlib/paths.c @@ -244,16 +244,16 @@ public void Path$append_bytes(Path_t path, Array_t bytes, int permissions) _write(path, bytes, O_WRONLY | O_APPEND | O_CREAT, permissions); } -public Array_t Path$read_bytes(Path_t path) +public OptionalArray_t Path$read_bytes(Path_t path) { path = Path$_expand_home(path); int fd = open(Text$as_c_string(path), O_RDONLY); if (fd == -1) - fail("Could not read file: %k (%s)", &path, strerror(errno)); + return NULL_ARRAY; struct stat sb; if (fstat(fd, &sb) != 0) - fail("Could not read file: %k (%s)", &path, strerror(errno)); + return NULL_ARRAY; if ((sb.st_mode & S_IFMT) == S_IFREG) { // Use memory mapping if it's a real file: const char *mem = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); @@ -269,7 +269,7 @@ public Array_t Path$read_bytes(Path_t path) char chunk[256]; ssize_t just_read = read(fd, chunk, sizeof(chunk)); if (just_read < 0) - fail("Failed while reading file: %k (%s)", &path, strerror(errno)); + return NULL_ARRAY; else if (just_read == 0) { if (errno == EAGAIN || errno == EINTR) continue; @@ -287,17 +287,14 @@ public Array_t Path$read_bytes(Path_t path) break; } close(fd); - - if (u8_check((uint8_t*)content, len) != NULL) - fail("File does not contain valid UTF8 data!"); - return (Array_t){.data=content, .atomic=1, .stride=1, .length=len}; } } -public Text_t Path$read(Path_t path) +public OptionalText_t Path$read(Path_t path) { Array_t bytes = Path$read_bytes(path); + if (bytes.length < 0) return NULL_TEXT; return Text$from_bytes(bytes); } @@ -480,13 +477,13 @@ static Text_t _next_line(FILE **f) return line_text; } -public Closure_t Path$by_line(Path_t path) +public OptionalClosure_t Path$by_line(Path_t path) { path = Path$_expand_home(path); FILE *f = fopen(Text$as_c_string(path), "r"); if (f == NULL) - fail("Could not read file: %k (%s)", &path, strerror(errno)); + return NULL_CLOSURE; FILE **wrapper = GC_MALLOC(sizeof(FILE*)); *wrapper = f; diff --git a/stdlib/paths.h b/stdlib/paths.h index d06d4fdc..65d98a0c 100644 --- a/stdlib/paths.h +++ b/stdlib/paths.h @@ -29,8 +29,8 @@ void Path$write(Path_t path, Text_t text, int permissions); void Path$write_bytes(Path_t path, Array_t bytes, int permissions); void Path$append(Path_t path, Text_t text, int permissions); void Path$append_bytes(Path_t path, Array_t bytes, int permissions); -Text_t Path$read(Path_t path); -Array_t Path$read_bytes(Path_t path); +OptionalText_t Path$read(Path_t path); +OptionalArray_t Path$read_bytes(Path_t path); void Path$remove(Path_t path, bool ignore_missing); void Path$create_directory(Path_t path, int permissions); Array_t Path$children(Path_t path, bool include_hidden); -- cgit v1.2.3