Rename *_verbose functions to idiomatic x*

This commit is contained in:
Rangi 2022-03-02 21:39:13 -05:00
parent 824d6bbaf2
commit e888b2f826
6 changed files with 42 additions and 33 deletions

View File

@ -27,7 +27,7 @@ void usage_exit(int status) {
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)
void *malloc_verbose(size_t size) { void *xmalloc(size_t size) {
errno = 0; errno = 0;
void *m = malloc(size); void *m = malloc(size);
if (!m) { if (!m) {
@ -36,7 +36,7 @@ void *malloc_verbose(size_t size) {
return m; return m;
} }
void *calloc_verbose(size_t size) { void *xcalloc(size_t size) {
errno = 0; errno = 0;
void *m = calloc(size, 1); void *m = calloc(size, 1);
if (!m) { if (!m) {
@ -45,7 +45,16 @@ void *calloc_verbose(size_t size) {
return m; return m;
} }
FILE *fopen_verbose(const char *filename, char rw) { void *xrealloc(void *m, size_t size) {
errno = 0;
m = realloc(m, size);
if (!m) {
error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
}
return m;
}
FILE *xfopen(const char *filename, char rw) {
char mode[3] = {rw, 'b', '\0'}; char mode[3] = {rw, 'b', '\0'};
errno = 0; errno = 0;
FILE *f = fopen(filename, mode); FILE *f = fopen(filename, mode);
@ -55,7 +64,7 @@ FILE *fopen_verbose(const char *filename, char rw) {
return f; return f;
} }
void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) { void xfread(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) {
fclose(f); fclose(f);
@ -63,7 +72,7 @@ void fread_verbose(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) { void xfwrite(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) {
fclose(f); fclose(f);
@ -71,7 +80,7 @@ void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE
} }
} }
long file_size_verbose(const char *filename, FILE *f) { long xfsize(const char *filename, FILE *f) {
long size = -1; long size = -1;
errno = 0; errno = 0;
if (!fseek(f, 0, SEEK_END)) { if (!fseek(f, 0, SEEK_END)) {
@ -87,24 +96,24 @@ long file_size_verbose(const char *filename, FILE *f) {
} }
uint8_t *read_u8(const char *filename, long *size) { uint8_t *read_u8(const char *filename, long *size) {
FILE *f = fopen_verbose(filename, 'r'); FILE *f = xfopen(filename, 'r');
*size = file_size_verbose(filename, f); *size = xfsize(filename, f);
uint8_t *data = malloc_verbose(*size); uint8_t *data = xmalloc(*size);
fread_verbose(data, *size, filename, f); xfread(data, *size, filename, f);
fclose(f); fclose(f);
return data; return data;
} }
void write_u8(const char *filename, uint8_t *data, size_t size) { void write_u8(const char *filename, uint8_t *data, size_t size) {
FILE *f = fopen_verbose(filename, 'w'); FILE *f = xfopen(filename, 'w');
fwrite_verbose(data, size, filename, f); xfwrite(data, size, filename, f);
fclose(f); fclose(f);
} }
uint32_t read_png_width_verbose(const char *filename) { uint32_t read_png_width(const char *filename) {
FILE *f = fopen_verbose(filename, 'r'); FILE *f = xfopen(filename, 'r');
uint8_t header[16] = {0}; uint8_t header[16] = {0};
fread_verbose(header, sizeof(header), filename, f); xfread(header, sizeof(header), filename, f);
static uint8_t expected_header[16] = { static uint8_t expected_header[16] = {
0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n', // signature 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n', // signature
0, 0, 0, 13, // IHDR chunk length 0, 0, 0, 13, // IHDR chunk length
@ -115,7 +124,7 @@ uint32_t read_png_width_verbose(const char *filename) {
error_exit("Not a valid PNG file: \"%s\"\n", filename); 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); xfread(bytes, sizeof(bytes), filename, f);
fclose(f); fclose(f);
return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3];
} }

View File

@ -61,7 +61,7 @@ void parse_args(int argc, char *argv[]) {
break; break;
case 'r': case 'r':
for (char *token = strtok(optarg, ","); token; token = strtok(NULL, ",")) { for (char *token = strtok(optarg, ","); token; token = strtok(NULL, ",")) {
options.preserved = realloc(options.preserved, ++options.num_preserved * sizeof(*options.preserved)); options.preserved = xrealloc(options.preserved, ++options.num_preserved * sizeof(*options.preserved));
options.preserved[options.num_preserved-1] = strtoul(token, NULL, 0); options.preserved[options.num_preserved-1] = strtoul(token, NULL, 0);
} }
break; break;
@ -212,7 +212,7 @@ const uint8_t flipped[256] = {
}; };
bool flip_exists(const uint8_t *tile, const uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) { bool flip_exists(const uint8_t *tile, const uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) {
uint8_t flip[tile_size]; uint8_t flip[tile_size]; // VLA
memset(flip, 0, tile_size); memset(flip, 0, tile_size);
int half_size = tile_size / 2; int half_size = tile_size / 2;
for (int i = 0; i < tile_size; i++) { for (int i = 0; i < tile_size; i++) {
@ -250,7 +250,7 @@ void interleave(struct Graphic *graphic, int width) {
int tile_size = options.depth * 8; int tile_size = options.depth * 8;
int width_tiles = width / 8; int width_tiles = width / 8;
int num_tiles = graphic->size / tile_size; int num_tiles = graphic->size / tile_size;
uint8_t *interleaved = malloc(graphic->size); uint8_t *interleaved = xmalloc(graphic->size);
for (int i = 0; i < num_tiles; i++) { for (int i = 0; i < num_tiles; i++) {
int row = i / width_tiles; int row = i / width_tiles;
int tile = i * 2 - (row % 2 ? width_tiles * (row + 1) - 1 : width_tiles * row); int tile = i * 2 - (row % 2 ? width_tiles * (row + 1) - 1 : width_tiles * row);
@ -280,7 +280,7 @@ int main(int argc, char *argv[]) {
if (!options.png_file) { if (!options.png_file) {
error_exit("--interleave needs --png to infer dimensions"); error_exit("--interleave needs --png to infer dimensions");
} }
int width = read_png_width_verbose(options.png_file); int width = read_png_width(options.png_file);
interleave(&graphic, width); interleave(&graphic, width);
} }
if (options.remove_duplicates) { if (options.remove_duplicates) {

View File

@ -4,7 +4,7 @@
#include "common.h" #include "common.h"
uint8_t read_png_dimensions(const char *filename) { uint8_t read_png_dimensions(const char *filename) {
uint32_t width_px = read_png_width_verbose(filename); uint32_t width_px = read_png_width(filename);
if (width_px != 40 && width_px != 48 && width_px != 56) { if (width_px != 40 && width_px != 48 && width_px != 56) {
error_exit("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);
} }

View File

@ -78,21 +78,21 @@ void make_frames(const uint8_t *tilemap, long tilemap_size, int width, struct Fr
int num_tiles_per_frame = width * width; int num_tiles_per_frame = width * width;
int num_frames = tilemap_size / num_tiles_per_frame - 1; int num_frames = tilemap_size / num_tiles_per_frame - 1;
frames->frames = malloc_verbose((sizeof *frames->frames) * num_frames); frames->frames = xmalloc((sizeof *frames->frames) * num_frames);
frames->num_frames = num_frames; frames->num_frames = num_frames;
bitmasks->bitmasks = malloc_verbose((sizeof *bitmasks->bitmasks) * num_frames); bitmasks->bitmasks = xmalloc((sizeof *bitmasks->bitmasks) * num_frames);
bitmasks->num_bitmasks = 0; bitmasks->num_bitmasks = 0;
const uint8_t *first_frame = &tilemap[0]; const uint8_t *first_frame = &tilemap[0];
const uint8_t *this_frame = &tilemap[num_tiles_per_frame]; const uint8_t *this_frame = &tilemap[num_tiles_per_frame];
for (int i = 0; i < num_frames; i++) { for (int i = 0; i < num_frames; i++) {
struct Frame *frame = malloc_verbose(sizeof *frame); struct Frame *frame = xmalloc(sizeof *frame);
frame->data = malloc_verbose(num_tiles_per_frame); frame->data = xmalloc(num_tiles_per_frame);
frame->size = 0; frame->size = 0;
struct Bitmask *bitmask = malloc_verbose(sizeof *bitmask); struct Bitmask *bitmask = xmalloc(sizeof *bitmask);
bitmask->data = calloc_verbose((num_tiles_per_frame + 7) / 8); bitmask->data = xcalloc((num_tiles_per_frame + 7) / 8);
bitmask->bitlength = 0; bitmask->bitlength = 0;
for (int j = 0; j < num_tiles_per_frame; j++) { for (int j = 0; j < num_tiles_per_frame; j++) {

View File

@ -40,7 +40,7 @@ void parse_args(int argc, char *argv[], struct Options *options) {
#define TILE_SIZE 16 #define TILE_SIZE 16
void transpose_tiles(uint8_t *tiles, int width, int size) { void transpose_tiles(uint8_t *tiles, int width, int size) {
uint8_t *new_tiles = malloc_verbose(size); uint8_t *new_tiles = xmalloc(size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
int j = i / TILE_SIZE * width * TILE_SIZE; int j = i / TILE_SIZE * width * TILE_SIZE;
j = (j / size) * TILE_SIZE + j % size + i % TILE_SIZE; j = (j / size) * TILE_SIZE + j % size + i % TILE_SIZE;
@ -91,7 +91,7 @@ void write_graphics(const char *filename, const uint8_t *tiles, long tiles_size,
// Ensure space for a duplicate of tile 0 at the end // Ensure space for a duplicate of tile 0 at the end
max_size += TILE_SIZE; max_size += TILE_SIZE;
} }
uint8_t *data = malloc_verbose(max_size); uint8_t *data = xmalloc(max_size);
int num_tiles = 0; int num_tiles = 0;
#define DATA_APPEND_TILES(tile, length) do { \ #define DATA_APPEND_TILES(tile, length) do { \
@ -119,7 +119,7 @@ void write_graphics(const char *filename, const uint8_t *tiles, long tiles_size,
void write_tilemap(const char *filename, const uint8_t *tiles, long tiles_size, int num_tiles_per_frame, bool girafarig) { void write_tilemap(const char *filename, const uint8_t *tiles, long tiles_size, int num_tiles_per_frame, bool girafarig) {
int size = tiles_size / TILE_SIZE; int size = tiles_size / TILE_SIZE;
uint8_t *data = malloc_verbose(size); uint8_t *data = xmalloc(size);
int num_tiles = num_tiles_per_frame; int num_tiles = num_tiles_per_frame;
// Copy the first frame directly // Copy the first frame directly

View File

@ -34,9 +34,9 @@ void scan_file(const char *filename, bool strict) {
} }
} }
long size = file_size_verbose(filename, f); long size = xfsize(filename, f);
char *contents = malloc_verbose(size + 1); char *contents = xmalloc(size + 1);
fread_verbose((uint8_t *)contents, size, filename, f); xfread((uint8_t *)contents, size, filename, f);
fclose(f); fclose(f);
contents[size] = '\0'; contents[size] = '\0';