kax_analyzer_c: refactor process() not to take explicit arguments

Instead those modes are configured via separate functions.
This commit is contained in:
Moritz Bunkus 2015-11-29 09:23:01 +01:00
parent b01f0d2ba5
commit 92f054dc79
7 changed files with 68 additions and 36 deletions

View File

@ -64,21 +64,16 @@ kax_analyzer_data_c::to_string() const {
}
kax_analyzer_c::kax_analyzer_c(std::string file_name)
: m_file_name(file_name)
, m_file(nullptr)
, m_close_file(true)
, m_stream(nullptr)
, m_debug{"kax_analyzer"}
{
m_file_name = file_name;
m_close_file = true;
}
kax_analyzer_c::kax_analyzer_c(mm_io_c *file)
: m_file_name(file->get_file_name())
, m_file(file)
, m_close_file(false)
, m_stream(nullptr)
, m_debug{"kax_analyzer"}
{
m_file_name = file->get_file_name();
m_file = file;
m_close_file = false;
}
kax_analyzer_c::~kax_analyzer_c() {
@ -97,11 +92,11 @@ kax_analyzer_c::close_file() {
}
void
kax_analyzer_c::reopen_file(const open_mode mode) {
kax_analyzer_c::reopen_file() {
if (m_file)
return;
m_file = new mm_file_io_c(m_file_name, mode);
m_file = new mm_file_io_c(m_file_name, m_open_mode);
m_stream = new EbmlStream(*m_file);
}
@ -203,12 +198,28 @@ kax_analyzer_c::probe(std::string file_name) {
}
}
kax_analyzer_c &
kax_analyzer_c::set_parse_mode(parse_mode_e parse_mode) {
m_parse_mode = parse_mode;
return *this;
}
kax_analyzer_c &
kax_analyzer_c::set_open_mode(open_mode mode) {
m_open_mode = mode;
return *this;
}
kax_analyzer_c &
kax_analyzer_c::set_throw_on_error(bool throw_on_error) {
m_throw_on_error = throw_on_error;
return *this;
}
bool
kax_analyzer_c::process(kax_analyzer_c::parse_mode_e parse_mode,
const open_mode mode,
bool throw_on_error) {
kax_analyzer_c::process() {
try {
auto result = process_internal(parse_mode, mode);
auto result = process_internal();
mxdebug_if(m_debug, boost::format("kax_analyzer: parsing file '%1%' result %2%\n") % m_file->get_file_name() % result);
return result;
@ -216,18 +227,17 @@ kax_analyzer_c::process(kax_analyzer_c::parse_mode_e parse_mode,
} catch (...) {
mxdebug_if(m_debug, boost::format("kax_analyzer: parsing file '%1%' failed with an exception\n") % m_file->get_file_name());
if (throw_on_error)
if (m_throw_on_error)
throw;
return false;
}
}
bool
kax_analyzer_c::process_internal(kax_analyzer_c::parse_mode_e parse_mode,
const open_mode mode) {
bool parse_fully = parse_mode_full == parse_mode;
kax_analyzer_c::process_internal() {
bool parse_fully = parse_mode_full == m_parse_mode;
reopen_file(mode);
reopen_file();
int64_t file_size = m_file->get_size();
show_progress_start(file_size);
@ -301,7 +311,7 @@ kax_analyzer_c::process_internal(kax_analyzer_c::parse_mode_e parse_mode,
show_progress_done();
if (!aborted) {
if (parse_mode_full != parse_mode)
if (parse_mode_full != m_parse_mode)
fix_element_sizes(file_size);
return true;
@ -1269,7 +1279,10 @@ bitvalue_cptr
kax_analyzer_c::read_segment_uid_from(std::string const &file_name) {
try {
auto analyzer = std::make_shared<kax_analyzer_c>(file_name);
auto ok = analyzer->process(kax_analyzer_c::parse_mode_fast, MODE_READ, false);
auto ok = analyzer
->set_parse_mode(kax_analyzer_c::parse_mode_fast)
.set_open_mode(MODE_READ)
.process();
if (ok) {
auto element = analyzer->read_all(EBML_INFO(KaxInfo));

View File

@ -93,12 +93,15 @@ public:
private:
std::vector<kax_analyzer_data_cptr> m_data;
std::string m_file_name;
mm_io_c *m_file;
bool m_close_file;
mm_io_c *m_file{};
bool m_close_file{true};
std::shared_ptr<KaxSegment> m_segment;
std::map<int64_t, bool> m_meta_seeks_by_position;
EbmlStream *m_stream;
debugging_option_c m_debug;
EbmlStream *m_stream{};
debugging_option_c m_debug{"kax_analyzer"};
parse_mode_e m_parse_mode{parse_mode_full};
open_mode m_open_mode{MODE_WRITE};
bool m_throw_on_error{};
public: // Static functions
static bool probe(std::string file_name);
@ -124,7 +127,11 @@ public:
virtual uint64_t get_segment_pos() const;
virtual uint64_t get_segment_data_start_pos() const;
virtual bool process(parse_mode_e parse_mode = parse_mode_full, const open_mode mode = MODE_WRITE, bool throw_on_error = false);
virtual kax_analyzer_c &set_parse_mode(parse_mode_e parse_mode);
virtual kax_analyzer_c &set_open_mode(open_mode mode);
virtual kax_analyzer_c &set_throw_on_error(bool throw_on_error);
virtual bool process();
virtual void show_progress_start(int64_t /* size */) {
}
@ -145,7 +152,7 @@ public:
}
virtual void close_file();
virtual void reopen_file(const open_mode = MODE_WRITE);
virtual void reopen_file();
virtual mm_io_c &get_file() {
return *m_file;
}
@ -184,7 +191,7 @@ protected:
virtual void fix_element_sizes(uint64_t file_size);
protected:
virtual bool process_internal(parse_mode_e parse_mode, const open_mode mode);
virtual bool process_internal();
};
using kax_analyzer_cptr = std::shared_ptr<kax_analyzer_c>;

View File

@ -45,7 +45,11 @@ open_and_analyze(std::string const &file_name,
// open input file
try {
auto analyzer = std::make_shared<kax_analyzer_c>(file_name);
auto ok = analyzer->process(parse_mode, MODE_READ, exit_on_error);
auto ok = analyzer
->set_parse_mode(parse_mode)
.set_open_mode(MODE_READ)
.set_throw_on_error(exit_on_error)
.process();
return ok ? analyzer : kax_analyzer_cptr{};

View File

@ -1210,7 +1210,11 @@ kax_reader_c::find_level1_elements_via_analyzer() {
try {
auto analyzer = std::make_shared<kax_analyzer_c>(m_in.get());
auto ok = analyzer->process(kax_analyzer_c::parse_mode_fast, MODE_READ, true);
auto ok = analyzer
->set_parse_mode(kax_analyzer_c::parse_mode_fast)
.set_open_mode(MODE_READ)
.set_throw_on_error(true)
.process();
if (!ok)
return;

View File

@ -229,7 +229,7 @@ Tab::LoadResult
Tab::loadFromMatroskaFile() {
m_analyzer = std::make_unique<QtKaxAnalyzer>(this, m_fileName);
if (!m_analyzer->process(kax_analyzer_c::parse_mode_fast)) {
if (!m_analyzer->set_parse_mode(kax_analyzer_c::parse_mode_fast).process()) {
Util::MessageBox::critical(this)->title(QY("File parsing failed")).text(QY("The file you tried to open (%1) could not be read successfully.").arg(m_fileName)).exec();
emit removeThisTab();
return {};
@ -531,7 +531,7 @@ Tab::saveToMatroskaImpl(bool requireNewFileName) {
if (doRequireNewFileName || (QFileInfo{newFileName}.lastModified() != m_fileModificationTime)) {
m_analyzer = std::make_unique<QtKaxAnalyzer>(this, newFileName);
if (!m_analyzer->process(kax_analyzer_c::parse_mode_fast)) {
if (!m_analyzer->set_parse_mode(kax_analyzer_c::parse_mode_fast).process()) {
Util::MessageBox::critical(this)->title(QY("File parsing failed")).text(QY("The file you tried to open (%1) could not be read successfully.").arg(newFileName)).exec();
return false;
}

View File

@ -89,7 +89,7 @@ Tab::load() {
m_analyzer = std::make_unique<QtKaxAnalyzer>(this, m_fileName);
if (!m_analyzer->process(kax_analyzer_c::parse_mode_fast)) {
if (!m_analyzer->set_parse_mode(kax_analyzer_c::parse_mode_fast).process()) {
Util::MessageBox::critical(this)->title(QY("File parsing failed")).text(QY("The file you tried to open (%1) could not be read successfully.").arg(m_fileName)).exec();
emit removeThisTab();
return;

View File

@ -97,7 +97,11 @@ run(options_cptr &options) {
bool ok = false;
try {
ok = analyzer->process(options->m_parse_mode, MODE_WRITE, true);
ok = analyzer
->set_parse_mode(options->m_parse_mode)
.set_open_mode(MODE_WRITE)
.set_throw_on_error(true)
.process();
} catch (mtx::mm_io::exception &ex) {
mxerror(boost::format(Y("The file '%1%' could not be opened for reading and writing, or a read/write operation on it failed: %2%.\n")) % options->m_file_name % ex);
} catch (...) {