diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-09-16 17:38:43 +0200 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-09-16 23:49:13 +0200 |
| commit | 02dd03b4ef42db0a43b2011b5c34446f6f987c12 (patch) | |
| tree | 1d78e3b1b5bc63cf38d72edc9cba485bd9727c0d /src | |
| parent | b33a97e3aa7dd3a0ddd3687762ff42a9a9e1079f (diff) | |
Extract StreamFormatter.
Diffstat (limited to 'src')
| -rw-r--r-- | src/html_formatter.cc | 13 | ||||
| -rw-r--r-- | src/html_formatter.h | 11 | ||||
| -rw-r--r-- | src/stream_formatter.h | 92 | ||||
| -rw-r--r-- | src/text_formatter.cc | 7 | ||||
| -rw-r--r-- | src/text_formatter.h | 5 | ||||
| -rw-r--r-- | src/verify_report.cc | 2 | ||||
| -rw-r--r-- | src/verify_report.h | 52 | ||||
| -rw-r--r-- | src/wscript | 1 |
8 files changed, 120 insertions, 63 deletions
diff --git a/src/html_formatter.cc b/src/html_formatter.cc index 5e223430..0e796cc3 100644 --- a/src/html_formatter.cc +++ b/src/html_formatter.cc @@ -23,12 +23,13 @@ #include <boost/filesystem.hpp> +using std::unique_ptr; using namespace dcp; HTMLFormatter::HTMLFormatter(boost::filesystem::path file) - : Formatter(file) + : StreamFormatter(file) { } @@ -48,7 +49,7 @@ HTMLFormatter::subheading(std::string const& text) } -Formatter::Wrap +unique_ptr<Formatter::Wrap> HTMLFormatter::document() { auto html = wrapped("html"); @@ -79,14 +80,14 @@ HTMLFormatter::document() } -Formatter::Wrap +unique_ptr<Formatter::Wrap> HTMLFormatter::body() { return wrapped("body"); } -Formatter::Wrap +unique_ptr<Formatter::Wrap> HTMLFormatter::unordered_list() { return wrapped("ul"); @@ -131,10 +132,10 @@ HTMLFormatter::tagged(std::string tag, std::string content) } -HTMLFormatter::Wrap +unique_ptr<Formatter::Wrap> HTMLFormatter::wrapped(std::string const& tag) { _file.puts(String::compose("<%1>", tag).c_str()); - return Wrap(this, String::compose("</%1>", tag)); + return unique_ptr<Formatter::Wrap>(new Wrap(this, String::compose("</%1>", tag))); } diff --git a/src/html_formatter.h b/src/html_formatter.h index b566db86..2f87a758 100644 --- a/src/html_formatter.h +++ b/src/html_formatter.h @@ -19,22 +19,23 @@ */ +#include "stream_formatter.h" #include "verify_report.h" namespace dcp { -class HTMLFormatter : public Formatter +class HTMLFormatter : public StreamFormatter { public: HTMLFormatter(boost::filesystem::path file); void heading(std::string const& text) override; void subheading(std::string const& text) override; - Wrap document() override; - Wrap body() override; - Wrap unordered_list() override; + std::unique_ptr<Formatter::Wrap> document() override; + std::unique_ptr<Formatter::Wrap> body() override; + std::unique_ptr<Formatter::Wrap> unordered_list() override; void list_item(std::string const& text, boost::optional<std::string> type = {}) override; std::function<std::string (std::string)> process_string() override; @@ -42,7 +43,7 @@ public: private: void tagged(std::string tag, std::string content); - Wrap wrapped(std::string const& tag); + std::unique_ptr<Formatter::Wrap> wrapped(std::string const& tag); }; } diff --git a/src/stream_formatter.h b/src/stream_formatter.h new file mode 100644 index 00000000..acdcbb4c --- /dev/null +++ b/src/stream_formatter.h @@ -0,0 +1,92 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#ifndef LIBDCP_STREAM_FORMATTER_H +#define LIBDCP_STREAM_FORMATTER_H + + +#include "file.h" +#include "verify_report.h" + + +namespace dcp { + + +class StreamFormatter : public Formatter +{ +public: + StreamFormatter(boost::filesystem::path file) + : _file(file, "w") + {} + + class Wrap : public Formatter::Wrap + { + public: + Wrap() = default; + + Wrap(StreamFormatter* formatter, std::string const& close) + : _formatter(formatter) + , _close(close) + {} + + Wrap(StreamFormatter* formatter, std::string const& close, std::function<void ()> closer) + : _formatter(formatter) + , _close(close) + , _closer(closer) + {} + + Wrap(Wrap&& other) + { + std::swap(_formatter, other._formatter); + std::swap(_close, other._close); + std::swap(_closer, other._closer); + } + + ~Wrap() + { + if (_formatter) { + _formatter->file().puts(_close.c_str()); + } + if (_closer) { + _closer(); + } + } + + private: + StreamFormatter* _formatter = nullptr; + std::string _close; + std::function<void ()> _closer = nullptr; + }; + + dcp::File& file() { + return _file; + } + +protected: + dcp::File _file; +}; + + +} + + +#endif + diff --git a/src/text_formatter.cc b/src/text_formatter.cc index a5c630b3..a147ea83 100644 --- a/src/text_formatter.cc +++ b/src/text_formatter.cc @@ -22,11 +22,12 @@ #include "text_formatter.h" +using std::unique_ptr; using namespace dcp; TextFormatter::TextFormatter(boost::filesystem::path file) - : Formatter(file) + : StreamFormatter(file) { } @@ -47,11 +48,11 @@ TextFormatter::subheading(std::string const& text) } -Formatter::Wrap +unique_ptr<Formatter::Wrap> TextFormatter::unordered_list() { _indent++; - return Wrap(this, "", [this]() { _indent--; }); + return unique_ptr<Formatter::Wrap>(new Wrap(this, "", [this]() { _indent--; })); } diff --git a/src/text_formatter.h b/src/text_formatter.h index df462576..8fdb0d89 100644 --- a/src/text_formatter.h +++ b/src/text_formatter.h @@ -19,20 +19,21 @@ */ +#include "stream_formatter.h" #include "verify_report.h" namespace dcp { -class TextFormatter : public Formatter +class TextFormatter : public StreamFormatter { public: TextFormatter(boost::filesystem::path file); void heading(std::string const& text) override; void subheading(std::string const& text) override; - Wrap unordered_list() override; + std::unique_ptr<Formatter::Wrap> unordered_list() override; void list_item(std::string const& text, boost::optional<std::string> type = {}) override; std::function<std::string (std::string)> process_string() override; std::function<std::string (std::string)> fixed_width() override; diff --git a/src/verify_report.cc b/src/verify_report.cc index fd95fdb3..521ed183 100644 --- a/src/verify_report.cc +++ b/src/verify_report.cc @@ -145,6 +145,8 @@ verify_report(dcp::VerificationResult const& result, Formatter& formatter) formatter.subheading("Report"); write_notes(result, {}); } + + formatter.finish(); } diff --git a/src/verify_report.h b/src/verify_report.h index 612918eb..7be8d9d9 100644 --- a/src/verify_report.h +++ b/src/verify_report.h @@ -36,67 +36,25 @@ namespace dcp { class Formatter { public: - Formatter(boost::filesystem::path file) - : _file(file, "w") - {} - class Wrap { public: - Wrap() = default; - - Wrap(Formatter* formatter, std::string const& close) - : _formatter(formatter) - , _close(close) - {} - - Wrap(Formatter* formatter, std::string const& close, std::function<void ()> closer) - : _formatter(formatter) - , _close(close) - , _closer(closer) - {} - - Wrap(Wrap&& other) - { - std::swap(_formatter, other._formatter); - std::swap(_close, other._close); - std::swap(_closer, other._closer); - } - - ~Wrap() - { - if (_formatter) { - _formatter->file().puts(_close.c_str()); - } - if (_closer) { - _closer(); - } - } - - private: - Formatter* _formatter = nullptr; - std::string _close; - std::function<void ()> _closer = nullptr; + virtual ~Wrap() {} }; - virtual Wrap document() { return {}; } + virtual std::unique_ptr<Wrap> document() { return {}; } virtual void heading(std::string const& text) = 0; virtual void subheading(std::string const& text) = 0; - virtual Wrap body() { return {}; } + virtual std::unique_ptr<Wrap> body() { return {}; } - virtual Wrap unordered_list() = 0; + virtual std::unique_ptr<Wrap> unordered_list() = 0; virtual void list_item(std::string const& text, boost::optional<std::string> type = {}) = 0; virtual std::function<std::string (std::string)> process_string() = 0; virtual std::function<std::string (std::string)> fixed_width() = 0; - dcp::File& file() { - return _file; - } - -protected: - dcp::File _file; + virtual void finish() {} }; diff --git a/src/wscript b/src/wscript index 14ce2292..3e3b75b8 100644 --- a/src/wscript +++ b/src/wscript @@ -242,6 +242,7 @@ def build(bld): stereo_j2k_picture_asset_reader.h stereo_j2k_picture_asset_writer.h stereo_j2k_picture_frame.h + stream_formatter.h text.h text_asset.h text_image.h |
