Project

General

Profile

Download (6.21 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;
537f40e6 David Sorber
std::cout << " Copyright 2017-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();
537f40e6 David Sorber
std::cout << "USAGE: " << app_name << " [-hdk] [-t <num threads>] "
<< "[-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";
537f40e6 David Sorber
std::cout << " -k [ --delete-orig ] delete original files\n";
ebc2733a David Sorber
std::cout << " -v [ --verbose ] verbose output while processing\n";
537f40e6 David Sorber
std::cout << " -t [ --num-threads ] number of processing threads to use (max 2x system cores)\n";
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)
{
537f40e6 David Sorber
uint32_t num_cores = std::thread::hardware_concurrency();
uint32_t num_threads = 0;
std::string search_path("");
std::string filter_str("");
328d0fa5 David Sorber
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")
537f40e6 David Sorber
328d0fa5 David Sorber
("decompress,d", "find '*.pjg' files and decompress them")
537f40e6 David Sorber
("delete-orig,k", "delete orignal files")
ebc2733a David Sorber
("verbose,v", "verbose output while processing")
537f40e6 David Sorber
("num-threads,t",
bpo::value<uint32_t>(&num_threads)->default_value(num_cores),
"number of processing threads to use")
("filter,f",
bpo::value<std::string>(&filter_str),
"regex with which to filter out input files")
// Note this has to be here even though it's a positional argument
("file_path", bpo::value<std::string>(&search_path)->required(),
"path in which to recursively look for input files")
328d0fa5 David Sorber
;

// Positional arguments
bpo::positional_options_description pos_opts;
pos_opts.add("file_path", 1);
// Parse general/positional options
537f40e6 David Sorber
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);
537f40e6 David Sorber
}
328d0fa5 David Sorber
catch (std::exception& e)
{
537f40e6 David Sorber
std::cerr << BOLD << RED << "\nERROR: " << ENDC << e.what() << "\n"
<< std::endl;
b216a5b0 David Sorber
return -1;
}
328d0fa5 David Sorber
// Make sure that path specified actually exists before proceeding
if (! bfs::exists(search_path))
b216a5b0 David Sorber
{
537f40e6 David Sorber
std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the path \""
<< search_path << "\" does not exist\n" << std::endl;
b216a5b0 David Sorber
return -2;
}
537f40e6 David Sorber
// Validate number of threads
if (num_threads > (2 * std::thread::hardware_concurrency()))
328d0fa5 David Sorber
{
537f40e6 David Sorber
std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the maximum number "
<< "of threads is: " << (2 * num_cores) << "(2 * " << num_cores
<< ")\n" << std::endl;
return -2;
328d0fa5 David Sorber
}
e0b7f8b9 David Sorber
// Check for, and if present validate the filter regex
boost::regex filter_regex("");
if (general_opts_vm.count("filter"))
{
try
{
537f40e6 David Sorber
filter_regex = filter_str;
e0b7f8b9 David Sorber
}
catch (std::exception& e)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "the filter \""
537f40e6 David Sorber
<< filter_str << "\" is not a valid regular expression.\n";
e0b7f8b9 David Sorber
std::cerr << " " << e.what() << std::endl;
return -1;
}
}
537f40e6 David Sorber
// Display header and options
header();
std::cout << " [" << num_cores << " cores detected]\n" << std::endl;
std::cout << BOLD << ">>> Options:" << ENDC << std::endl;
// Decode/store decompress option
bool decompress = false;
if (general_opts_vm.count("decompress"))
{
decompress = true;
}
std::cout << " decompress mode => " << (decompress ? "ON" : "OFF")
<< std::endl;
// Decode/store delete-orig option
bool delete_orig = false;
if (general_opts_vm.count("delete-orig"))
{
delete_orig = true;
}
std::cout << " delete original files => " << (delete_orig ? "ON" : "OFF")
<< std::endl;
ebc2733a David Sorber
// Verbose output option
bool verbose = false;
if (general_opts_vm.count("verbose"))
{
verbose = true;
}
std::cout << " verbose => " << (verbose ? "ON" : "OFF")
<< std::endl;
537f40e6 David Sorber
// Number of processing threads option
std::cout << " num processing threads => " << num_threads << std::endl;
// Filter regex option
if (general_opts_vm.count("filter"))
{
std::cout << " filter regex => ON (" << filter_str << ")\n";
}
328d0fa5 David Sorber
std::cout << std::endl;
e0b7f8b9 David Sorber
// Create the PCA object and let 'er rip!
537f40e6 David Sorber
auto pca = new PhotoCompressArchiver(search_path, num_threads, decompress,
ebc2733a David Sorber
delete_orig, verbose, filter_regex);
328d0fa5 David Sorber
pca->execute();
delete pca;
b216a5b0 David Sorber
return 0;
}