Project

General

Profile

Download (4.66 KB) Statistics
| Branch: | Tag: | Revision:
b216a5b0 David Sorber
#include <iostream>
#include <string>

#include <boost/filesystem.hpp>
328d0fa5 David Sorber
#include <boost/program_options.hpp>
b216a5b0 David Sorber
#include <boost/regex.hpp>

#include "PhotoCompressArchiver.hh"

328d0fa5 David Sorber
// Alias the namespace for readability
namespace bpo = boost::program_options;

void header()
b216a5b0 David Sorber
{
328d0fa5 David Sorber
std::cout << BOLD << "Photo Compress Archiver -- " << version << "\n" << ENDC;
4f6113f8 David Sorber
std::cout << " Copyright 2018 - David Sorber\n";
cec188e4 David Sorber
std::cout << " >>> Utilizing packJPG by Matthias Stirner\n" << std::endl;
328d0fa5 David Sorber
}

void usage(char** argv)
{
// Remove leading "./" if it exits
char* app_name = argv[0];
if (argv[0][0] == '.' && argv[0][1] == '/')
{
char* app_name = &argv[0][2];
}
b216a5b0 David Sorber
328d0fa5 David Sorber
header();
e0b7f8b9 David Sorber
std::cout << "USAGE: " << app_name << " [-hkd] [-f <filter regex>] <file path>\n\n";
328d0fa5 David Sorber
std::cout << "Positional arguments:\n";
std::cout << " <file path> path in which to recursively look "
<< "for input files\n\n";
std::cout << "General options:\n";
std::cout << " -h [ --help ] display this help message\n";
std::cout << " -d [ --decompress ] find '*.pjg' files and decompress them\n";
std::cout << " -k [ --keep-orig ] keep original files; don't delete them\n";
e0b7f8b9 David Sorber
std::cout << " -f [ --filter ] regex with which to filter out input files\n";
328d0fa5 David Sorber
std::cout << std::endl;
}

57e6c22f David Sorber
// NOTE: useful filter: -f ^\\._.+$
328d0fa5 David Sorber
int main(int argc, char** argv)
{
bpo::variables_map general_opts_vm;
b216a5b0 David Sorber
328d0fa5 David Sorber
try
b216a5b0 David Sorber
{
328d0fa5 David Sorber
// General options
bpo::options_description general_opts("General options");
general_opts.add_options()
("help,h", "display this help message")
("decompress,d", "find '*.pjg' files and decompress them")
("keep-orig,k", "keep orignal files")
e0b7f8b9 David Sorber
("filter,f", bpo::value<std::string>(),
"regex with which to filter out input files")
328d0fa5 David Sorber
("file_path", "path in which to recursively look for input files")
;

// Positional arguments
bpo::positional_options_description pos_opts;
pos_opts.add("file_path", 1);
// Parse general/positional options
bpo::store(bpo::command_line_parser(argc, argv).options(general_opts)
e0b7f8b9 David Sorber
.positional(pos_opts).run(),
328d0fa5 David Sorber
general_opts_vm);
// Display help if the option is listed or if no arguments are provided
if (general_opts_vm.count("help") || (argc == 1))
{
usage(argv);
return 0;
}
e0b7f8b9 David Sorber
bpo::notify(general_opts_vm);
328d0fa5 David Sorber
}
catch (std::exception& e)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << e.what() << "\n";
b216a5b0 David Sorber
return -1;
}
328d0fa5 David Sorber
// Make a copy of the file search path for passing into the object below
std::string search_path(general_opts_vm["file_path"].as<std::string>());
// Make sure that path specified actually exists before proceeding
if (! bfs::exists(search_path))
b216a5b0 David Sorber
{
328d0fa5 David Sorber
std::cerr << BOLD << RED << "ERROR: " << ENDC << "the path \""
<< search_path << "\" does not exist" << std::endl;
b216a5b0 David Sorber
return -2;
}
328d0fa5 David Sorber
// Display header and
header();
uint32_t num_cores = std::thread::hardware_concurrency();
cec188e4 David Sorber
std::cout << "[" << num_cores << " cores detected]\n";
b216a5b0 David Sorber
328d0fa5 David Sorber
// Decode/store decompress option
bool decompress = false;
if (general_opts_vm.count("decompress"))
{
decompress = true;
std::cout << " decompress mode => ON\n";
}

// Decode/store keep-orig option
bool keep_orig = false;
if (general_opts_vm.count("keep-orig"))
{
keep_orig = true;
std::cout << " keep original files => ON\n";
}
e0b7f8b9 David Sorber
// Check for, and if present validate the filter regex
boost::regex filter_regex("");
if (general_opts_vm.count("filter"))
{
try
{
filter_regex = general_opts_vm["filter"].as<std::string>().c_str();
}
catch (std::exception& e)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "the filter \""
<< general_opts_vm["filter"].as<std::string>()
<< "\" is not a valid regular expression.\n";
std::cerr << " " << e.what() << std::endl;
return -1;
}
57e6c22f David Sorber
std::cout << " filter regex => ON (" << filter_regex << ")\n";
e0b7f8b9 David Sorber
}
328d0fa5 David Sorber
std::cout << std::endl;
e0b7f8b9 David Sorber
// Create the PCA object and let 'er rip!
328d0fa5 David Sorber
auto pca = new PhotoCompressArchiver(search_path, num_cores, decompress,
e0b7f8b9 David Sorber
keep_orig, filter_regex);
328d0fa5 David Sorber
pca->execute();
delete pca;
b216a5b0 David Sorber
return 0;
}