Merge pull request #16 from Lymphatus/libcaesium-with-cmake

Exit on error
This commit is contained in:
Matteo Paonessa 2017-03-13 02:13:24 -07:00 committed by GitHub
commit 9c00af9cd2
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.
The sequence should be something like:
$ tar xfv caesium-clt-*.tar.gz
$ tar xfv caesiumclt-*.tar.gz
$ cd caesium-clt-*
$ cmake .
$ 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
```
$ caesiumclt -q 0 -RS-o ~/output/ ~/Pictures
$ caesiumclt -q 0 -RS -o ~/output/ ~/Pictures
```
----------

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <caesium.h>
#include <stdlib.h>
#include "error.h"
@ -10,6 +11,9 @@ void display_error(error_level level, int code)
error_level,
code,
get_error_message(code));
if (level == ERROR) {
exit(-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.";
case 11:
return "-S is useless without -R.";
case 12:
return "Cannot set output folder inside the input one";
default:
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);
if (options->jpeg.quality < 0 || options->jpeg.quality > 100) {
display_error(ERROR, 1);
exit(EXIT_FAILURE);
}
break;
case 'e':
@ -64,7 +63,6 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options)
default:
fprintf(stderr, "%s: %s\n", argv[0], opts.errmsg);
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 (strstr(parameters.output_folder, parameters.input_folder) != NULL) {
display_error(ERROR, 12);
}
if (parameters.recursive && !folders_flag) {
display_error(WARNING, 10);
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
if (mkpath(options->output_folder, 0777) == -1) {
display_error(ERROR, 5);
exit(EXIT_FAILURE);
}
for (int i = 0; i < options->files_count; i++) {

View File

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

View File

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