summaryrefslogtreecommitdiff
path: root/igen/misc.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-02-13 20:20:59 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2022-02-13 20:20:59 -0400
commitf478e6c1223cc8370fa51d44b9244ec25be99788 (patch)
tree4abe8889888c0b098ea99ee020c446254822a923 /igen/misc.c
parent6833f6bc2a5730169084c74d8e8fc0b76666b2a0 (diff)
igen: Start work on writing an instruction handler
generator. This will make it easier in the long run to modify instructions, add new instructions, and move the opcode tables around.
Diffstat (limited to 'igen/misc.c')
-rw-r--r--igen/misc.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/igen/misc.c b/igen/misc.c
new file mode 100644
index 0000000..ba8c755
--- /dev/null
+++ b/igen/misc.c
@@ -0,0 +1,77 @@
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "misc.h"
+
+char *read_file(const char *filename, long *size) {
+ /* Open the file. */
+ FILE *fp = fopen(filename, "r");
+ /* Size of the file, in bytes. */
+ long filesize = 0;
+ /* Buffer of the file contents. */
+ char *buf;
+
+ /* Return NULL, if we couldn't open the file. */
+ if (fp == NULL) {
+ return NULL;
+ }
+
+ /* Return NULL, if we couldn't seek to the end of the file. */
+ if (fseek(fp, 0L, SEEK_END)) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Get the size of the file, in bytes. */
+ filesize = ftell(fp);
+
+ /* Return NULL, if the returned size is negative. */
+ if (filesize < 0) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Allocate enough space for the entire file, plus one. */
+ buf = calloc(filesize+1, sizeof(char));
+
+ /* Return NULL, if the buffer wasn't allocated. */
+ if (buf == NULL) {
+ fclose(fp);
+ return NULL;
+ }
+
+ /* Seek back to the start of the file. */
+ rewind(fp);
+ /* Read the entire file contents into the buffer. */
+ fread(buf, sizeof(char), filesize, fp);
+ /* Close the file. */
+ fclose(fp);
+
+ /* Return the filesize, in bytes. */
+ *size = filesize;
+ /* Return the buffer. */
+ return buf;
+}
+
+char *get_line(char **str) {
+ char *s;
+ size_t i;
+ char *tmp = *str;
+
+ for (i = 0; tmp[i] != '\n' && tmp[i] != '\0'; i++);
+
+ s = calloc(i+1, sizeof(char));
+ memcpy(s, *str, i);
+
+ *str += (i+1);
+ return s;
+}
+
+char *make_str(const char *str) {
+ const size_t length = strlen(str);
+ char *s = calloc(length+1, sizeof(char));
+ memcpy(s, str, length+1);
+ return s;
+}