diff --git a/src/Makefile.am b/src/Makefile.am index 75a6705..9121e61 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,4 @@ bin_PROGRAMS = caesiumclt -caesiumclt_SOURCES = main.c jpeg.c compresshelper.c utils.c png.c lodepng.c +caesiumclt_SOURCES = main.c jpeg.c compresshelper.c utils.c png.c lodepng.c error.c caesiumclt_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -std=c99 caesiumclt_LDADD = -lm diff --git a/src/Makefile.in b/src/Makefile.in index 8d62933..f2400a0 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -101,7 +101,7 @@ PROGRAMS = $(bin_PROGRAMS) am_caesiumclt_OBJECTS = caesiumclt-main.$(OBJEXT) \ caesiumclt-jpeg.$(OBJEXT) caesiumclt-compresshelper.$(OBJEXT) \ caesiumclt-utils.$(OBJEXT) caesiumclt-png.$(OBJEXT) \ - caesiumclt-lodepng.$(OBJEXT) + caesiumclt-lodepng.$(OBJEXT) caesiumclt-error.$(OBJEXT) caesiumclt_OBJECTS = $(am_caesiumclt_OBJECTS) caesiumclt_DEPENDENCIES = caesiumclt_LINK = $(CCLD) $(caesiumclt_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ @@ -253,7 +253,7 @@ target_alias = @target_alias@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -caesiumclt_SOURCES = main.c jpeg.c compresshelper.c utils.c png.c lodepng.c +caesiumclt_SOURCES = main.c jpeg.c compresshelper.c utils.c png.c lodepng.c error.c caesiumclt_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -std=c99 caesiumclt_LDADD = -lm all: all-am @@ -343,6 +343,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caesiumclt-compresshelper.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caesiumclt-error.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caesiumclt-jpeg.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caesiumclt-lodepng.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/caesiumclt-main.Po@am__quote@ @@ -447,6 +448,20 @@ caesiumclt-lodepng.obj: lodepng.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(caesiumclt_CFLAGS) $(CFLAGS) -c -o caesiumclt-lodepng.obj `if test -f 'lodepng.c'; then $(CYGPATH_W) 'lodepng.c'; else $(CYGPATH_W) '$(srcdir)/lodepng.c'; fi` +caesiumclt-error.o: error.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(caesiumclt_CFLAGS) $(CFLAGS) -MT caesiumclt-error.o -MD -MP -MF $(DEPDIR)/caesiumclt-error.Tpo -c -o caesiumclt-error.o `test -f 'error.c' || echo '$(srcdir)/'`error.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/caesiumclt-error.Tpo $(DEPDIR)/caesiumclt-error.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='error.c' object='caesiumclt-error.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(caesiumclt_CFLAGS) $(CFLAGS) -c -o caesiumclt-error.o `test -f 'error.c' || echo '$(srcdir)/'`error.c + +caesiumclt-error.obj: error.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(caesiumclt_CFLAGS) $(CFLAGS) -MT caesiumclt-error.obj -MD -MP -MF $(DEPDIR)/caesiumclt-error.Tpo -c -o caesiumclt-error.obj `if test -f 'error.c'; then $(CYGPATH_W) 'error.c'; else $(CYGPATH_W) '$(srcdir)/error.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/caesiumclt-error.Tpo $(DEPDIR)/caesiumclt-error.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='error.c' object='caesiumclt-error.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(caesiumclt_CFLAGS) $(CFLAGS) -c -o caesiumclt-error.obj `if test -f 'error.c'; then $(CYGPATH_W) 'error.c'; else $(CYGPATH_W) '$(srcdir)/error.c'; fi` + ID: $(am__tagged_files) $(am__define_uniq_tagged_files); mkid -fID $$unique tags: tags-am diff --git a/src/compresshelper.c b/src/compresshelper.c index cf81c38..2dcfe08 100644 --- a/src/compresshelper.c +++ b/src/compresshelper.c @@ -16,6 +16,7 @@ #include "utils.h" #include "jpeg.h" #include "png.h" +#include "error.h" void initialize_jpeg_parameters(cclt_parameters* par) { par->jpeg.quality = 0; @@ -56,31 +57,26 @@ void validate_parameters(cclt_parameters* pars) { if (!((pars->jpeg.lossless) ^ (pars->jpeg.quality > 0))) { //Both or none are set if (pars->jpeg.lossless && pars->jpeg.quality > 0) { - fprintf(stderr, "[ERROR] -l option can't be used with -q. Either use one or the other.\n"); - exit(-1); + trigger_error(1, true); } else if (!pars->jpeg.lossless && pars->jpeg.quality <= 0) { - fprintf(stderr, "[ERROR] Either -l or -q must be set.\n"); - exit(-2); + trigger_error(2, true); } } else { //One of them is set //If -q is set check it is within the 1-100 range if (!(pars->jpeg.quality >= 1 && pars->jpeg.quality <= 100) && pars->jpeg.lossless == 0) { - fprintf(stderr, "[ERROR] Quality must be within a [1-100] range.\n"); - exit(-3); + trigger_error(3, true); } } //Check if you set the input files if (pars->input_files_count == 0) { - fprintf(stderr, "[ERROR] No input files.\n"); - exit(-9); + trigger_error(9, true); } //Check if the output folder is set if (pars->output_folder == NULL) { - fprintf(stderr, "[ERROR] No -o option pointing to the destination folder. Aborting.\n"); - exit(-4); + trigger_error(4, true); } } @@ -96,22 +92,19 @@ cclt_parameters parse_arguments(int argc, char* argv[]) { case 'v': printf("%s-%d\n", APP_VERSION, BUILD); exit(0); - break; case '?': if (optopt == 'q' || optopt == 'o' || optopt == 's') { - fprintf (stderr, "[ERROR] Option -%c requires an argument.\n", optopt); - //Arguments without values - exit(-1); + trigger_error(6, true, optopt); } else if (isprint(optopt)) { - fprintf (stderr, "[ERROR] Unknown option `-%c'.\n", optopt); + trigger_error(100, false, optopt); } else { - fprintf (stderr, "[ERROR] Unknown option character `\\x%x'.\n", optopt); + trigger_error(101, false, optopt); } break; case ':': - fprintf(stderr, "[ERROR] Parameter expected.\n"); + trigger_error(102, false); break; case 'q': parameters.jpeg.quality = string_to_int(optarg); @@ -144,10 +137,9 @@ cclt_parameters parse_arguments(int argc, char* argv[]) { if (is_directory(argv[optind])) { if (i != 0) { //TODO This error appears also if there a value to the -l parameter - printf("[ERROR] Found folder along with input files. Aborting.\n"); - exit(-20); + trigger_error(20, true); } else if (i == 0 && argc - optind > 1) { - printf("[WARNING] Folder found, skipping all other inputs.\n"); + trigger_error(103, false); } scan_folder(¶meters, argv[optind], parameters.recursive); return parameters; @@ -189,7 +181,7 @@ int cclt_compress_routine(char* input, char* output, cclt_parameters* pars) { } else if (type == PNG) { cclt_png_optimize(input, output, &pars->png); } else { - printf("[WARNING] Unknown file type.\n"); + trigger_error(104, false, input); return -1; } return 0; @@ -203,8 +195,7 @@ void cclt_start(cclt_parameters* pars, off_t* i_t_size, off_t* o_t_size) { //Creates the output folder (which will always be needed) if (mkpath(pars->output_folder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) { if (errno != EEXIST) { - fprintf(stderr, "[ERROR] Failed to create output directory.\n"); - exit(-5); + trigger_error(5, true); } } @@ -228,8 +219,7 @@ void cclt_start(cclt_parameters* pars, off_t* i_t_size, off_t* o_t_size) { //Get input stats status = stat(pars->input_files[i], &st_buf); if (status != 0) { - fprintf(stderr, "[ERROR] Failed to get input file stats.\n"); - exit(-11); + trigger_error(11, true, pars->input_files[i]); } //Check if we ran into a folder diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..99f89da --- /dev/null +++ b/src/error.c @@ -0,0 +1,86 @@ +#include "error.h" + +#include +#include +#include + +#define parse_error_level(level) ((level) ? "ERROR" : "WARNING") + +void trigger_error(int code, bool is_critical, ...) { + va_list args; + va_start(args, is_critical); + + fprintf(stderr, "%s %d: ", + parse_error_level(is_critical), + code); + + switch (code) { + case 1: + fprintf(stderr, + "-l option can't be used with -q. Either use one or the other."); + break; + case 2: + fprintf(stderr, + "Either -l or -q must be set."); + break; + case 3: + fprintf(stderr, + "Quality must be within a [1-100] range."); + break; + case 4: + fprintf(stderr, + "No -o option pointing to the destination folder."); + break; + case 5: + fprintf(stderr, + "Failed to create output directory. Permission issue?"); + break; + case 6: + vfprintf(stderr, + "Option -%c requires an argument.", args); + break; + case 9: + fprintf(stderr, + "No input files."); + break; + case 11: + vfprintf(stderr, + "Failed to get input file stats: %s", args); + case 20: + fprintf(stderr, + "Found folder along with input files."); + case 100: + vfprintf(stderr, + "Unknown option `-%c'.", args); + break; + case 101: + vfprintf(stderr, + "Unknown option character `\\x%x'.", args); + break; + case 102: + fprintf(stderr, + "Parameter expected."); + break; + case 103: + fprintf(stderr, + "Folder found, skipping all other inputs."); + break; + case 104: + vfprintf(stderr, + "Unknown file type: %s", args); + default: + //Every unlisted code is critical + is_critical = true; + fprintf(stderr, + "Cs-137 spreading out. Good luck."); + break; + } + + fprintf(stderr, "\n"); + + va_end (args); + + if (is_critical) { + exit(EXIT_FAILURE); + } +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..c195b0b --- /dev/null +++ b/src/error.h @@ -0,0 +1,8 @@ +#ifndef CCLT_ERROR +#define CCLT_ERROR + +#include + +void trigger_error(int code, bool is_critical, ...); + +#endif