common.h 854 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #ifndef GUARD_COMMON_H
  2. #define GUARD_COMMON_H
  3. int __getopt_long_i__;
  4. #define getopt_long(c, v, s, l) getopt_long(c, v, s, l, &__getopt_long_i__)
  5. FILE *fopen_verbose(char *filename, char *mode) {
  6. FILE *f = fopen(filename, mode);
  7. if (!f) {
  8. fprintf(stderr, "Could not open file: \"%s\"\n", filename);
  9. }
  10. return f;
  11. }
  12. uint8_t *read_u8(char *filename, int *size) {
  13. FILE *f = fopen_verbose(filename, "rb");
  14. if (!f) {
  15. exit(1);
  16. }
  17. fseek(f, 0, SEEK_END);
  18. *size = ftell(f);
  19. rewind(f);
  20. uint8_t *data = malloc(*size);
  21. if (*size != (int)fread(data, 1, *size, f)) {
  22. fprintf(stderr, "Could not read file: \"%s\"\n", filename);
  23. exit(1);
  24. }
  25. fclose(f);
  26. return data;
  27. }
  28. void write_u8(char *filename, uint8_t *data, int size) {
  29. FILE *f = fopen_verbose(filename, "wb");
  30. if (f) {
  31. fwrite(data, 1, size, f);
  32. fclose(f);
  33. }
  34. }
  35. #endif // GUARD_COMMON_H