From 4af0415e73e8d7af1fd0213decf48e511308d2ba Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Sat, 3 Jun 2017 12:21:50 +0200 Subject: [PATCH] Initial --- CMakeLists.txt | 4 +- src/error.c | 4 +- src/helper.c | 11 ++- src/optparse.c | 12 ++-- src/utils.c | 177 ++++++++++++++++++++++++------------------------- 5 files changed, 106 insertions(+), 102 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a9bddd6..e613397 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ project(caesiumclt) # The version number. set(VERSION_MAJOR 0) -set(VERSION_MINOR 10) -set(VERSION_PATCH 2) +set(VERSION_MINOR 11) +set(VERSION_PATCH 0) configure_file( "src/config.h.in" diff --git a/src/error.c b/src/error.c index cc1b898..e2e6b21 100644 --- a/src/error.c +++ b/src/error.c @@ -39,9 +39,9 @@ const char *get_error_message(int code) case 9: return "Input files provided. Cannot mix them with a folder."; case 10: - return "-R is useless on files."; + return "-R has no effects on files."; case 11: - return "-S is useless without -R."; + return "-S has no effect without -R."; case 12: return "Cannot set output folder inside the input one"; diff --git a/src/helper.c b/src/helper.c index 4f73aaa..26912e9 100644 --- a/src/helper.c +++ b/src/helper.c @@ -18,6 +18,7 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) struct optparse_long longopts[] = { {"quality", 'q', OPTPARSE_REQUIRED}, {"exif", 'e', OPTPARSE_NONE}, + {"progressive", 'p', OPTPARSE_NONE}, {"output", 'o', OPTPARSE_REQUIRED}, {"recursive", 'R', OPTPARSE_NONE}, {"keep-structure", 'S', OPTPARSE_NONE}, @@ -37,6 +38,8 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) break; case 'e': options->jpeg.exif_copy = true; + case 'p': + options->jpeg.progressive = false; break; case 'o': if (opts.optarg[strlen(opts.optarg) - 1] == '/' || opts.optarg[strlen(opts.optarg) - 1] == '\\') { @@ -76,7 +79,7 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) while ((arg = optparse_arg(&opts))) { if (folders_flag) { display_error(WARNING, 8); - continue; + break; } //Check if it's a directory and add its content if (is_directory(arg)) { @@ -155,9 +158,11 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) size_t index = strspn(options->input_folder, options->input_files[i]) + 1; size_t size = strlen(options->input_files[i]) - index - strlen(filename); char output_full_folder[strlen(options->output_folder) + size + 1]; - snprintf(output_full_folder, strlen(options->output_folder) + size + 1, "%s%s", options->output_folder, &options->input_files[i][index]); + snprintf(output_full_folder, strlen(options->output_folder) + size + 1, "%s%s", options->output_folder, + &options->input_files[i][index]); output_full_path = malloc((strlen(output_full_folder) + strlen(filename) + 1) * sizeof(char)); - snprintf(output_full_path, strlen(output_full_folder) + strlen(filename) + 1, "%s%s", output_full_folder, filename); + snprintf(output_full_path, strlen(output_full_folder) + strlen(filename) + 1, "%s%s", output_full_folder, + filename); mkpath(output_full_folder); } diff --git a/src/optparse.c b/src/optparse.c index 9804d99..78ecb8e 100644 --- a/src/optparse.c +++ b/src/optparse.c @@ -99,11 +99,6 @@ int optparse(struct optparse *options, const char *optstring) int type = argtype(optstring, option[0]); char *next = options->argv[options->optind + 1]; switch (type) { - case -1: { - options->optind++; - char str[2] = {option[0]}; - return opterror(options, MSG_INVALID, str); - } case OPTPARSE_NONE: if (option[1]) { options->subopt++; @@ -134,8 +129,13 @@ int optparse(struct optparse *options, const char *optstring) else options->optarg = 0; return option[0]; + default: + case -1: { + options->optind++; + char str[2] = {option[0]}; + return opterror(options, MSG_INVALID, str); + } } - return 0; } char *optparse_arg(struct optparse *options) diff --git a/src/utils.c b/src/utils.c index 9913ef0..654d51c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -19,6 +19,7 @@ void print_help() "Options:\n" "\t-q, --quality\t\tset output file quality between [0-100], 0 for optimization\n" "\t-e, --exif\t\tkeeps EXIF info during compression\n" + "\t-p, --progressive\t\toutputs a progressive JPEG\n" "\t-o, --output\t\toutput folder\n" "\t-R, --recursive\t\tif input is a folder, scan subfolders too\n" "\t-S, --keep-structure\tkeep the folder structure, use with -R\n" @@ -30,18 +31,18 @@ void print_help() bool is_directory(const char *path) { #ifdef _WIN32 - tinydir_dir dir; + tinydir_dir dir; - return tinydir_open(&dir, path) != -1; + return tinydir_open(&dir, path) != -1; #else - tinydir_file file; + tinydir_file file; - if (tinydir_file_open(&file, path) == -1) { - display_error(ERROR, 6); - } + if (tinydir_file_open(&file, path) == -1) { + display_error(ERROR, 6); + } - return (bool) file.is_dir; + return (bool) file.is_dir; #endif } @@ -62,10 +63,10 @@ int scan_folder(const char *directory, cclt_options *options, bool recursive) } else { options->input_files = realloc(options->input_files, (options->files_count + 1) * sizeof(char *)); options->input_files[options->files_count] = malloc((strlen(file.path) + 1) * sizeof(char)); - snprintf(options->input_files[options->files_count], - strlen(file.path) + 1, "%s", file.path); + snprintf(options->input_files[options->files_count], + strlen(file.path) + 1, "%s", file.path); #ifdef _WIN32 - options->input_files[options->files_count] = str_replace(options->input_files[options->files_count], "/", "\\"); + options->input_files[options->files_count] = str_replace(options->input_files[options->files_count], "/", "\\"); #endif options->files_count++; n++; @@ -107,13 +108,11 @@ char *get_filename(char *full_path) //Get just the filename tofree = strdup(full_path); - //TODO change to strncpy - strcpy(tofree, full_path); - //TODO Windows? + snprintf(tofree, strlen(full_path) + 1, "%s", full_path); #ifdef _WIN32 - while ((token = strsep(&tofree, "\\")) != NULL) { + while ((token = strsep(&tofree, "\\")) != NULL) { #else - while ((token = strsep(&tofree, "/")) != NULL) { + while ((token = strsep(&tofree, "/")) != NULL) { #endif if (tofree == NULL) { break; @@ -127,15 +126,15 @@ char *get_filename(char *full_path) off_t get_file_size(const char *path) { - FILE *f = fopen(path, "rb"); - if (f == NULL) { - display_error(ERROR, 7); - } - fseek(f, 0, SEEK_END); - unsigned long len = (unsigned long)ftell(f); - fclose(f); + FILE *f = fopen(path, "rb"); + if (f == NULL) { + display_error(ERROR, 7); + } + fseek(f, 0, SEEK_END); + off_t len = ftell(f); + fclose(f); - return len; + return len; } char *get_human_size(off_t size) @@ -160,86 +159,86 @@ char *get_human_size(off_t size) #ifdef _WIN32 char *str_replace(char *orig, char *rep, char *with) { - char *result; // the return string - char *ins; // the next insert point - char *tmp; // varies - int len_rep; // length of rep (the string to remove) - int len_with; // length of with (the string to replace rep with) - int len_front; // distance between rep and end of last rep - int count; // number of replacements + char *result; // the return string + char *ins; // the next insert point + char *tmp; // varies + int len_rep; // length of rep (the string to remove) + int len_with; // length of with (the string to replace rep with) + int len_front; // distance between rep and end of last rep + int count; // number of replacements - if (!orig || !rep) - return NULL; - len_rep = strlen(rep); - if (len_rep == 0) - return NULL; - if (!with) - with = ""; - len_with = strlen(with); + if (!orig || !rep) + return NULL; + len_rep = strlen(rep); + if (len_rep == 0) + return NULL; + if (!with) + with = ""; + len_with = strlen(with); - ins = orig; - for (count = 0; tmp = strstr(ins, rep); ++count) { - ins = tmp + len_rep; - } + ins = orig; + for (count = 0; tmp = strstr(ins, rep); ++count) { + ins = tmp + len_rep; + } - tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); + tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); - if (!result) - return NULL; + if (!result) + return NULL; - while (count--) { - ins = strstr(orig, rep); - len_front = ins - orig; - tmp = strncpy(tmp, orig, len_front) + len_front; - tmp = strcpy(tmp, with) + len_with; - orig += len_front + len_rep; - } - strcpy(tmp, orig); - return result; + while (count--) { + ins = strstr(orig, rep); + len_front = ins - orig; + tmp = strncpy(tmp, orig, len_front) + len_front; + tmp = strcpy(tmp, with) + len_with; + orig += len_front + len_rep; + } + strcpy(tmp, orig); + return result; } char *strsep (char **stringp, const char *delim) { - char *begin, *end; + char *begin, *end; - begin = *stringp; - if (begin == NULL) - return NULL; + begin = *stringp; + if (begin == NULL) + return NULL; - /* A frequent case is when the delimiter string contains only one - character. Here we don't need to call the expensive `strpbrk' - function and instead work using `strchr'. */ - if (delim[0] == '\0' || delim[1] == '\0') - { - char ch = delim[0]; + /* A frequent case is when the delimiter string contains only one + character. Here we don't need to call the expensive `strpbrk' + function and instead work using `strchr'. */ + if (delim[0] == '\0' || delim[1] == '\0') + { + char ch = delim[0]; - if (ch == '\0') - end = NULL; - else - { - if (*begin == ch) - end = begin; - else if (*begin == '\0') - end = NULL; - else - end = strchr (begin + 1, ch); - } - } - else - /* Find the end of the token. */ - end = strpbrk (begin, delim); + if (ch == '\0') + end = NULL; + else + { + if (*begin == ch) + end = begin; + else if (*begin == '\0') + end = NULL; + else + end = strchr (begin + 1, ch); + } + } + else + /* Find the end of the token. */ + end = strpbrk (begin, delim); - if (end) - { - /* Terminate the token and set *STRINGP past NUL character. */ - *end++ = '\0'; - *stringp = end; - } - else - /* No more delimiters; this is the last token. */ - *stringp = NULL; + if (end) + { + /* Terminate the token and set *STRINGP past NUL character. */ + *end++ = '\0'; + *stringp = end; + } + else + /* No more delimiters; this is the last token. */ + *stringp = NULL; - return begin; + return begin; } #endif