mirror of
https://gitlab.com/xCrystal/pokecrystal-board.git
synced 2024-11-16 11:27:33 -08:00
Use an error_exit
macro for tools
This commit is contained in:
parent
0d1a029e81
commit
c8f06f45d5
@ -11,6 +11,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
|
#define error_exit(...) exit((fprintf(stderr, __VA_ARGS__), 1))
|
||||||
|
|
||||||
int getopt_long_index;
|
int getopt_long_index;
|
||||||
#define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index)
|
#define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index)
|
||||||
|
|
||||||
@ -18,8 +20,7 @@ void *malloc_verbose(size_t size) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
void *m = malloc(size);
|
void *m = malloc(size);
|
||||||
if (!m) {
|
if (!m) {
|
||||||
fprintf(stderr, "Could not allocate %zu bytes: %s\n", size, strerror(errno));
|
error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
@ -29,8 +30,7 @@ FILE *fopen_verbose(const char *filename, char rw) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
FILE *f = fopen(filename, mode);
|
FILE *f = fopen(filename, mode);
|
||||||
if (!f) {
|
if (!f) {
|
||||||
fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
|
error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
@ -38,18 +38,16 @@ FILE *fopen_verbose(const char *filename, char rw) {
|
|||||||
void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) {
|
void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (fread(data, 1, size, f) != size) {
|
if (fread(data, 1, size, f) != size) {
|
||||||
fprintf(stderr, "Could not read from file \"%s\": %s\n", filename, strerror(errno));
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
exit(1);
|
error_exit("Could not read from file \"%s\": %s\n", filename, strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) {
|
void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if (fwrite(data, 1, size, f) != size) {
|
if (fwrite(data, 1, size, f) != size) {
|
||||||
fprintf(stderr, "Could not write to file \"%s\": %s\n", filename, strerror(errno));
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
exit(1);
|
error_exit("Could not write to file \"%s\": %s\n", filename, strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +61,7 @@ long file_size_verbose(const char *filename, FILE *f) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (size == -1) {
|
if (size == -1) {
|
||||||
fprintf(stderr, "Could not measure file \"%s\": %s\n", filename, strerror(errno));
|
error_exit("Could not measure file \"%s\": %s\n", filename, strerror(errno));
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -94,9 +91,8 @@ uint32_t read_png_width_verbose(const char *filename) {
|
|||||||
'I', 'H', 'D', 'R', // IHDR chunk type
|
'I', 'H', 'D', 'R', // IHDR chunk type
|
||||||
};
|
};
|
||||||
if (memcmp(header, expected_header, sizeof(header))) {
|
if (memcmp(header, expected_header, sizeof(header))) {
|
||||||
fprintf(stderr, "Not a valid PNG file: \"%s\"\n", filename);
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
exit(1);
|
error_exit("Not a valid PNG file: \"%s\"\n", filename);
|
||||||
}
|
}
|
||||||
uint8_t bytes[4] = {0};
|
uint8_t bytes[4] = {0};
|
||||||
fread_verbose(bytes, sizeof(bytes), filename, f);
|
fread_verbose(bytes, sizeof(bytes), filename, f);
|
||||||
|
@ -7,8 +7,7 @@ void usage() {
|
|||||||
uint8_t read_dimensions(const char *filename) {
|
uint8_t read_dimensions(const char *filename) {
|
||||||
uint32_t width_px = read_png_width_verbose(filename);
|
uint32_t width_px = read_png_width_verbose(filename);
|
||||||
if (width_px != 40 && width_px != 48 && width_px != 56) {
|
if (width_px != 40 && width_px != 48 && width_px != 56) {
|
||||||
fprintf(stderr, "Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px);
|
error_exit("Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px);
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
uint8_t width_tiles = (uint8_t)(width_px / 8);
|
uint8_t width_tiles = (uint8_t)(width_px / 8);
|
||||||
return (width_tiles << 4) | width_tiles;
|
return (width_tiles << 4) | width_tiles;
|
||||||
|
@ -9,8 +9,7 @@ void scan_file(const char *filename, bool strict) {
|
|||||||
FILE *f = fopen(filename, "rb");
|
FILE *f = fopen(filename, "rb");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (strict) {
|
if (strict) {
|
||||||
fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
|
error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
|
||||||
exit(1);
|
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user