Skip to content

Commit

Permalink
feat(cli): make missing src error msgs consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
dbohdan committed Sep 29, 2023
1 parent ebf854b commit ef33700
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Create 15/16-bit color RGB images.
usage:
hicolor (encode|quantize) [-5|-6] [-n] [--] <src> [<dest>]
hicolor (decode <src> [<dest>]|info <file>|version|help|-h|--help)
hicolor (decode <src> [<dest>]|info <src>|version|help|-h|--help)
commands:
encode convert PNG to HiColor
decode convert HiColor to PNG
quantize quantize PNG to PNG
info print file version and resolution
info print HiColor image version and resolution
version print program version
help print this help message
Expand Down
45 changes: 31 additions & 14 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ cp_image_t rgb_to_cp(const hicolor_metadata meta, const hicolor_rgb* rgb_img)
return img;
}

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

return true;
}

bool png_to_hicolor(
hicolor_version version,
bool dither,
Expand All @@ -76,12 +91,8 @@ bool png_to_hicolor(
{
hicolor_result res;

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

Expand Down Expand Up @@ -159,12 +170,8 @@ bool png_quantize(
{
hicolor_result res;

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

Expand Down Expand Up @@ -231,6 +238,11 @@ bool hicolor_to_png(
{
hicolor_result res;

bool exists = check_src_exists(src);
if (!exists) {
return false;
}

FILE* hi_file = fopen(src, "rb");
if (hi_file == NULL) {
fprintf(
Expand Down Expand Up @@ -284,6 +296,11 @@ bool hicolor_print_info(
{
hicolor_result res;

bool exists = check_src_exists(src);
if (!exists) {
return false;
}

FILE* hi_file = fopen(src, "rb");
if (hi_file == NULL) {
fprintf(
Expand Down Expand Up @@ -328,7 +345,7 @@ void usage(FILE* output)
output,
"usage:\n"
" hicolor (encode|quantize) [-5|-6] [-n] [--] <src> [<dest>]\n"
" hicolor (decode <src> [<dest>]|info <file>|version|help|-h|--help)\n"
" hicolor (decode <src> [<dest>]|info <src>|version|help|-h|--help)\n"
);
}

Expand Down Expand Up @@ -357,7 +374,7 @@ void help()
" encode convert PNG to HiColor\n"
" decode convert HiColor to PNG\n"
" quantize quantize PNG to PNG\n"
" info print file version and resolution\n"
" info print HiColor image version and resolution\n"
" version print program version\n"
" help print this help message\n"
"\noptions:\n"
Expand Down

0 comments on commit ef33700

Please sign in to comment.