diff options
author | yenatch <yenatch@gmail.com> | 2017-06-24 20:42:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-24 20:42:25 -0400 |
commit | 5dfe27125b589a6a38c5d43fddd3724386e9803b (patch) | |
tree | 5d21f6dff1ddc8cf44deca26b52dd897719ceb22 /tools/common.h | |
parent | fc300ab0ee63a1ecbcead67c3a7019a0bfe8deb5 (diff) | |
parent | dbaec2053429e04f21613c3c0964e213008bfdb9 (diff) |
Merge branch 'master' into tools-makefile-integration
Diffstat (limited to 'tools/common.h')
-rw-r--r-- | tools/common.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/common.h b/tools/common.h new file mode 100644 index 000000000..bc877ccb9 --- /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 |