Project

General

Profile

« Previous | Next » 

Revision 328d0fa5

Added by David Sorber over 9 years ago

Adding proper option parsing and some general improvements. I've added an option for decompression but I still need to implement it.

View differences:

software/photo_compress_archiver/PhotoCompressArchiver.cc
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sys/stat.h>
......
const char *PROG_OPT3 = "-p";
PhotoCompressArchiver::PhotoCompressArchiver(
const char* path,
uint32_t num_cores)
const std::string& path,
uint32_t num_cores,
bool decompress,
bool keep_orig)
: m_path(path),
m_num_cores(num_cores),
m_decompress(decompress),
m_keep_orig(keep_orig),
m_files_processed(0),
m_num_input_files(0),
m_total_uncompressed_size(0),
......
// Start the file finder thread
OUT(std::cout << "Starting file finder..." << std::flush;);
p_finder_thread = new std::thread(&PhotoCompressArchiver::find_files, this,
m_path, JPEG_REGEX);
m_path,
(m_decompress ? PJG_REGEX : JPEG_REGEX));
OUT(std::cout << "DONE" << std::endl;)
p_finder_thread->join();
OUT(std::cout << "File finder completed:" << std::endl;)
OUT(std::cout << " Found " << m_num_input_files << " images" << std::endl;)
OUT(std::cout << " Total uncompressed bytes: " << m_total_uncompressed_size
<< "\n" << std::endl;)
if (! m_decompress)
{
OUT(std::cout << " Total uncompressed bytes: "
<< m_total_uncompressed_size << "\n" << std::endl;)
}
// Start worker threads
OUT(std::cout << "Starting worker threads..." << std::endl;)
OUT(std::cout << "Starting worker threads..." << std::flush;)
// TODO: need to handle the cause were number of input files is less than
// TODO: need to handle the case were number of input files is less than
// the number of threads
uint32_t num_threads = m_num_cores;
uint32_t sublist_len = m_num_input_files / num_threads;
......
this, idx, sublist);
m_worker_threads.push_back(worker);
}
OUT(std::cout << "All worker threads started" << std::endl;)
OUT(std::cout << "DONE\n" << std::endl;)
// Clean up the finder thread
delete p_finder_thread;
......
delete worker;
}
OUT(std::cout << "\n Total compressed bytes: " << m_total_compressed_size
<< std::endl;)
if (! m_decompress)
{
OUT(std::cout << "\n Total compressed bytes: "
<< m_total_compressed_size
<< std::endl;)
// Calculate and display ratio
double ratio = (double)m_total_compressed_size / m_total_uncompressed_size;
OUT(std::cout << " Compression ratio: " << std::fixed
<< std::setprecision(4) << (ratio * 100) << "%\n" << std::endl;)
// Calculate and display ratio
double ratio = (double)m_total_compressed_size / m_total_uncompressed_size;
OUT(std::cout << " Compression ratio: " << std::fixed
<< std::setprecision(4) << (ratio * 100) << "%\n"
<< std::endl;)
}
return 0;
}
......
int rc = stat(dir_iter->path().string().c_str(), &statbuf);
if (rc)
{
OUT(std::cerr << "ERROR: unable to stat file "
<< dir_iter->path() << std::endl;)
OUT(std::cerr << BOLD << RED << "ERROR: " << ENDC
<< "unable to stat file " << dir_iter->path()
<< std::endl;)
}
m_total_uncompressed_size += statbuf.st_size;
}
......
uint32_t sublist_file_count = file_sublist->size();
uint32_t files_processed = 0;
#if 0
// Print out the input files given to this worker
OUT(std::cout << "T[" << tid << "] top of the morning to ya: "
<< sublist_file_count << std::endl;)
// Print out the input files given to this worker
#if 0
for (auto& filepath : *file_sublist)
{
OUT(std::cout << "T[" << tid << "] file: " << filepath.string()
......
stat_rc = stat(output_filenames[output_idx - 1]->c_str(), &statbuf);
if (stat_rc)
{
OUT(std::cerr << "ERROR while stating: "
OUT(std::cerr << BOLD << RED << "ERROR " << ENDC
<< "while stating: "
<< *output_filenames[output_idx - 1] << std::endl;)
}
......
++m_files_processed;
++files_processed;
// Unless keeping original files, remove the source file
if (! m_keep_orig)
{
std::remove(file_sublist->at(output_idx - 1).string().c_str());
}
// Calculate the local percent done
complete_percent = (double)files_processed / sublist_file_count;
complete_percent *= 100;
......
stat_rc = stat(output_filenames[output_idx - 1]->c_str(), &statbuf);
if (stat_rc)
{
OUT(std::cerr << "ERROR while stating: "
OUT(std::cerr << BOLD << RED << "ERROR " << ENDC << "while stating: "
<< *output_filenames[output_idx - 1] << std::endl;)
}
......
++m_files_processed;
++files_processed;
// Unless keeping original files, remove the source file
if (! m_keep_orig)
{
std::remove(file_sublist->at(output_idx - 1).string().c_str());
}
// Calculate the local percent done
complete_percent = (double)files_processed / sublist_file_count;
complete_percent *= 100;
......
<< *output_filenames[output_idx - 1] << " -- "
<< statbuf.st_size << std::endl;)
OUT(std::cout << "T[" << tid << "] complete ==> RC: " << status << std::endl;)
OUT(std::cout << "T[" << tid << "] "<< PURPLE << "complete ==> RC: "
<< status << ENDC << std::endl;)
// Free up the argv array and output filename list
delete[] exec_argv;

Also available in: Unified diff