Refactor error reporting to simplify BSD-style err (#949)

This commit is contained in:
Rangi
2021-11-21 16:16:54 -05:00
committed by GitHub
parent 54293a9184
commit bdcef6f252
24 changed files with 262 additions and 297 deletions

View File

@@ -20,7 +20,7 @@ void transpose_tiles(struct GBImage *gb, int width)
newdata = calloc(gb->size, 1);
if (!newdata)
err(1, "%s: Failed to allocate memory for new data", __func__);
err("%s: Failed to allocate memory for new data", __func__);
for (i = 0; i < gb->size; i++) {
newbyte = i / (8 * depth) * width * 8 * depth;
@@ -65,7 +65,7 @@ void output_file(const struct Options *opts, const struct GBImage *gb)
f = fopen(opts->outfile, "wb");
if (!f)
err(1, "%s: Opening output file '%s' failed", __func__,
err("%s: Opening output file '%s' failed", __func__,
opts->outfile);
fwrite(gb->data, 1, gb->size - gb->trim * 8 * depth, f);
@@ -141,7 +141,7 @@ int get_mirrored_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles,
tile_yflip = malloc(tile_size);
if (!tile_yflip)
err(1, "%s: Failed to allocate memory for Y flip of tile",
err("%s: Failed to allocate memory for Y flip of tile",
__func__);
yflip(tile, tile_yflip, tile_size);
index = get_tile_index(tile_yflip, tiles, num_tiles, tile_size);
@@ -153,7 +153,7 @@ int get_mirrored_tile_index(uint8_t *tile, uint8_t **tiles, int num_tiles,
tile_xflip = malloc(tile_size);
if (!tile_xflip)
err(1, "%s: Failed to allocate memory for X flip of tile",
err("%s: Failed to allocate memory for X flip of tile",
__func__);
xflip(tile, tile_xflip, tile_size);
index = get_tile_index(tile_xflip, tiles, num_tiles, tile_size);
@@ -198,13 +198,13 @@ void create_mapfiles(const struct Options *opts, struct GBImage *gb,
tiles = calloc(max_tiles, sizeof(*tiles));
if (!tiles)
err(1, "%s: Failed to allocate memory for tiles", __func__);
err("%s: Failed to allocate memory for tiles", __func__);
num_tiles = 0;
if (*opts->tilemapfile) {
tilemap->data = calloc(max_tiles, sizeof(*tilemap->data));
if (!tilemap->data)
err(1, "%s: Failed to allocate memory for tilemap data",
err("%s: Failed to allocate memory for tilemap data",
__func__);
tilemap->size = 0;
}
@@ -212,7 +212,7 @@ void create_mapfiles(const struct Options *opts, struct GBImage *gb,
if (*opts->attrmapfile) {
attrmap->data = calloc(max_tiles, sizeof(*attrmap->data));
if (!attrmap->data)
err(1, "%s: Failed to allocate memory for attrmap data",
err("%s: Failed to allocate memory for attrmap data",
__func__);
attrmap->size = 0;
}
@@ -222,7 +222,7 @@ void create_mapfiles(const struct Options *opts, struct GBImage *gb,
flags = 0;
tile = malloc(tile_size);
if (!tile)
err(1, "%s: Failed to allocate memory for tile",
err("%s: Failed to allocate memory for tile",
__func__);
/*
* If the input image doesn't fill the last tile,
@@ -269,7 +269,7 @@ void create_mapfiles(const struct Options *opts, struct GBImage *gb,
free(gb->data);
gb->data = malloc(tile_size * num_tiles);
if (!gb->data)
err(1, "%s: Failed to allocate memory for tile data",
err("%s: Failed to allocate memory for tile data",
__func__);
for (i = 0; i < num_tiles; i++) {
tile = tiles[i];
@@ -292,7 +292,7 @@ void output_tilemap_file(const struct Options *opts,
f = fopen(opts->tilemapfile, "wb");
if (!f)
err(1, "%s: Opening tilemap file '%s' failed", __func__,
err("%s: Opening tilemap file '%s' failed", __func__,
opts->tilemapfile);
fwrite(tilemap->data, 1, tilemap->size, f);
@@ -309,7 +309,7 @@ void output_attrmap_file(const struct Options *opts,
f = fopen(opts->attrmapfile, "wb");
if (!f)
err(1, "%s: Opening attrmap file '%s' failed", __func__,
err("%s: Opening attrmap file '%s' failed", __func__,
opts->attrmapfile);
fwrite(attrmap->data, 1, attrmap->size, f);
@@ -352,7 +352,7 @@ void output_palette_file(const struct Options *opts,
f = fopen(opts->palfile, "wb");
if (!f)
err(1, "%s: Opening palette file '%s' failed", __func__,
err("%s: Opening palette file '%s' failed", __func__,
opts->palfile);
for (i = 0; i < raw_image->num_colors; i++) {

View File

@@ -167,7 +167,7 @@ int main(int argc, char *argv[])
opts.infile = argv[argc - 1];
if (depth != 1 && depth != 2)
errx(1, "Depth option must be either 1 or 2.");
errx("Depth option must be either 1 or 2.");
colors = 1 << depth;
@@ -200,17 +200,17 @@ int main(int argc, char *argv[])
opts.trim = png_options.trim;
if (raw_image->width % 8) {
errx(1, "Input PNG file %s not sized correctly. The image's width must be a multiple of 8.",
errx("Input PNG file %s not sized correctly. The image's width must be a multiple of 8.",
opts.infile);
}
if (raw_image->width / 8 > 1 && raw_image->height % 8) {
errx(1, "Input PNG file %s not sized correctly. If the image is more than 1 tile wide, its height must be a multiple of 8.",
errx("Input PNG file %s not sized correctly. If the image is more than 1 tile wide, its height must be a multiple of 8.",
opts.infile);
}
if (opts.trim &&
opts.trim > (raw_image->width / 8) * (raw_image->height / 8) - 1) {
errx(1, "Trim (%d) for input raw_image file '%s' too large (max: %u)",
errx("Trim (%d) for input raw_image file '%s' too large (max: %u)",
opts.trim, opts.infile,
(raw_image->width / 8) * (raw_image->height / 8) - 1);
}

View File

@@ -32,7 +32,7 @@ struct RawIndexedImage *input_png_file(const struct Options *opts,
f = fopen(opts->infile, "rb");
if (!f)
err(1, "Opening input png file '%s' failed", opts->infile);
err("Opening input png file '%s' failed", opts->infile);
initialize_png(&img, f);
@@ -54,7 +54,7 @@ struct RawIndexedImage *input_png_file(const struct Options *opts,
raw_image = truecolor_png_to_raw(&img); break;
default:
/* Shouldn't happen, but might as well handle just in case. */
errx(1, "Input PNG file is of invalid color type.");
errx("Input PNG file is of invalid color type.");
}
get_text(&img, png_options);
@@ -83,7 +83,7 @@ void output_png_file(const struct Options *opts,
if (opts->debug) {
outfile = malloc(strlen(opts->infile) + 5);
if (!outfile)
err(1, "%s: Failed to allocate memory for outfile",
err("%s: Failed to allocate memory for outfile",
__func__);
strcpy(outfile, opts->infile);
strcat(outfile, ".out");
@@ -93,16 +93,16 @@ void output_png_file(const struct Options *opts,
f = fopen(outfile, "wb");
if (!f)
err(1, "Opening output png file '%s' failed", outfile);
err("Opening output png file '%s' failed", outfile);
img.png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!img.png)
errx(1, "Creating png structure failed");
errx("Creating png structure failed");
img.info = png_create_info_struct(img.png);
if (!img.info)
errx(1, "Creating png info structure failed");
errx("Creating png info structure failed");
if (setjmp(png_jmpbuf(img.png)))
exit(1);
@@ -115,7 +115,7 @@ void output_png_file(const struct Options *opts,
png_palette = malloc(sizeof(*png_palette) * raw_image->num_colors);
if (!png_palette)
err(1, "%s: Failed to allocate memory for PNG palette",
err("%s: Failed to allocate memory for PNG palette",
__func__);
for (i = 0; i < raw_image->num_colors; i++) {
png_palette[i].red = raw_image->palette[i].red;
@@ -159,11 +159,11 @@ static void initialize_png(struct PNGImage *img, FILE *f)
img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!img->png)
errx(1, "Creating png structure failed");
errx("Creating png structure failed");
img->info = png_create_info_struct(img->png);
if (!img->info)
errx(1, "Creating png info structure failed");
errx("Creating png info structure failed");
if (setjmp(png_jmpbuf(img->png)))
exit(1);
@@ -215,13 +215,13 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
original_palette = palette;
palette = malloc(sizeof(*palette) * colors_in_PLTE);
if (!palette)
err(1, "%s: Failed to allocate memory for palette",
err("%s: Failed to allocate memory for palette",
__func__);
colors_in_new_palette = 0;
old_to_new_palette = malloc(sizeof(*old_to_new_palette)
* colors_in_PLTE);
if (!old_to_new_palette)
err(1, "%s: Failed to allocate memory for new palette",
err("%s: Failed to allocate memory for new palette",
__func__);
for (i = 0; i < num_trans; i++) {
@@ -243,7 +243,7 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
sizeof(*palette) *
colors_in_new_palette);
if (!palette)
err(1, "%s: Failed to allocate memory for palette",
err("%s: Failed to allocate memory for palette",
__func__);
}
@@ -372,7 +372,7 @@ static void rgba_build_palette(struct PNGImage *img,
*/
*palette_ptr_ptr = calloc(colors, sizeof(**palette_ptr_ptr));
if (!*palette_ptr_ptr)
err(1, "%s: Failed to allocate memory for palette", __func__);
err("%s: Failed to allocate memory for palette", __func__);
palette = *palette_ptr_ptr;
*num_colors = 0;
@@ -429,7 +429,7 @@ static void update_built_palette(png_color *palette,
}
if (!color_exists) {
if (*num_colors == colors) {
errx(1, "Too many colors in input PNG file to fit into a %d-bit palette (max %d).",
errx("Too many colors in input PNG file to fit into a %d-bit palette (max %d).",
depth, colors);
}
palette[*num_colors] = *pixel_color;
@@ -445,9 +445,9 @@ static int fit_grayscale_palette(png_color *palette, int *num_colors)
int i, shade_index;
if (!fitted_palette)
err(1, "%s: Failed to allocate memory for palette", __func__);
err("%s: Failed to allocate memory for palette", __func__);
if (!set_indices)
err(1, "%s: Failed to allocate memory for indices", __func__);
err("%s: Failed to allocate memory for indices", __func__);
fitted_palette[0].red = 0xFF;
fitted_palette[0].green = 0xFF;
@@ -508,7 +508,7 @@ static void order_color_palette(png_color *palette, int num_colors)
malloc(sizeof(*palette_with_luminance) * num_colors);
if (!palette_with_luminance)
err(1, "%s: Failed to allocate memory for palette", __func__);
err("%s: Failed to allocate memory for palette", __func__);
for (i = 0; i < num_colors; i++) {
/*
@@ -600,7 +600,7 @@ static uint8_t palette_index_of(png_color const *palette,
return i;
}
}
errx(1, "The input PNG file contains colors that don't appear in its embedded palette.");
errx("The input PNG file contains colors that don't appear in its embedded palette.");
}
static void read_png(struct PNGImage *img)
@@ -611,12 +611,12 @@ static void read_png(struct PNGImage *img)
img->data = malloc(sizeof(*img->data) * img->height);
if (!img->data)
err(1, "%s: Failed to allocate memory for image data",
err("%s: Failed to allocate memory for image data",
__func__);
for (y = 0; y < img->height; y++) {
img->data[y] = malloc(png_get_rowbytes(img->png, img->info));
if (!img->data[y])
err(1, "%s: Failed to allocate memory for image data",
err("%s: Failed to allocate memory for image data",
__func__);
}
@@ -632,7 +632,7 @@ static struct RawIndexedImage *create_raw_image(int width, int height,
raw_image = malloc(sizeof(*raw_image));
if (!raw_image)
err(1, "%s: Failed to allocate memory for raw image",
err("%s: Failed to allocate memory for raw image",
__func__);
raw_image->width = width;
@@ -641,18 +641,18 @@ static struct RawIndexedImage *create_raw_image(int width, int height,
raw_image->palette = malloc(sizeof(*raw_image->palette) * num_colors);
if (!raw_image->palette)
err(1, "%s: Failed to allocate memory for raw image palette",
err("%s: Failed to allocate memory for raw image palette",
__func__);
raw_image->data = malloc(sizeof(*raw_image->data) * height);
if (!raw_image->data)
err(1, "%s: Failed to allocate memory for raw image data",
err("%s: Failed to allocate memory for raw image data",
__func__);
for (y = 0; y < height; y++) {
raw_image->data[y] = malloc(sizeof(*raw_image->data[y])
* width);
if (!raw_image->data[y])
err(1, "%s: Failed to allocate memory for raw image data",
err("%s: Failed to allocate memory for raw image data",
__func__);
}
@@ -665,7 +665,7 @@ static void set_raw_image_palette(struct RawIndexedImage *raw_image,
int i;
if (num_colors > raw_image->num_colors) {
errx(1, "Too many colors in input PNG file's palette to fit into a %d-bit palette (%d in input palette, max %d).",
errx("Too many colors in input PNG file's palette to fit into a %d-bit palette (%d in input palette, max %d).",
raw_image->num_colors >> 1,
num_colors, raw_image->num_colors);
}
@@ -740,7 +740,7 @@ static void set_text(const struct PNGImage *img,
text = malloc(sizeof(*text));
if (!text)
err(1, "%s: Failed to allocate memory for PNG text",
err("%s: Failed to allocate memory for PNG text",
__func__);
if (png_options->horizontal) {