summaryrefslogtreecommitdiff
path: root/tools/common.h
diff options
context:
space:
mode:
authorAndrew Martinek <andrewrmartinek@gmail.com>2020-12-20 17:31:00 -0500
committerAndrew Martinek <andrewrmartinek@gmail.com>2020-12-20 17:31:00 -0500
commite8ec8efa902ea56cf17bdcd908067247d13ee28d (patch)
treeba708ba6b0e0125148de6de2c48713d0b64d70de /tools/common.h
parent5930b53dd28705197ebfc1a905ea3175552ba39a (diff)
parent9fab715759ddf919b0c4bb9a01095c4c225fcac0 (diff)
Merge in master (and update some macros)
Diffstat (limited to 'tools/common.h')
-rw-r--r--tools/common.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/tools/common.h b/tools/common.h
new file mode 100644
index 0000000..4da0b2e
--- /dev/null
+++ b/tools/common.h
@@ -0,0 +1,40 @@
+#ifndef GUARD_COMMON_H
+#define GUARD_COMMON_H
+
+int __getopt_long_i__;
+#define getopt_long(c, v, s, l) getopt_long(c, v, s, l, &__getopt_long_i__)
+
+FILE *fopen_verbose(char *filename, char *mode) {
+ FILE *f = fopen(filename, mode);
+ if (!f) {
+ fprintf(stderr, "Could not open file: \"%s\"\n", filename);
+ }
+ return f;
+}
+
+uint8_t *read_u8(char *filename, int *size) {
+ FILE *f = fopen_verbose(filename, "rb");
+ if (!f) {
+ exit(1);
+ }
+ fseek(f, 0, SEEK_END);
+ *size = ftell(f);
+ rewind(f);
+ uint8_t *data = malloc(*size);
+ if (*size != (int)fread(data, 1, *size, f)) {
+ fprintf(stderr, "Could not read file: \"%s\"\n", filename);
+ exit(1);
+ }
+ fclose(f);
+ return data;
+}
+
+void write_u8(char *filename, uint8_t *data, int size) {
+ FILE *f = fopen_verbose(filename, "wb");
+ if (f) {
+ fwrite(data, 1, size, f);
+ fclose(f);
+ }
+}
+
+#endif // GUARD_COMMON_H