summaryrefslogtreecommitdiff
path: root/tools/common.h
diff options
context:
space:
mode:
authorMarcus Huderle <huderlem@gmail.com>2017-07-08 11:35:34 -0700
committerGitHub <noreply@github.com>2017-07-08 11:35:34 -0700
commit057ebcb601f200c6ec009353e64021c4fbee8c9b (patch)
tree30640c28ed3d9f434f3441c96027a8eb698ac2b1 /tools/common.h
parent1add8675b3dcb87856425dd51af92cc3887f47b4 (diff)
parenta6f85dabbe5fad7f4fb7962293f52b6942a364c8 (diff)
Merge pull request #15 from yenatch/c-tools
Replace python/submodule with c tools
Diffstat (limited to 'tools/common.h')
-rw-r--r--tools/common.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/common.h b/tools/common.h
new file mode 100644
index 0000000..bc877cc
--- /dev/null
+++ b/tools/common.h
@@ -0,0 +1,37 @@
+#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);
+ fread(data, 1, *size, f);
+ 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