aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-16 12:03:42 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-16 12:03:42 +0100
commitd01b39d1bb08687077309eb00704461c228c6a3e (patch)
treec4dd83d39f8ac1a1bcdfe645bd08478273f816f9 /src
parente7616cdeb615772c07aba6aae506a969ad2546fb (diff)
downloadovm-d01b39d1bb08687077309eb00704461c228c6a3e.tar.gz
ovm-d01b39d1bb08687077309eb00704461c228c6a3e.tar.bz2
ovm-d01b39d1bb08687077309eb00704461c228c6a3e.zip
Added helper functions to read and write bytes from files
Given a filepath, helper function to write a buffer of bytes to a file and to read a file completely as a buffer.
Diffstat (limited to 'src')
-rw-r--r--src/main.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index f47d308..6682fc5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,12 +10,37 @@
* Description: Entrypoint to program
*/
+#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "./inst.h"
#include "./runtime.h"
+void write_bytes_to_file(darr_t *bytes, const char *filepath)
+{
+ FILE *fp = fopen(filepath, "wb");
+ size_t size = fwrite(bytes->data, bytes->used, 1, fp);
+ fclose(fp);
+ assert(size == 1);
+}
+
+void read_bytes_from_file(const char *filepath, darr_t *darr)
+{
+ darr->data = NULL;
+ darr->used = 0;
+ darr->available = 0;
+
+ FILE *fp = fopen(filepath, "rb");
+ fseek(fp, 0, SEEK_END);
+ long size = ftell(fp);
+ darr_init(darr, size);
+ fseek(fp, 0, SEEK_SET);
+ size_t read = fread(darr->data, size, 1, fp);
+ fclose(fp);
+ assert(read == 1);
+}
+
int main(void)
{
byte stack_data[256];