root/software/photo_compress_archiver/PhotoCompressArchiver.hh @ 1a887ea3
| b216a5b0 | David Sorber | #ifndef PHOTOCOMPRESSARCHIVER_H
|
||
#define PHOTOCOMPRESSARCHIVER_H
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
| 3eeb1485 | David Sorber | #include <deque>
|
||
| b216a5b0 | David Sorber | #include <mutex>
|
||
| ddd2cb4a | David Sorber | #include <sstream>
|
||
| b216a5b0 | David Sorber | #include <string>
|
||
#include <thread>
|
||||
#include <vector>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
| 4f6113f8 | David Sorber | #include "ProtectedAndSynchronizedQueue.h"
|
||
| b216a5b0 | David Sorber | // Alias the boost fs namespace to keep the code readable
|
||
namespace bfs = boost::filesystem;
|
||||
| 328d0fa5 | David Sorber | const std::string BOLD("\033[1m");
|
||
const std::string ENDC("\033[0m");
|
||||
const std::string RED("\033[31m");
|
||||
const std::string YELLOW("\033[33m");
|
||||
const std::string PURPLE("\033[35m");
|
||||
| ebc2733a | David Sorber | const std::string UP_ONE = "\033[1A";
|
||
const std::string DOWN_ONE = "\033[1B";
|
||||
| 328d0fa5 | David Sorber | |||
| 13f1ac5a | David Sorber | const std::string version("v0.4.1");
|
||
| 328d0fa5 | David Sorber | |||
| b216a5b0 | David Sorber | const boost::regex JPEG_REGEX("^.+\\.(jpg)|(jpeg)$", boost::regex::icase);
|
||
| 328d0fa5 | David Sorber | const boost::regex PJG_REGEX("^.+\\.pjg$", boost::regex::icase);
|
||
| b216a5b0 | David Sorber | |||
| 4f6113f8 | David Sorber | const std::string JPG_EXTENSION(".jpg");
|
||
const std::string PJG_EXTENSION(".pjg");
|
||||
| ebc2733a | David Sorber | const uint32_t PROGRESS_BAR_WIDTH = 80;
|
||
| 13f1ac5a | David Sorber | const std::string LINE(100, '-');
|
||
| ebc2733a | David Sorber | |||
| 4f6113f8 | David Sorber | class WorkUnit
|
||
{
|
||||
public:
|
||||
bfs::path m_path;
|
||||
uint32_t m_file_size;
|
||||
| a62448d3 | David Sorber | |||
explicit WorkUnit(const bfs::path& path)
|
||||
| 4f6113f8 | David Sorber | : m_path(path),
|
||
m_file_size(0)
|
||||
{
|
||||
| 537f40e6 | David Sorber | // Automagically determine the file size
|
||
| 4f6113f8 | David Sorber | m_file_size = bfs::file_size(path);
|
||
};
|
||||
| 3eeb1485 | David Sorber | };
|
||
| b216a5b0 | David Sorber | class PhotoCompressArchiver
|
||
{
|
||||
public:
|
||||
| 328d0fa5 | David Sorber | PhotoCompressArchiver(
|
||
const std::string& path,
|
||||
| 537f40e6 | David Sorber | uint32_t num_threads,
|
||
| 328d0fa5 | David Sorber | bool decompress,
|
||
| 537f40e6 | David Sorber | bool delete_orig,
|
||
| ebc2733a | David Sorber | bool verbose,
|
||
| e0b7f8b9 | David Sorber | boost::regex& filter_regex);
|
||
| b216a5b0 | David Sorber | |||
~PhotoCompressArchiver();
|
||||
/**
|
||||
* Start processing
|
||||
*/
|
||||
int execute();
|
||||
/**
|
||||
* Recursively find input files based on passed regex
|
||||
*/
|
||||
void find_files(
|
||||
const bfs::path& dir_path,
|
||||
const boost::regex& file_regex);
|
||||
/**
|
||||
| 4f6113f8 | David Sorber | * Compress/decompress worker thread body
|
||
| b216a5b0 | David Sorber | */
|
||
| 4f6113f8 | David Sorber | void worker_thread_body(uint32_t tid);
|
||
| f8e600bc | David Sorber | |||
/**
|
||||
* Calculate the return the "global" percent done as determined by the
|
||||
* m_files_processed and m_num_input_files instance variables.
|
||||
*/
|
||||
double get_global_percent_done();
|
||||
| ebc2733a | David Sorber | |||
/**
|
||||
* Asynchronously terminate processing.
|
||||
*/
|
||||
void terminate();
|
||||
| b216a5b0 | David Sorber | |||
private:
|
||||
| f8e600bc | David Sorber | const bfs::path m_path; // top level path where to look for input files
|
||
| 537f40e6 | David Sorber | uint32_t m_num_threads; // number of processing threads to use
|
||
| 328d0fa5 | David Sorber | bool m_decompress; // decompress files instead of compress them
|
||
| 537f40e6 | David Sorber | bool m_delete_orig; // delete original files after de/compression
|
||
| 13f1ac5a | David Sorber | bool m_verbose; // verbose processing output
|
||
| 328d0fa5 | David Sorber | |||
| b216a5b0 | David Sorber | std::mutex m_output_mutex; // output mutex
|
||
| ebc2733a | David Sorber | bool m_terminate_flag; // terminate flag
|
||
| ddd2cb4a | David Sorber | std::atomic<uint32_t> m_files_processed; // number of input files processed
|
||
uint32_t m_num_input_files; // total number of input files found
|
||||
uint64_t m_total_uncompressed_size; // total uncompressed size of all images
|
||||
uint64_t m_total_compressed_size; // total compressed size of all images
|
||||
| ebc2733a | David Sorber | uint64_t m_running_compressed_size; // running compressed size (for status)
|
||
| b216a5b0 | David Sorber | |||
| 4f6113f8 | David Sorber | // input file list (either images or compressed images)
|
||
ProtectedAndSynchronizedQueue<WorkUnit*> m_file_list;
|
||||
| b216a5b0 | David Sorber | |||
| 537f40e6 | David Sorber | // list of any errors
|
||
ProtectedAndSynchronizedQueue<WorkUnit*> m_errors;
|
||||
| f8e600bc | David Sorber | std::thread* p_finder_thread; // file finder thread
|
||
std::vector<std::thread*> m_worker_threads; // worker threads
|
||||
| e0b7f8b9 | David Sorber | |||
bool m_filter_empty; // is the filter regex empty
|
||||
boost::regex& m_filter_regex; // filter regular expression
|
||||
| b216a5b0 | David Sorber | };
|
||
#endif // PHOTOCOMPRESSARCHIVER_H
|