OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLStyles_Internal.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLSTYLES_INTERNAL_HPP
2#define OPENXLSX_XLSTYLES_INTERNAL_HPP
3
4#include <cctype>
5#include <string>
6#include <string_view>
7
8#include <fmt/format.h>
9#include <pugixml.hpp>
10
11#include "XLException.hpp"
12#include "XLXmlParser.hpp"
13
14namespace OpenXLSX
15{
16
17 inline void copyXMLNode(XMLNode& destination, const XMLNode& source)
18 {
19 if (not source.empty()) {
20 // ===== Copy all XML child nodes
21 for (XMLNode child = source.first_child(); not child.empty(); child = child.next_sibling()) destination.append_copy(child);
22 // ===== Copy all XML attributes
23 for (XMLAttribute attr = source.first_attribute(); not attr.empty(); attr = attr.next_attribute())
24 destination.append_copy(attr);
25 }
26 }
27
35 inline std::string xmlNodeFingerprint(const XMLNode& node)
36 {
37 std::string fp;
38 // Attributes first, in document order
39 for (XMLAttribute attr = node.first_attribute(); attr; attr = attr.next_attribute())
40 fp += attr.name() + std::string("=") + attr.value() + ';';
41 // Element and text children
42 for (XMLNode child = node.first_child(); child; child = child.next_sibling()) {
43 if (child.type() == pugi::node_pcdata) {
44 std::string_view v(child.value());
45 // Skip whitespace-only text (indentation artifacts)
46 bool isWhitespace = std::all_of(v.begin(), v.end(), [](char c) { return std::isspace(static_cast<unsigned char>(c)); });
47 if (isWhitespace) continue;
48 fp += '[' + std::string(child.value()) + ']';
49 }
50 else {
51 fp += '<' + std::string(child.name()) + ':' + xmlNodeFingerprint(child) + '>';
52 }
53 }
54 return fp;
55 }
56
57 template<typename E>
59 {
60 std::string_view str;
61 E val;
62 };
63
64 template<typename E, size_t N>
65 constexpr E EnumFromString(std::string_view str, const EnumStringMap<E> (&mapping)[N], E invalidVal)
66 {
67 for (const auto& kv : mapping) {
68 if (kv.str == str) return kv.val;
69 }
70 return invalidVal;
71 }
72
73 template<typename E, size_t N>
74 constexpr const char* EnumToString(E val, const EnumStringMap<E> (&mapping)[N], const char* invalidStr)
75 {
76 for (const auto& kv : mapping) {
77 if (kv.val == val) return kv.str.data();
78 }
79 return invalidStr;
80 }
81
85 inline std::string formatDoubleAsString(double val, int decimalPlaces = 2) { return fmt::format("{:.{}f}", val, decimalPlaces); }
86
90 inline std::string checkAndFormatDoubleAsString(double val, double min, double max, double absTolerance, int decimalPlaces = 2)
91 {
92 if (max <= min or absTolerance < 0.0)
93 throw XLException("checkAndFormatDoubleAsString: max must be greater than min and absTolerance must be >= 0.0");
94 if (val < min - absTolerance or val > max + absTolerance) return ""; // range check
95 if (val < min)
96 val = min;
97 else if (val > max)
98 val = max; // fix rounding errors within tolerance
99 return formatDoubleAsString(val, decimalPlaces);
100 }
101
102} // namespace OpenXLSX
103
104#endif // OPENXLSX_XLSTYLES_INTERNAL_HPP
Definition XLXmlParser.hpp:84
const pugi::char_t * name() const
get node name while stripping namespace, if so configured (name_begin > 0)
Definition XLXmlParser.hpp:135
Definition XLException.hpp:23
Definition IZipArchive.hpp:18
std::string formatDoubleAsString(double val, int decimalPlaces=2)
Format val as a string with decimalPlaces.
Definition XLStyles_Internal.hpp:85
std::string xmlNodeFingerprint(const XMLNode &node)
Compute a canonical, deterministic string fingerprint of a pugixml subtree.
Definition XLStyles_Internal.hpp:35
void copyXMLNode(XMLNode &destination, const XMLNode &source)
Definition XLStyles_Internal.hpp:17
constexpr E EnumFromString(std::string_view str, const EnumStringMap< E >(&mapping)[N], E invalidVal)
Definition XLStyles_Internal.hpp:65
pugi::xml_attribute XMLAttribute
Definition XLXmlParser.hpp:64
std::string checkAndFormatDoubleAsString(double val, double min, double max, double absTolerance, int decimalPlaces=2)
Check that a double value is within range, and format it as a string with decimalPlaces.
Definition XLStyles_Internal.hpp:90
constexpr const char * EnumToString(E val, const EnumStringMap< E >(&mapping)[N], const char *invalidStr)
Definition XLStyles_Internal.hpp:74
Definition XLStyles_Internal.hpp:59
std::string_view str
Definition XLStyles_Internal.hpp:60
E val
Definition XLStyles_Internal.hpp:61