From e0d62b0835caae8421fc7381110af38114cf3fd9 Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Tue, 26 Jun 2018 20:32:59 +1200 Subject: [PATCH] Cleanup from review --- include/xlnt/styles/font.hpp | 4 +- include/xlnt/utils/serialisation_utils.hpp | 44 ++++++++++++++++ include/xlnt/worksheet/phonetic_pr.hpp | 52 +++++++++---------- include/xlnt/worksheet/print_options.hpp | 2 +- include/xlnt/worksheet/sheet_pr.hpp | 18 +++---- source/cell/rich_text.cpp | 4 +- .../header_footer/header_footer_code.cpp | 7 +-- source/detail/serialization/xlsx_consumer.cpp | 22 ++++---- source/detail/serialization/xlsx_producer.cpp | 49 +++++++---------- source/styles/font.cpp | 21 +++++--- source/worksheet/phonetic_pr.cpp | 40 +++++++------- 11 files changed, 151 insertions(+), 112 deletions(-) create mode 100644 include/xlnt/utils/serialisation_utils.hpp diff --git a/include/xlnt/styles/font.hpp b/include/xlnt/styles/font.hpp index b5b12fdc..195271e7 100644 --- a/include/xlnt/styles/font.hpp +++ b/include/xlnt/styles/font.hpp @@ -170,7 +170,7 @@ public: /// /// Returns the name of the font face. /// - std::string name() const; + const std::string& name() const; /// /// Returns true if this font has a color applied. @@ -232,7 +232,7 @@ public: /// /// Returns the scheme of this font. /// - std::string scheme() const; + const std::string& scheme() const; /// /// Returns true if left is exactly equal to right. diff --git a/include/xlnt/utils/serialisation_utils.hpp b/include/xlnt/utils/serialisation_utils.hpp new file mode 100644 index 00000000..23bc2ada --- /dev/null +++ b/include/xlnt/utils/serialisation_utils.hpp @@ -0,0 +1,44 @@ +// Copyright (c) 2014-2018 Thomas Fussell +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE +// +// @license: http://www.opensource.org/licenses/mit-license.php +// @author: see AUTHORS file + +#pragma once + +#include +#include + +namespace xlnt { +/// +/// Takes in any nuber and outputs a string form of that number which will +/// serialise and deserialise without loss of precision +/// +template +std::string serialize_number_to_string(Number num) +{ + // more digits and excel won't match + constexpr int Excel_Digit_Precision = 15; //sf + std::stringstream ss; + ss.precision(Excel_Digit_Precision); + ss << num; + return ss.str(); +} +} diff --git a/include/xlnt/worksheet/phonetic_pr.hpp b/include/xlnt/worksheet/phonetic_pr.hpp index e3929108..3ac802e0 100644 --- a/include/xlnt/worksheet/phonetic_pr.hpp +++ b/include/xlnt/worksheet/phonetic_pr.hpp @@ -43,29 +43,29 @@ public: /// /// possible values for alignment property /// - enum class Alignment + enum class align { - Center, - Distributed, - Left, - No_Control + center, + distributed, + left, + no_control }; /// /// possible values for type property /// - enum class Type + enum class phonetic_type { - Full_Width_Katakana, - Half_Width_Katakana, - Hiragana, - No_Conversion + full_width_katakana, + half_width_katakana, + hiragana, + no_conversion }; /// /// FontID represented by an unsigned 32-bit integer /// - using Font_ID = std::uint32_t; + using font_id_t = std::uint32_t; /// /// Default ctor for phonetic properties @@ -75,7 +75,7 @@ public: /// /// FontID ctor for phonetic properties /// - explicit phonetic_pr(Font_ID font); + explicit phonetic_pr(font_id_t font); /// /// adds the xml serialised representation of this element to the stream @@ -85,12 +85,12 @@ public: /// /// get the font index /// - Font_ID font_id() const; + font_id_t font_id() const; /// /// set the font index /// - void font_id(Font_ID font); + void font_id(font_id_t font); /// /// is the phonetic type set @@ -100,12 +100,12 @@ public: /// /// returns the phonetic type /// - Type type() const; + phonetic_type type() const; /// /// sets the phonetic type /// - void type(Type type); + void type(phonetic_type type); /// /// is the alignment set @@ -115,51 +115,51 @@ public: /// /// get the alignment /// - Alignment alignment() const; + align alignment() const; /// /// set the alignment /// - void alignment(Alignment align); + void alignment(align align); // serialisation /// /// string form of the type enum /// - static const std::string &type_as_string(Type type); + static const std::string &type_as_string(phonetic_type type); /// /// type enum from string /// - static Type type_from_string(const std::string &str); + static phonetic_type type_from_string(const std::string &str); /// /// string form of alignment enum /// - static const std::string &alignment_as_string(xlnt::phonetic_pr::Alignment type); + static const std::string &alignment_as_string(xlnt::phonetic_pr::align type); /// /// alignment enum from string /// - static Alignment alignment_from_string(const std::string &str); + static align alignment_from_string(const std::string &str); private: /// /// zero based index into style sheet font record. /// Default: 0 /// - Font_ID font_id_ = 0; + font_id_t font_id_ = 0; /// /// Type of characters to use. /// Default: full width katakana /// - xlnt::optional type_; + xlnt::optional type_; /// - /// Alignment across the cell(s). + /// align across the cell(s). /// Default: Left /// - xlnt::optional alignment_; + xlnt::optional alignment_; }; } \ No newline at end of file diff --git a/include/xlnt/worksheet/print_options.hpp b/include/xlnt/worksheet/print_options.hpp index c0ef2ccf..bcc8ca47 100644 --- a/include/xlnt/worksheet/print_options.hpp +++ b/include/xlnt/worksheet/print_options.hpp @@ -28,7 +28,7 @@ namespace xlnt { -struct print_options +struct XLNT_API print_options { /// /// if both grid_lines_set and this are true, grid lines are printed diff --git a/include/xlnt/worksheet/sheet_pr.hpp b/include/xlnt/worksheet/sheet_pr.hpp index 9994553c..86097274 100644 --- a/include/xlnt/worksheet/sheet_pr.hpp +++ b/include/xlnt/worksheet/sheet_pr.hpp @@ -31,42 +31,42 @@ namespace xlnt { -struct sheet_pr +struct XLNT_API sheet_pr { /// /// is horizontally synced to the anchor point /// - optional sync_horizontal_; + optional sync_horizontal; /// /// is vertically synced to the anchor point /// - optional sync_vertical_; + optional sync_vertical; /// /// Anchor point for worksheet's window /// - optional sync_ref_; + optional sync_ref; /// /// Lotus compatibility option /// - optional transition_evaluation_; + optional transition_evaluation; /// /// Lotus compatibility option /// - optional transition_entry_; + optional transition_entry; /// /// worksheet is published /// - optional published_; + optional published; /// /// stable name of the sheet /// - optional code_name_; + optional code_name; /// /// worksheet has one or more autofilters or advanced filters on @@ -76,6 +76,6 @@ struct sheet_pr /// /// whether the conditional formatting calculations shall be evaluated /// - optional enable_format_condition_calculation_; + optional enable_format_condition_calculation; }; } // namespace xlnt \ No newline at end of file diff --git a/source/cell/rich_text.cpp b/source/cell/rich_text.cpp index 1e0d666c..ad5cccbd 100644 --- a/source/cell/rich_text.cpp +++ b/source/cell/rich_text.cpp @@ -34,7 +34,7 @@ rich_text::rich_text(const std::string &plain_text) } rich_text::rich_text(const std::string &plain_text, const class font &text_font) - : rich_text({plain_text, optional(text_font), false}) + : rich_text(rich_text_run{plain_text, optional(text_font), false}) { } @@ -51,7 +51,7 @@ void rich_text::clear() void rich_text::plain_text(const std::string &s, bool preserve_space = false) { clear(); - add_run({s, {}, preserve_space}); + add_run(rich_text_run{s, {}, preserve_space}); } std::string rich_text::plain_text() const diff --git a/source/detail/header_footer/header_footer_code.cpp b/source/detail/header_footer/header_footer_code.cpp index 301c922c..d598ff26 100644 --- a/source/detail/header_footer/header_footer_code.cpp +++ b/source/detail/header_footer/header_footer_code.cpp @@ -22,7 +22,7 @@ // @author: see AUTHORS file #include -#include +#include namespace xlnt { namespace detail { @@ -533,10 +533,7 @@ std::string encode_header_footer(const rich_text &t, header_footer::location whe if (run.second.get().has_size()) { encoded.push_back('&'); - std::stringstream ss; - ss.precision(15); - ss << run.second.get().size(); - encoded.append(ss.str()); + encoded.append(serialize_number_to_string(run.second.get().size())); } if (run.second.get().underlined()) { diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index 7bfee386..e15ef435 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -368,8 +368,7 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id) target_.d_->sheet_title_rel_id_map_.end(), [&](const std::pair &p) { return p.second == rel_id; - }) - ->first; + })->first; auto ws = worksheet(current_worksheet_); @@ -385,31 +384,31 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id) sheet_pr props; if (parser().attribute_present("syncHorizontal")) { // optional, boolean, false - props.sync_horizontal_.set(parser().attribute("syncHorizontal")); + props.sync_horizontal.set(parser().attribute("syncHorizontal")); } if (parser().attribute_present("syncVertical")) {// optional, boolean, false - props.sync_vertical_.set(parser().attribute("syncVertical")); + props.sync_vertical.set(parser().attribute("syncVertical")); } if (parser().attribute_present("syncRef")) { // optional, ST_Ref, false - props.sync_ref_.set(cell_reference(parser().attribute("syncRef"))); + props.sync_ref.set(cell_reference(parser().attribute("syncRef"))); } if (parser().attribute_present("transitionEvaluation")) { // optional, boolean, false - props.transition_evaluation_.set(parser().attribute("transitionEvaluation")); + props.transition_evaluation.set(parser().attribute("transitionEvaluation")); } if (parser().attribute_present("transitionEntry")) {// optional, boolean, false - props.transition_entry_.set(parser().attribute("transitionEntry")); + props.transition_entry.set(parser().attribute("transitionEntry")); } if (parser().attribute_present("published")) {// optional, boolean, true - props.published_.set(parser().attribute("published")); + props.published.set(parser().attribute("published")); } if (parser().attribute_present("codeName")) { // optional, string - props.code_name_.set(parser().attribute("codeName")); + props.code_name.set(parser().attribute("codeName")); } if (parser().attribute_present("filterMode")) {// optional, boolean, false @@ -417,7 +416,7 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id) } if (parser().attribute_present("enableFormatConditionsCalculation")) {// optional, boolean, true - props.enable_format_condition_calculation_.set(parser().attribute("enableFormatConditionsCalculation")); + props.enable_format_condition_calculation.set(parser().attribute("enableFormatConditionsCalculation")); } ws.d_->sheet_properties_.set(props); while (in_element(current_worksheet_element)) @@ -1844,8 +1843,7 @@ void xlsx_consumer::read_office_document(const std::string &content_type) // CT_ target_.d_->sheet_title_rel_id_map_.end(), [&](const std::pair &p) { return p.second == worksheet_rel.id(); - }) - ->first; + })->first; auto id = sheet_title_id_map_[title]; auto index = sheet_title_index_map_[title]; diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index 06d46292..b7cae367 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -72,16 +73,6 @@ std::vector> core_property_namespace(xlnt::c return {{constants::ns("core-properties"), "cp"}}; } -std::string number_to_string(double num) -{ - // any more digits and excel won't match - constexpr int Excel_Digit_Precision = 15; - std::stringstream ss; - ss.precision(Excel_Digit_Precision); - ss << num; - return ss.str(); -} - } // namespace namespace xlnt { @@ -2220,41 +2211,41 @@ void xlsx_producer::write_worksheet(const relationship &rel) { write_start_element(xmlns, "sheetPr"); auto &props = ws.d_->sheet_properties_.get(); - if (props.sync_horizontal_.is_set()) + if (props.sync_horizontal.is_set()) { - write_attribute("syncHorizontal", props.sync_horizontal_.get()); + write_attribute("syncHorizontal", props.sync_horizontal.get()); } - if (props.sync_vertical_.is_set()) + if (props.sync_vertical.is_set()) { - write_attribute("syncVertical", props.sync_vertical_.get()); + write_attribute("syncVertical", props.sync_vertical.get()); } - if (props.sync_ref_.is_set()) + if (props.sync_ref.is_set()) { - write_attribute("syncRef", props.sync_ref_.get().to_string()); + write_attribute("syncRef", props.sync_ref.get().to_string()); } - if (props.transition_evaluation_.is_set()) + if (props.transition_evaluation.is_set()) { - write_attribute("transitionEvaluation", props.transition_evaluation_.get()); + write_attribute("transitionEvaluation", props.transition_evaluation.get()); } - if (props.transition_entry_.is_set()) + if (props.transition_entry.is_set()) { - write_attribute("transitionEntry", props.transition_entry_.get()); + write_attribute("transitionEntry", props.transition_entry.get()); } - if (props.published_.is_set()) + if (props.published.is_set()) { - write_attribute("published", props.published_.get()); + write_attribute("published", props.published.get()); } - if (props.code_name_.is_set()) + if (props.code_name.is_set()) { - write_attribute("codeName", props.code_name_.get()); + write_attribute("codeName", props.code_name.get()); } if (props.filter_mode.is_set()) { write_attribute("filterMode", props.filter_mode.get()); } - if (props.enable_format_condition_calculation_.is_set()) + if (props.enable_format_condition_calculation.is_set()) { - write_attribute("enableFormatConditionsCalculation", props.enable_format_condition_calculation_.get()); + write_attribute("enableFormatConditionsCalculation", props.enable_format_condition_calculation.get()); } write_start_element(xmlns, "outlinePr"); @@ -2407,7 +2398,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (props.width.is_set()) { double width = (props.width.get() * 7 + 5) / 7; - write_attribute("width", number_to_string(width)); + write_attribute("width", serialize_number_to_string(width)); } if (props.best_fit) @@ -2505,7 +2496,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (props.height.is_set()) { auto height = props.height.get(); - write_attribute("ht", number_to_string(height)); + write_attribute("ht", serialize_number_to_string(height)); } if (props.hidden) @@ -2628,7 +2619,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) case cell::type::number: write_start_element(xmlns, "v"); - write_characters(number_to_string(cell.value())); + write_characters(serialize_number_to_string(cell.value())); write_end_element(xmlns, "v"); break; diff --git a/source/styles/font.cpp b/source/styles/font.cpp index 35c278ed..b474ab07 100644 --- a/source/styles/font.cpp +++ b/source/styles/font.cpp @@ -26,6 +26,11 @@ #include +namespace { +const std::string Default_Name = "Calibri"; +constexpr double Default_Size = 12.0; +} // namespace + namespace xlnt { font::font() @@ -138,7 +143,11 @@ font &font::size(double size) double font::size() const { - return size_.get(); + if (size_.is_set()) + { + return size_.get(); + } + return Default_Size; } bool font::has_name() const @@ -152,13 +161,13 @@ font &font::name(const std::string &name) return *this; } -std::string font::name() const +const std::string& font::name() const { if (name_.is_set()) { return name_.get(); } - return "Calibri"; + return Default_Name; } bool font::has_color() const @@ -220,7 +229,7 @@ std::size_t font::family() const return family_.get(); } -std::string font::scheme() const +const std::string& font::scheme() const { return scheme_.get(); } @@ -243,8 +252,8 @@ bool font::operator==(const font &other) const if (has_color() != other.has_color()) return false; if (has_color() && color() != other.color()) return false; // charset - if (has_charset()!= other.has_charset()) return false; - if (has_charset() && charset()!= other.charset()) return false; + if (has_charset() != other.has_charset()) return false; + if (has_charset() && charset() != other.charset()) return false; // modifiers if (bold() != other.bold()) return false; if (italic() != other.italic()) return false; diff --git a/source/worksheet/phonetic_pr.cpp b/source/worksheet/phonetic_pr.cpp index c86f58d3..f9e6b49f 100644 --- a/source/worksheet/phonetic_pr.cpp +++ b/source/worksheet/phonetic_pr.cpp @@ -31,8 +31,8 @@ const std::array Types{ "Hiragana", "noConversion"}; -// Order of elements defined by phonetic_pr::Alignment enum -const std::array Alignments{ +// Order of elements defined by phonetic_pr::alignment enum +const std::array alignments{ "Center", "Distributed", "Left", @@ -46,7 +46,7 @@ namespace xlnt { /// const std::string phonetic_pr::Serialised_ID = "phoneticPr"; -phonetic_pr::phonetic_pr(Font_ID font) +phonetic_pr::phonetic_pr(font_id_t font) : font_id_(font) { } @@ -65,12 +65,12 @@ void phonetic_pr::serialise(std::ostream &output_stream) const output_stream << "/>"; } -phonetic_pr::Font_ID phonetic_pr::font_id() const +phonetic_pr::font_id_t phonetic_pr::font_id() const { return font_id_; } -void phonetic_pr::font_id(Font_ID font) +void phonetic_pr::font_id(font_id_t font) { font_id_ = font; } @@ -80,12 +80,12 @@ bool phonetic_pr::has_type() const return type_.is_set(); } -phonetic_pr::Type phonetic_pr::type() const +phonetic_pr::phonetic_type phonetic_pr::type() const { return type_.get(); } -void phonetic_pr::type(Type type) +void phonetic_pr::type(phonetic_type type) { type_.set(type); } @@ -95,49 +95,49 @@ bool phonetic_pr::has_alignment() const return alignment_.is_set(); } -phonetic_pr::Alignment phonetic_pr::alignment() const +phonetic_pr::align phonetic_pr::alignment() const { return alignment_.get(); } -void phonetic_pr::alignment(Alignment align) +void phonetic_pr::alignment(align align) { alignment_.set(align); } // serialisation -const std::string &phonetic_pr::type_as_string(phonetic_pr::Type type) +const std::string &phonetic_pr::type_as_string(phonetic_pr::phonetic_type type) { return Types[static_cast(type)]; } -phonetic_pr::Type phonetic_pr::type_from_string(const std::string &str) +phonetic_pr::phonetic_type phonetic_pr::type_from_string(const std::string &str) { for (std::size_t i = 0; i < Types.size(); ++i) { if (str == Types[i]) { - return static_cast(i); + return static_cast(i); } } - return Type::No_Conversion; + return phonetic_type::no_conversion; } -const std::string &phonetic_pr::alignment_as_string(Alignment type) +const std::string &phonetic_pr::alignment_as_string(align type) { - return Alignments[static_cast(type)]; + return alignments[static_cast(type)]; } -phonetic_pr::Alignment phonetic_pr::alignment_from_string(const std::string &str) +phonetic_pr::align phonetic_pr::alignment_from_string(const std::string &str) { - for (std::size_t i = 0; i < Alignments.size(); ++i) + for (std::size_t i = 0; i < alignments.size(); ++i) { - if (str == Alignments[i]) + if (str == alignments[i]) { - return static_cast(i); + return static_cast(i); } } - return Alignment::No_Control; + return align::no_control; } } // namespace xlnt \ No newline at end of file