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 35262f5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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
43 changes: 30 additions & 13 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 @@ -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 35262f5

Please sign in to comment.