From 92f054dc793da3e218957c38f6f07e078116019b Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sun, 29 Nov 2015 09:23:01 +0100 Subject: [PATCH] kax_analyzer_c: refactor process() not to take explicit arguments Instead those modes are configured via separate functions. --- src/common/kax_analyzer.cpp | 59 ++++++++++++++--------- src/common/kax_analyzer.h | 21 +++++--- src/extract/mkvextract.cpp | 6 ++- src/input/r_matroska.cpp | 6 ++- src/mkvtoolnix-gui/chapter_editor/tab.cpp | 4 +- src/mkvtoolnix-gui/header_editor/tab.cpp | 2 +- src/propedit/propedit.cpp | 6 ++- 7 files changed, 68 insertions(+), 36 deletions(-) diff --git a/src/common/kax_analyzer.cpp b/src/common/kax_analyzer.cpp index e0dd3041b..34165e815 100644 --- a/src/common/kax_analyzer.cpp +++ b/src/common/kax_analyzer.cpp @@ -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(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)); diff --git a/src/common/kax_analyzer.h b/src/common/kax_analyzer.h index ed4a98e77..b8c7e9077 100644 --- a/src/common/kax_analyzer.h +++ b/src/common/kax_analyzer.h @@ -93,12 +93,15 @@ public: private: std::vector 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 m_segment; std::map 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; diff --git a/src/extract/mkvextract.cpp b/src/extract/mkvextract.cpp index 15d2982b1..7946340e5 100644 --- a/src/extract/mkvextract.cpp +++ b/src/extract/mkvextract.cpp @@ -45,7 +45,11 @@ open_and_analyze(std::string const &file_name, // open input file try { auto analyzer = std::make_shared(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{}; diff --git a/src/input/r_matroska.cpp b/src/input/r_matroska.cpp index f8491013c..cf6a6e8e0 100644 --- a/src/input/r_matroska.cpp +++ b/src/input/r_matroska.cpp @@ -1210,7 +1210,11 @@ kax_reader_c::find_level1_elements_via_analyzer() { try { auto analyzer = std::make_shared(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; diff --git a/src/mkvtoolnix-gui/chapter_editor/tab.cpp b/src/mkvtoolnix-gui/chapter_editor/tab.cpp index a681086f9..438aeb0e2 100644 --- a/src/mkvtoolnix-gui/chapter_editor/tab.cpp +++ b/src/mkvtoolnix-gui/chapter_editor/tab.cpp @@ -229,7 +229,7 @@ Tab::LoadResult Tab::loadFromMatroskaFile() { m_analyzer = std::make_unique(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(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; } diff --git a/src/mkvtoolnix-gui/header_editor/tab.cpp b/src/mkvtoolnix-gui/header_editor/tab.cpp index 0659cf96d..d440bf362 100644 --- a/src/mkvtoolnix-gui/header_editor/tab.cpp +++ b/src/mkvtoolnix-gui/header_editor/tab.cpp @@ -89,7 +89,7 @@ Tab::load() { m_analyzer = std::make_unique(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; diff --git a/src/propedit/propedit.cpp b/src/propedit/propedit.cpp index 4ebcb74ac..4b71eb021 100644 --- a/src/propedit/propedit.cpp +++ b/src/propedit/propedit.cpp @@ -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 (...) {