aboutsummaryrefslogtreecommitdiff
path: root/asm/main.cpp
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:10:23 +0630
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-04-14 17:10:23 +0630
commitd368a49f56d2ad0717a97113307214006a6f838c (patch)
tree69971246f6aa244d2a0953ee58de04977c602a32 /asm/main.cpp
parent98d4f73134ac90ab12d023affa829ebd2937e592 (diff)
downloadovm-d368a49f56d2ad0717a97113307214006a6f838c.tar.gz
ovm-d368a49f56d2ad0717a97113307214006a6f838c.tar.bz2
ovm-d368a49f56d2ad0717a97113307214006a6f838c.zip
Implemented a function to read a file in full
Uses std::optional in case file doesn't exist.
Diffstat (limited to 'asm/main.cpp')
-rw-r--r--asm/main.cpp23
1 files changed, 23 insertions, 0 deletions
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 <algorithm>
#include <cstdio>
#include <iostream>
+#include <optional>
+#include <string>
#include <lib/inst.h>
#include "./lexer.hpp"
+using std::pair, std::string, std::string_view, std::vector;
+
+std::optional<string> 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,