Exit on error

This commit is contained in:
Matteo Paonessa 2017-03-13 10:13:03 +01:00
parent 788092064e
commit c93b0bdb25
6 changed files with 21 additions and 13 deletions

View File

@ -14,7 +14,7 @@ Unpack the archive (if not cloned from GIT), enter the directory
and compile the program. and compile the program.
The sequence should be something like: The sequence should be something like:
$ tar xfv caesium-clt-*.tar.gz $ tar xfv caesiumclt-*.tar.gz
$ cd caesium-clt-* $ cd caesium-clt-*
$ cmake . $ cmake .
$ make $ make

View File

@ -48,7 +48,7 @@ $ caesiumclt -q 0 -R -o ~/output/ ~/Pictures
Losslessly compress ```Pictures``` folder and subfolders, located in the ```home``` directory, into a folder called ```output``` retaining the input folder structure Losslessly compress ```Pictures``` folder and subfolders, located in the ```home``` directory, into a folder called ```output``` retaining the input folder structure
``` ```
$ caesiumclt -q 0 -RS-o ~/output/ ~/Pictures $ caesiumclt -q 0 -RS -o ~/output/ ~/Pictures
``` ```
---------- ----------

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <caesium.h> #include <caesium.h>
#include <stdlib.h>
#include "error.h" #include "error.h"
@ -10,6 +11,9 @@ void display_error(error_level level, int code)
error_level, error_level,
code, code,
get_error_message(code)); get_error_message(code));
if (level == ERROR) {
exit(-code);
}
} }
const char *get_error_message(int code) const char *get_error_message(int code)
@ -38,6 +42,8 @@ const char *get_error_message(int code)
return "-R is useless on files."; return "-R is useless on files.";
case 11: case 11:
return "-S is useless without -R."; return "-S is useless without -R.";
case 12:
return "Cannot set output folder inside the input one";
default: default:
return "Unrecognized error."; return "Unrecognized error.";

View File

@ -33,7 +33,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options)
options->jpeg.quality = atoi(opts.optarg); options->jpeg.quality = atoi(opts.optarg);
if (options->jpeg.quality < 0 || options->jpeg.quality > 100) { if (options->jpeg.quality < 0 || options->jpeg.quality > 100) {
display_error(ERROR, 1); display_error(ERROR, 1);
exit(EXIT_FAILURE);
} }
break; break;
case 'e': case 'e':
@ -64,7 +63,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options)
default: default:
fprintf(stderr, "%s: %s\n", argv[0], opts.errmsg); fprintf(stderr, "%s: %s\n", argv[0], opts.errmsg);
display_error(ERROR, 2); display_error(ERROR, 2);
exit(EXIT_FAILURE);
} }
} }
@ -100,6 +98,10 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options)
} }
//If there're files and folders, we cannot keep the structure //If there're files and folders, we cannot keep the structure
if (strstr(parameters.output_folder, parameters.input_folder) != NULL) {
display_error(ERROR, 12);
}
if (parameters.recursive && !folders_flag) { if (parameters.recursive && !folders_flag) {
display_error(WARNING, 10); display_error(WARNING, 10);
parameters.recursive = false; parameters.recursive = false;
@ -125,7 +127,6 @@ int start_compression(cclt_options *options, cs_image_pars *parameters)
//Create the output folder if does not exists //Create the output folder if does not exists
if (mkpath(options->output_folder, 0777) == -1) { if (mkpath(options->output_folder, 0777) == -1) {
display_error(ERROR, 5); display_error(ERROR, 5);
exit(EXIT_FAILURE);
} }
for (int i = 0; i < options->files_count; i++) { for (int i = 0; i < options->files_count; i++) {

View File

@ -30,11 +30,15 @@ int main(int argc, char *argv[])
diff = clock() - start; diff = clock() - start;
compression_time = diff * 1000 / CLOCKS_PER_SEC; compression_time = diff * 1000 / CLOCKS_PER_SEC;
//Output the compression results //Output the compression results
fprintf(stdout, "-------------------------------\n" fprintf(stdout, "-------------------------------\n");
"Compression completed in " compression_time / 1000 % 60 >= 1 ?
"%lum%lus%lums\n%s -> %s [%.2f%% | %s]\n", fprintf(stdout, "Compression completed in %lum%lus\n",
compression_time / 1000 / 60, compression_time / 1000 % 60, compression_time % 1000, compression_time / 1000 / 60, compression_time / 1000 % 60) :
fprintf(stdout, "Compression completed in %lum%lus%lums\n",
compression_time / 1000 / 60, compression_time / 1000 % 60, compression_time % 1000);
fprintf(stdout, "%s -> %s [%.2f%% | %s]\n",
get_human_size(options.input_total_size), get_human_size(options.output_total_size), get_human_size(options.input_total_size), get_human_size(options.output_total_size),
((float) options.output_total_size - options.input_total_size) * 100 / options.input_total_size, ((float) options.output_total_size - options.input_total_size) * 100 / options.input_total_size,
get_human_size((options.output_total_size - options.input_total_size))); get_human_size((options.output_total_size - options.input_total_size)));

View File

@ -21,8 +21,7 @@ void print_help()
"\t-e, --exif\t\tkeeps EXIF info during compression\n" "\t-e, --exif\t\tkeeps EXIF info during compression\n"
"\t-o, --output\t\toutput folder\n" "\t-o, --output\t\toutput folder\n"
"\t-R, --recursive\t\tif input is a folder, scan subfolders too\n" "\t-R, --recursive\t\tif input is a folder, scan subfolders too\n"
//TODO Remove this warning "\t-S, --keep-structure\tkeep the folder structure, use with -R\n"
"\t-S, --keep-structure\tkeep the folder structure [Not active yet], use with -R\n"
"\t-h, --help\t\tdisplay this help and exit\n" "\t-h, --help\t\tdisplay this help and exit\n"
"\t-v, --version\t\toutput version information and exit\n\n"); "\t-v, --version\t\toutput version information and exit\n\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -34,7 +33,6 @@ bool is_directory(const char *path)
if (tinydir_file_open(&file, path) == -1) { if (tinydir_file_open(&file, path) == -1) {
display_error(ERROR, 6); display_error(ERROR, 6);
exit(EXIT_FAILURE);
} }
return (bool) file.is_dir; return (bool) file.is_dir;
@ -118,7 +116,6 @@ off_t get_file_size(const char *path)
if (tinydir_file_open(&file, path) == -1) { if (tinydir_file_open(&file, path) == -1) {
display_error(ERROR, 7); display_error(ERROR, 7);
exit(EXIT_FAILURE);
} }
return file._s.st_size; return file._s.st_size;