Project

General

Profile

« Previous | Next » 

Revision 537f40e6

Added by David Sorber almost 8 years ago

Continued improvements to PCA; handle errors better, make number of
processing threads configurable, and reversed the "keep-orig" option to
"delete-orig" which makes more sense.

View differences:

software/photo_compress_archiver/main.cc
void header()
{
std::cout << BOLD << "Photo Compress Archiver -- " << version << "\n" << ENDC;
std::cout << " Copyright 2018 - David Sorber\n";
std::cout << " Copyright 2017-2018 -- David Sorber\n";
std::cout << " >>> Utilizing packJPG by Matthias Stirner\n" << std::endl;
}
......
}
header();
std::cout << "USAGE: " << app_name << " [-hkd] [-f <filter regex>] <file path>\n\n";
std::cout << "USAGE: " << app_name << " [-hdk] [-t <num threads>] "
<< "[-f <filter regex>] <file path>\n\n";
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";
std::cout << " -f [ --filter ] regex with which to filter out input files\n";
std::cout << " -k [ --delete-orig ] delete original files\n";
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";
std::cout << std::endl;
}
// NOTE: useful filter: -f ^\\._.+$
int main(int argc, char** argv)
{
uint32_t num_cores = std::thread::hardware_concurrency();
uint32_t num_threads = 0;
std::string search_path("");
std::string filter_str("");
bpo::variables_map general_opts_vm;
try
......
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")
("filter,f", bpo::value<std::string>(),
"regex with which to filter out input files")
("file_path", "path in which to recursively look for input files")
("delete-orig,k", "delete orignal files")
("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")
;
// Positional arguments
......
pos_opts.add("file_path", 1);
// Parse general/positional options
bpo::store(bpo::command_line_parser(argc, argv).options(general_opts)
bpo::store(bpo::command_line_parser(argc, argv)
.options(general_opts)
.positional(pos_opts).run(),
general_opts_vm);
......
}
bpo::notify(general_opts_vm);
}
}
catch (std::exception& e)
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << e.what() << "\n";
std::cerr << BOLD << RED << "\nERROR: " << ENDC << e.what() << "\n"
<< std::endl;
return -1;
}
// 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))
{
std::cerr << BOLD << RED << "ERROR: " << ENDC << "the path \""
<< search_path << "\" does not exist" << std::endl;
std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the path \""
<< search_path << "\" does not exist\n" << std::endl;
return -2;
}
// Display header and
header();
uint32_t num_cores = std::thread::hardware_concurrency();
std::cout << "[" << num_cores << " cores detected]\n";
// 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"))
// Validate number of threads
if (num_threads > (2 * std::thread::hardware_concurrency()))
{
keep_orig = true;
std::cout << " keep original files => ON\n";
std::cerr << BOLD << RED << "\nERROR: " << ENDC << "the maximum number "
<< "of threads is: " << (2 * num_cores) << "(2 * " << num_cores
<< ")\n" << std::endl;
return -2;
}
// Check for, and if present validate the filter regex
......
{
try
{
filter_regex = general_opts_vm["filter"].as<std::string>().c_str();
filter_regex = filter_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";
<< filter_str << "\" is not a valid regular expression.\n";
std::cerr << " " << e.what() << std::endl;
return -1;
}
std::cout << " filter regex => ON (" << filter_regex << ")\n";
}
// 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;
// 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";
}
std::cout << std::endl;
// Create the PCA object and let 'er rip!
auto pca = new PhotoCompressArchiver(search_path, num_cores, decompress,
keep_orig, filter_regex);
auto pca = new PhotoCompressArchiver(search_path, num_threads, decompress,
delete_orig, filter_regex);
pca->execute();
delete pca;

Also available in: Unified diff