Panini 1.4.0
Header-only library for generating C++, written in C++17
CompareWriter.hpp
Go to the documentation of this file.
1/*
2 MIT No Attribution
3
4 Copyright 2021-2023 Mr. Hands
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE.
20*/
21
22#pragma once
23
25#include "writers/Writer.hpp"
26
27namespace panini
28{
29
44 : public ConfiguredWriter<CompareWriterConfig>
45 {
46
47 public:
53 inline explicit CompareWriter(const CompareWriterConfig& config = {})
54 : CompareWriter(config.filePath, config)
55 {
56 }
57
68 inline explicit CompareWriter(
69 const std::filesystem::path& filePath,
70 const WriterConfig& config = WriterConfig())
72 {
73 m_config.filePath = filePath;
74
75 // read the previous output, if available
76
77 std::ifstream stream(m_config.filePath.string(), std::ios::binary);
78 m_pathExists = stream.is_open();
79 if (m_pathExists)
80 {
81 stream.seekg(0, std::ios::end);
82 m_writtenPrevious.resize(static_cast<size_t>(stream.tellg()));
83 stream.seekg(0, std::ios::beg);
84 stream.read(&m_writtenPrevious[0], m_writtenPrevious.size());
85 stream.close();
86
87 m_writtenCurrent.reserve(m_writtenPrevious.size());
88 }
89 }
90
94 inline ~CompareWriter() override
95 {
96 Commit();
97 }
98
103 inline bool IsChanged() const override
104 {
106 }
107
108 protected:
109 inline void Write(const std::string& chunk) override
110 {
111 m_writtenCurrent += chunk;
112 }
113
114 inline bool OnCommit(bool force = false) override
115 {
116 (void)force;
117
118 std::ofstream stream(m_config.filePath.string(), std::ios::binary);
119 if (!stream.is_open())
120 {
121 return false;
122 }
123
124 stream.write(m_writtenCurrent.c_str(), m_writtenCurrent.length());
125 stream.close();
126
128
129 return true;
130 }
131
132 protected:
133 bool m_pathExists = false;
134 std::string m_writtenPrevious;
135 std::string m_writtenCurrent;
136
137 };
138
139};
Writes output to a path only when the written bytes differ from what was loaded on disk.
Definition: CompareWriter.hpp:45
std::string m_writtenPrevious
Definition: CompareWriter.hpp:134
bool OnCommit(bool force=false) override
Definition: CompareWriter.hpp:114
bool IsChanged() const override
Definition: CompareWriter.hpp:103
CompareWriter(const std::filesystem::path &filePath, const WriterConfig &config=WriterConfig())
Definition: CompareWriter.hpp:68
~CompareWriter() override
Definition: CompareWriter.hpp:94
std::string m_writtenCurrent
Definition: CompareWriter.hpp:135
bool m_pathExists
Definition: CompareWriter.hpp:133
CompareWriter(const CompareWriterConfig &config={})
Definition: CompareWriter.hpp:53
void Write(const std::string &chunk) override
Definition: CompareWriter.hpp:109
Base class implementation for writers.
Definition: Writer.hpp:185
bool Commit(bool force=false) override
Definition: Writer.hpp:453
CompareWriterConfig m_config
Definition: Writer.hpp:468
Definition: Braces.hpp:29
Configuration for the CompareWriter class.
Definition: CompareWriterConfig.hpp:37
std::filesystem::path filePath
Definition: CompareWriterConfig.hpp:41
Global configuration applied to writers.
Definition: WriterConfig.hpp:39