Skip to content

Commit 0812515

Browse files
committed
more tidy
1 parent d004994 commit 0812515

9 files changed

Lines changed: 172 additions & 167 deletions

src/odr/internal/ooxml/ooxml_crypto.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace {
1313
template <typename I, typename O> void to_little_endian(I in, O &out) {
14-
for (int i = 0; i < sizeof(in); ++i) {
14+
for (unsigned int i = 0; i < sizeof(in); ++i) {
1515
out[i] = in & 0xff;
1616
in >>= 8;
1717
}
@@ -25,13 +25,14 @@ template <typename I, typename O> void to_big_endian(I in, O &out) {
2525
}
2626

2727
std::string xor_bytes(const std::string &a, const std::string &b) {
28-
if (a.size() != b.size())
28+
if (a.size() != b.size()) {
2929
throw std::invalid_argument("a.size() != b.size()");
30+
}
3031

3132
std::string result(a.size(), ' ');
3233

3334
for (std::size_t i = 0; i < result.size(); ++i) {
34-
result[i] = a[i] ^ b[i];
35+
result[i] = static_cast<char>(a[i] ^ b[i]);
3536
}
3637

3738
return result;

src/odr/internal/ooxml/text/ooxml_text_document.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
namespace odr::internal::ooxml::text {
1717

1818
Document::Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem)
19-
: TemplateDocument<Element>(FileType::office_open_xml_document,
20-
DocumentType::text, std::move(filesystem)) {
19+
: TemplateDocument(FileType::office_open_xml_document, DocumentType::text,
20+
std::move(filesystem)) {
2121
m_document_xml =
2222
util::xml::parse(*m_filesystem, AbsPath("/word/document.xml"));
2323
m_styles_xml = util::xml::parse(*m_filesystem, AbsPath("/word/styles.xml"));

src/odr/internal/ooxml/text/ooxml_text_document.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
22

3-
#include <odr/file.hpp>
4-
53
#include <odr/internal/common/document.hpp>
64
#include <odr/internal/common/path.hpp>
75
#include <odr/internal/ooxml/text/ooxml_text_element.hpp>
@@ -11,19 +9,17 @@
119
#include <string>
1210
#include <unordered_map>
1311

14-
#include <pugixml.hpp>
15-
1612
namespace odr::internal::ooxml::text {
1713

1814
class Document final : public TemplateDocument<Element> {
1915
public:
2016
explicit Document(std::shared_ptr<abstract::ReadableFilesystem> filesystem);
2117

22-
[[nodiscard]] bool is_editable() const noexcept final;
23-
[[nodiscard]] bool is_savable(bool encrypted) const noexcept final;
18+
[[nodiscard]] bool is_editable() const noexcept override;
19+
[[nodiscard]] bool is_savable(bool encrypted) const noexcept override;
2420

25-
void save(const Path &path) const final;
26-
void save(const Path &path, const char *password) const final;
21+
void save(const Path &path) const override;
22+
void save(const Path &path, const char *password) const override;
2723

2824
private:
2925
pugi::xml_document m_document_xml;

src/odr/internal/ooxml/text/ooxml_text_element.cpp

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
#include <optional>
1212

13-
#include <pugixml.hpp>
14-
1513
namespace odr::internal::ooxml::text {
1614

17-
Element::Element(pugi::xml_node node) : m_node{node} {
15+
Element::Element(const pugi::xml_node node) : m_node{node} {
1816
if (!node) {
1917
// TODO log error
2018
throw std::runtime_error("node not set");
@@ -28,8 +26,7 @@ ResolvedStyle Element::partial_style(const abstract::Document *) const {
2826
ResolvedStyle
2927
Element::intermediate_style(const abstract::Document *document) const {
3028
ResolvedStyle base;
31-
abstract::Element *parent = this->parent(document);
32-
if (parent == nullptr) {
29+
if (abstract::Element *parent = this->parent(document); parent == nullptr) {
3330
base = style_(document)->default_style()->resolved();
3431
} else {
3532
base = dynamic_cast<Element *>(parent)->intermediate_style(document);
@@ -87,9 +84,9 @@ TextStyle Span::style(const abstract::Document *document) const {
8784
return intermediate_style(document).text_style;
8885
}
8986

90-
Text::Text(pugi::xml_node node) : Text(node, node) {}
87+
Text::Text(const pugi::xml_node node) : Text(node, node) {}
9188

92-
Text::Text(pugi::xml_node first, pugi::xml_node last)
89+
Text::Text(const pugi::xml_node first, const pugi::xml_node last)
9390
: Element(first), m_last{last} {
9491
if (!last) {
9592
// TODO log error
@@ -111,14 +108,14 @@ void Text::set_content(const abstract::Document *, const std::string &text) {
111108
// <w:t xml:space="preserve">
112109
// use `xml:space`
113110

114-
auto parent = m_node.parent();
115-
auto old_first = m_node;
116-
auto old_last = m_last;
117-
auto new_first = old_first;
118-
auto new_last = m_last;
111+
pugi::xml_node parent = m_node.parent();
112+
const pugi::xml_node old_first = m_node;
113+
const pugi::xml_node old_last = m_last;
114+
pugi::xml_node new_first = old_first;
115+
pugi::xml_node new_last = m_last;
119116

120117
const auto insert_node = [&](const char *node) {
121-
auto new_node = parent.insert_child_before(node, old_first);
118+
const pugi::xml_node new_node = parent.insert_child_before(node, old_first);
122119
if (new_first == old_first) {
123120
new_first = new_node;
124121
}
@@ -154,8 +151,8 @@ void Text::set_content(const abstract::Document *, const std::string &text) {
154151
m_node = new_first;
155152
m_last = new_last;
156153

157-
for (auto node = old_first; node != old_last.next_sibling();) {
158-
auto next = node.next_sibling();
154+
for (pugi::xml_node node = old_first; node != old_last.next_sibling();) {
155+
const pugi::xml_node next = node.next_sibling();
159156
parent.remove_child(node);
160157
node = next;
161158
}
@@ -166,7 +163,7 @@ TextStyle Text::style(const abstract::Document *document) const {
166163
}
167164

168165
std::string Text::text_(const pugi::xml_node node) {
169-
std::string name = node.name();
166+
const std::string name = node.name();
170167

171168
if (name == "w:t") {
172169
return node.text().get();
@@ -179,12 +176,13 @@ std::string Text::text_(const pugi::xml_node node) {
179176
}
180177

181178
std::string Link::href(const abstract::Document *document) const {
182-
if (auto anchor = m_node.attribute("w:anchor")) {
179+
if (const pugi::xml_attribute anchor = m_node.attribute("w:anchor")) {
183180
return std::string("#") + anchor.value();
184181
}
185-
if (auto ref = m_node.attribute("r:id")) {
182+
if (const pugi::xml_attribute ref = m_node.attribute("r:id")) {
186183
auto relations = document_relations_(document);
187-
if (auto rel = relations.find(ref.value()); rel != std::end(relations)) {
184+
if (const auto rel = relations.find(ref.value());
185+
rel != std::end(relations)) {
188186
return rel->second;
189187
}
190188
}
@@ -213,7 +211,8 @@ TableStyle Table::style(const abstract::Document *document) const {
213211

214212
TableColumnStyle TableColumn::style(const abstract::Document *) const {
215213
TableColumnStyle result;
216-
if (auto width = read_twips_attribute(m_node.attribute("w:w"))) {
214+
if (const std::optional<Measure> width =
215+
read_twips_attribute(m_node.attribute("w:w"))) {
217216
result.width = width;
218217
}
219218
return result;
@@ -254,7 +253,7 @@ std::optional<std::string> Frame::y(const abstract::Document *) const {
254253

255254
std::optional<std::string>
256255
Frame::width(const abstract::Document *document) const {
257-
if (auto width = read_emus_attribute(
256+
if (const std::optional<Measure> width = read_emus_attribute(
258257
inner_node_(document).child("wp:extent").attribute("cx"))) {
259258
return width->to_string();
260259
}
@@ -263,7 +262,7 @@ Frame::width(const abstract::Document *document) const {
263262

264263
std::optional<std::string>
265264
Frame::height(const abstract::Document *document) const {
266-
if (auto height = read_emus_attribute(
265+
if (const std::optional<Measure> height = read_emus_attribute(
267266
inner_node_(document).child("wp:extent").attribute("cy"))) {
268267
return height->to_string();
269268
}
@@ -277,17 +276,18 @@ std::optional<std::string> Frame::z_index(const abstract::Document *) const {
277276
GraphicStyle Frame::style(const abstract::Document *) const { return {}; }
278277

279278
pugi::xml_node Frame::inner_node_(const abstract::Document *) const {
280-
if (auto anchor = m_node.child("wp:anchor")) {
279+
if (const pugi::xml_node anchor = m_node.child("wp:anchor")) {
281280
return anchor;
282-
} else if (auto inline_node = m_node.child("wp:inline")) {
281+
}
282+
if (const pugi::xml_node inline_node = m_node.child("wp:inline")) {
283283
return inline_node;
284284
}
285285
return {};
286286
}
287287

288288
bool Image::is_internal(const abstract::Document *document) const {
289-
auto doc = document_(document);
290-
if (!doc || !doc->as_filesystem()) {
289+
const Document *doc = document_(document);
290+
if (doc == nullptr || !doc->as_filesystem()) {
291291
return false;
292292
}
293293
try {
@@ -297,22 +297,23 @@ bool Image::is_internal(const abstract::Document *document) const {
297297
return false;
298298
}
299299

300-
std::optional<odr::File> Image::file(const abstract::Document *document) const {
301-
auto doc = document_(document);
302-
if (!doc || !is_internal(document)) {
300+
std::optional<File> Image::file(const abstract::Document *document) const {
301+
const Document *doc = document_(document);
302+
if (doc == nullptr || !is_internal(document)) {
303303
return {};
304304
}
305-
AbsPath path = Path(href(document)).make_absolute();
305+
const AbsPath path = Path(href(document)).make_absolute();
306306
return File(doc->as_filesystem()->open(path));
307307
}
308308

309309
std::string Image::href(const abstract::Document *document) const {
310-
if (auto ref = m_node.child("pic:pic")
311-
.child("pic:blipFill")
312-
.child("a:blip")
313-
.attribute("r:embed")) {
310+
if (const pugi::xml_attribute ref = m_node.child("pic:pic")
311+
.child("pic:blipFill")
312+
.child("a:blip")
313+
.attribute("r:embed")) {
314314
auto relations = document_relations_(document);
315-
if (auto rel = relations.find(ref.value()); rel != std::end(relations)) {
315+
if (const auto rel = relations.find(ref.value());
316+
rel != std::end(relations)) {
316317
return AbsPath("/word").join(RelPath(rel->second)).string();
317318
}
318319
}

0 commit comments

Comments
 (0)