From d368a49f56d2ad0717a97113307214006a6f838c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 14 Apr 2024 17:10:23 +0630 Subject: Implemented a function to read a file in full Uses std::optional in case file doesn't exist. --- asm/main.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/asm/main.cpp b/asm/main.cpp index 17023e7..c1b23b7 100644 --- a/asm/main.cpp +++ b/asm/main.cpp @@ -10,13 +10,36 @@ * Description: Entrypoint for assembly program */ +#include #include #include +#include +#include #include #include "./lexer.hpp" +using std::pair, std::string, std::string_view, std::vector; + +std::optional read_file(const char *filename) +{ + FILE *fp = fopen(filename, "rb"); + if (fp) + { + string contents; + fseek(fp, 0, SEEK_END); + contents.resize(ftell(fp)); + rewind(fp); + fread(&contents[0], 1, contents.size(), fp); + fclose(fp); + + return contents; + } + else + return std::nullopt; +} + void usage(const char *program_name, FILE *fp) { fprintf(fp, -- cgit v1.2.3-13-gbd6f