Skip to content

Commit

Permalink
feat(cli): prefix error messages with "error: "
Browse files Browse the repository at this point in the history
  • Loading branch information
dbohdan committed Sep 29, 2023
1 parent 710a429 commit c05766a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
59 changes: 43 additions & 16 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define HICOLOR_IMPLEMENTATION
#include "hicolor.h"

#define HICOLOR_CLI_ERROR "error: "
#define HICOLOR_CLI_NO_MEMORY_EXIT_CODE 255

bool check_and_report_error(char* step, hicolor_result res)
Expand All @@ -20,7 +21,7 @@ bool check_and_report_error(char* step, hicolor_result res)

fprintf(
stderr,
"%s: %s\n",
HICOLOR_CLI_ERROR "%s: %s\n",
step,
hicolor_error_message(res)
);
Expand Down Expand Up @@ -76,15 +77,19 @@ bool png_to_hicolor(
hicolor_result res;

if (access(src, F_OK) != 0) {
fprintf(stderr, "source image \"%s\" doesn't exist\n", src);
fprintf(
stderr,
HICOLOR_CLI_ERROR "source image \"%s\" doesn't exist\n",
src
);
return false;
}

cp_image_t png_img = cp_load_png(src);
if (png_img.pix == 0) {
fprintf(
stderr,
"can't load PNG file \"%s\": %s\n",
HICOLOR_CLI_ERROR "can't load PNG file \"%s\": %s\n",
src,
cp_error_reason
);
Expand All @@ -93,7 +98,11 @@ bool png_to_hicolor(

FILE* hi_file = fopen(dest, "wb");
if (hi_file == NULL) {
fprintf(stderr, "can't open destination \"%s\" for writing\n", dest);
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't open destination \"%s\" for writing\n",
dest
);
return false;
}

Expand All @@ -110,7 +119,10 @@ bool png_to_hicolor(

hicolor_rgb* rgb_img = cp_to_rgb(png_img);
if (rgb_img == NULL) {
fprintf(stderr, "can't allocate memory for RGB image\n");
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't allocate memory for RGB image\n"
);
goto clean_up_file;
}

Expand Down Expand Up @@ -148,15 +160,19 @@ bool png_quantize(
hicolor_result res;

if (access(src, F_OK) != 0) {
fprintf(stderr, "source image \"%s\" doesn't exist\n", src);
fprintf(
stderr,
HICOLOR_CLI_ERROR "source image \"%s\" doesn't exist\n",
src
);
return false;
}

cp_image_t png_img = cp_load_png(src);
if (png_img.pix == 0) {
fprintf(
stderr,
"can't load PNG file \"%s\": %s\n",
HICOLOR_CLI_ERROR "can't load PNG file \"%s\": %s\n",
src,
cp_error_reason
);
Expand All @@ -171,7 +187,10 @@ bool png_quantize(

hicolor_rgb* rgb_img = cp_to_rgb(png_img);
if (rgb_img == NULL) {
fprintf(stderr, "can't allocate memory for RGB image\n");
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't allocate memory for RGB image\n"
);
return false;
}

Expand All @@ -187,7 +206,7 @@ bool png_quantize(
quant_png_img.pix[i].a = png_img.pix[i].a;
}
if (!cp_save_png(dest, &quant_png_img)) {
fprintf(stderr, "can't save PNG\n");
fprintf(stderr, HICOLOR_CLI_ERROR "can't save PNG\n");
goto clean_up_quant_image;
}

Expand All @@ -214,7 +233,11 @@ bool hicolor_to_png(

FILE* hi_file = fopen(src, "rb");
if (hi_file == NULL) {
fprintf(stderr, "can't open source image \"%s\" for reading\n", src);
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't open source image \"%s\" for reading\n",
src
);
return false;
}

Expand All @@ -237,7 +260,7 @@ bool hicolor_to_png(

cp_image_t png_img = rgb_to_cp(meta, rgb_img);
if (!cp_save_png(dest, &png_img)) {
fprintf(stderr, "can't save PNG\n");
fprintf(stderr, HICOLOR_CLI_ERROR "can't save PNG\n");
goto clean_up_png_image;
}

Expand All @@ -263,7 +286,11 @@ bool hicolor_print_info(

FILE* hi_file = fopen(src, "rb");
if (hi_file == NULL) {
fprintf(stderr, "can't open source image \"%s\" for reading\n", src);
fprintf(
stderr,
HICOLOR_CLI_ERROR "can't open source image \"%s\" for reading\n",
src
);
return false;
}

Expand Down Expand Up @@ -383,7 +410,7 @@ int main(int argc, char** argv)
}

if (argc < 3) {
fprintf(stderr, "too few arguments\n");
fprintf(stderr, HICOLOR_CLI_ERROR "too few arguments\n");
usage(stderr);
return 1;
}
Expand All @@ -397,7 +424,7 @@ int main(int argc, char** argv)
} else if (str_prefix("quantize", argv[i])) {
opt_command = QUANTIZE;
} else {
fprintf(stderr, "invalid command\n");
fprintf(stderr, HICOLOR_CLI_ERROR "invalid command\n");
usage(stderr);
return 1;
}
Expand All @@ -422,7 +449,7 @@ int main(int argc, char** argv)
}

if (i >= argc) {
fprintf(stderr, "too few arguments\n");
fprintf(stderr, HICOLOR_CLI_ERROR "too few arguments\n");
usage(stderr);
return 1;
}
Expand All @@ -444,7 +471,7 @@ int main(int argc, char** argv)
i++;

if (i < argc) {
fprintf(stderr, "too many arguments\n");
fprintf(stderr, HICOLOR_CLI_ERROR "too many arguments\n");
usage(stderr);
return 1;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/hicolor.test
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ tcltest::test encode-2.7 {encode flags} -body {

tcltest::test encode-3.1 {bad input} -body {
hicolor encode truncated.png
} -returnCodes error -result {can't load PNG file "truncated.png": corrupt\
zlib structure in DEFLATE stream}
} -returnCodes error -result {error: can't load PNG file "truncated.png":\
corrupt zlib structure in DEFLATE stream}

tcltest::test encode-3.2 {bad input} -body {
hicolor encode wrong-size.png
} -returnCodes error -match glob -result {can't load PNG file*}
} -returnCodes error -match glob -result {error: can't load PNG file*}


hicolor encode --15-bit photo.png photo.hi5
Expand Down Expand Up @@ -173,12 +173,12 @@ tcltest::test quantize-2.1 {bad input} -body {

tcltest::test quantize-2.2 {bad input} -body {
hicolor quantize truncated.png
} -returnCodes error -result {can't load PNG file "truncated.png": corrupt\
zlib structure in DEFLATE stream}
} -returnCodes error -result {error: can't load PNG file "truncated.png":\
corrupt zlib structure in DEFLATE stream}

tcltest::test quantize-2.3 {bad input} -body {
hicolor quantize wrong-size.png
} -returnCodes error -match glob -result {can't load PNG file*}
} -returnCodes error -match glob -result {error: can't load PNG file*}


tcltest::test data-integrity-1.1 {roundtrip} -constraints gm -body {
Expand Down

0 comments on commit c05766a

Please sign in to comment.