Working demo

This commit is contained in:
Matteo Paonessa 2016-12-18 20:15:40 +01:00
parent bc7ab356db
commit 0300036b4c
5 changed files with 118 additions and 54 deletions

View File

@ -94,19 +94,55 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options)
if (is_directory(arg)) {
//NOTE Scanning a folder with this function does not check if we are actually getting images
//The actual check is performed by the library
int count = 0;
count = scan_folder(arg, &result, result.recursive);
if (count == 0) {
//TODO Trigger a warning
}
} else {
result.input_files = realloc(result.input_files, (result.files_count + 1) * sizeof(char*));
result.input_files[result.files_count] = malloc((strlen(arg) + 1) * sizeof(char));
//TODO Replace with strdup for alloc
strncpy(result.input_files[result.files_count], arg, strlen(opts.optarg) + 1);
result.files_count++;
}
//If there're files and folders, we cannot keep the structure
//TODO Trigger a warning
result.keep_structure = !(files_flag && folders_flag);
}
//If there're files and folders, we cannot keep the structure
//TODO Trigger a warning
result.keep_structure = !(files_flag && folders_flag);
return result;
}
int start_compression(cclt_options *options, cs_image_pars *parameters)
{
//TODO Support folder structure
int status = 0;
//Create the output folder if does not exists
if (mkpath(options->output_folder, 0777) == -1) {
//TODO Error
exit(EXIT_FAILURE);
}
for (int i = 0; i < options->files_count; i++) {
//TODO remove unnecessary "/"s
char *filename = get_filename(options->input_files[i]);
char *output_full_path = malloc((strlen(filename) + strlen(options->output_folder) + 2) * sizeof(char));
strncpy(output_full_path, options->output_folder, strlen(options->output_folder));
strcat(output_full_path, "/");
strcat(output_full_path, filename);
fprintf(stdout,
"Compressing %s in %s\n", filename, output_full_path);
cs_compress(options->input_files[i], output_full_path, parameters);
}
return status;
}

View File

@ -20,4 +20,6 @@ cs_image_pars initialize_parameters();
cclt_options parse_arguments(char *argv[], cs_image_pars *options);
int start_compression(cclt_options *options, cs_image_pars *parameters);
#endif //CAESIUM_CLT_HELPER_H

View File

@ -10,6 +10,7 @@
int main(int argc, char* argv[]) {
errno = 0;
long execution_ms = 0;
int compression_status = 0;
cs_image_pars compress_options;
cclt_options options;
@ -20,14 +21,7 @@ int main(int argc, char* argv[]) {
clock_t start = clock(), diff;
//TODO Compress here
//start(input_files, output_folder, &options);
printf("%s\n", options.output_folder);
for (int i = 0; i < options.files_count; i++) {
printf("%s\n", options.input_files[i]);
}
scan_folder("/Users/lymphatus/Documents/Progetti/C/caesium-clt/samples");
compression_status = start_compression(&options, &compress_options);
//Cleanup the two memory allocated objects
free(options.output_folder);

View File

@ -26,47 +26,25 @@ void print_help()
exit(EXIT_SUCCESS);
}
int is_directory(const char* path)
int is_directory(const char *path)
{
struct stat sb;
return ((stat(path, &sb) && S_ISDIR(sb.st_mode)));
}
int scan_folder(const char *directory)
{
//TODO Not recursive at all
int n = 0;
tinydir_dir dir;
tinydir_open(&dir, directory);
tinydir_file file;
bool is_dir = false;
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
tinydir_open(&dir, path);
if (file.is_dir) {
if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0) {
printf("%s/", file.name);
char real[PATH_MAX];
realpath(file.name, real);
scan_folder(real);
}
} else {
printf("%s\n", file.name);
}
tinydir_next(&dir);
n++;
}
tinydir_readfile(&dir, &file);
is_dir = (bool) file.is_dir;
tinydir_close(&dir);
return 0;
return is_dir;
}
/*
* int n = 0;
int scan_folder(const char *directory, cclt_options *options, bool recursive)
{
int n = 0;
tinydir_dir dir;
tinydir_open(&dir, directory);
@ -74,18 +52,65 @@ int scan_folder(const char *directory)
tinydir_file file;
tinydir_readfile(&dir, &file);
printf("%s", file.name);
if (file.is_dir && (file.name != "." && file.name != "..")) {
scan_folder(file.name);
printf("/");
if (file.is_dir) {
if (strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0 && recursive) {
scan_folder(file.path, options, true);
}
} else {
tinydir_next(&dir);
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));
strncpy(options->input_files[options->files_count], file.path, strlen(file.path) + 1);
options->files_count++;
n++;
}
printf("\n");
tinydir_next(&dir);
}
tinydir_close(&dir);
return 0;
*/
return n;
}
//TODO Recheck
int mkpath(const char *pathname, mode_t mode)
{
char parent[PATH_MAX], *p;
/* make a parent directory path */
strncpy(parent, pathname, sizeof(parent));
parent[sizeof(parent) - 1] = '\0';
for (p = parent + strlen(parent); *p != '/' && p != parent; p--);
*p = '\0';
/* try make parent directory */
if (p != parent && mkpath(parent, mode) != 0) {
return -1;
}
/* make this one if parent has been made */
if (mkdir(pathname, mode) == 0) {
return 0;
}
/* if it already exists that is fine */
if (errno == EEXIST) {
return 0;
}
return -1;
}
char *get_filename(char *full_path)
{
char *token, *tofree;
//Get just the filename
tofree = strdup(full_path);
//TODO change to strncpy
strcpy(tofree, full_path);
//TODO Change on Windows
while ((token = strsep(&tofree, "/")) != NULL) {
if (tofree == NULL) {
break;
}
}
free(tofree);
return token;
}

View File

@ -5,14 +5,21 @@
#ifndef CAESIUM_CLT_UTILS_H
#define CAESIUM_CLT_UTILS_H
#include "helper.h"
#define APP_VERSION_STRING "0.10.0"
#define APP_VERSION_NUMBER 0100
#define BUILD 20161215
void print_help();
int is_directory(const char* path);
int is_directory(const char *path);
int scan_folder(const char *directory, cclt_options *options, bool recursive);
int mkpath(const char *pathname, mode_t mode);
char *get_filename(char * full_path);
int scan_folder(const char* directory);
#endif //CAESIUM_CLT_UTILS_H