From 9c05e04f70af8487bb101ca9c0b94100867680ff Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Mon, 2 Nov 2015 16:45:05 -0500 Subject: [PATCH] fix -Wall -Wextra -pedantic warnings --- configure | 4 +- include/xlnt/common/hash_combine.hpp | 2 +- include/xlnt/styles/color.hpp | 34 +-- include/xlnt/styles/fill.hpp | 8 +- include/xlnt/styles/font.hpp | 24 +- include/xlnt/styles/number_format.hpp | 30 +- include/xlnt/styles/side.hpp | 2 +- include/xlnt/workbook/manifest.hpp | 2 +- include/xlnt/worksheet/column_properties.hpp | 2 +- include/xlnt/worksheet/row_properties.hpp | 2 +- source/cell/cell.cpp | 287 ++++++++++-------- source/common/datetime.cpp | 18 +- source/detail/constants.cpp | 72 +++-- source/detail/constants.hpp | 36 +-- source/serialization/excel_serializer.cpp | 45 +-- source/serialization/manifest_serializer.cpp | 6 +- .../serialization/relationship_serializer.cpp | 10 +- source/serialization/style_serializer.cpp | 3 +- source/serialization/theme_serializer.cpp | 2 +- source/serialization/worksheet_serializer.cpp | 4 +- source/serialization/xml_document.cpp | 2 +- source/styles/number_format.cpp | 274 +++++++++-------- source/styles/style.cpp | 11 +- source/workbook/workbook.cpp | 23 +- source/worksheet/worksheet.cpp | 5 +- 25 files changed, 509 insertions(+), 399 deletions(-) diff --git a/configure b/configure index 57dd30e6..d0ce9c7a 100755 --- a/configure +++ b/configure @@ -28,7 +28,9 @@ if not os.path.isdir('./build'): generator = 'Unix Makefiles' -if sys.platform == 'darwin': +if len(sys.argv) > 1: + generator = sys.argv[1] +elif sys.platform == 'darwin': generator = 'Unix Makefiles' elif sys.platform == 'win32': generator = 'Visual Studio 14 2015' diff --git a/include/xlnt/common/hash_combine.hpp b/include/xlnt/common/hash_combine.hpp index 7c672e56..97e77ec0 100644 --- a/include/xlnt/common/hash_combine.hpp +++ b/include/xlnt/common/hash_combine.hpp @@ -12,4 +12,4 @@ inline void hash_combine(std::size_t &seed, const T &v) seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } -} // namespace xlnt \ No newline at end of file +} // namespace xlnt diff --git a/include/xlnt/styles/color.hpp b/include/xlnt/styles/color.hpp index 4606e510..879278d7 100644 --- a/include/xlnt/styles/color.hpp +++ b/include/xlnt/styles/color.hpp @@ -40,16 +40,16 @@ class color rgb }; - static const color black; - static const color white; - static const color red; - static const color darkred; - static const color blue; - static const color darkblue; - static const color green; - static const color darkgreen; - static const color yellow; - static const color darkyellow; + static const color black(); + static const color white(); + static const color red(); + static const color darkred(); + static const color blue(); + static const color darkblue(); + static const color green(); + static const color darkgreen(); + static const color yellow(); + static const color darkyellow(); color() { @@ -63,19 +63,19 @@ class color { } - void set_auto(int auto_index) + void set_auto(std::size_t auto_index) { type_ = type::auto_; index_ = auto_index; } - void set_index(int index) + void set_index(std::size_t index) { type_ = type::indexed; index_ = index; } - void set_theme(int theme) + void set_theme(std::size_t theme) { type_ = type::theme; index_ = theme; @@ -86,7 +86,7 @@ class color return type_; } - int get_auto() const + std::size_t get_auto() const { if (type_ != type::auto_) { @@ -96,7 +96,7 @@ class color return index_; } - int get_index() const + std::size_t get_index() const { if (type_ != type::indexed) { @@ -106,7 +106,7 @@ class color return index_; } - int get_theme() const + std::size_t get_theme() const { if (type_ != type::theme) { @@ -149,7 +149,7 @@ class color private: type type_ = type::indexed; - int index_ = 0; + std::size_t index_ = 0; std::string rgb_string_; }; diff --git a/include/xlnt/styles/fill.hpp b/include/xlnt/styles/fill.hpp index bd8804ef..1e61cadf 100644 --- a/include/xlnt/styles/fill.hpp +++ b/include/xlnt/styles/fill.hpp @@ -321,13 +321,13 @@ class fill gradient_type gradient_type_; double rotation_ = 0; bool foreground_color_assigned_ = false; - color foreground_color_ = color::black; + color foreground_color_ = color::black(); bool background_color_assigned_ = false; - color background_color_ = color::white; + color background_color_ = color::white(); bool start_color_assigned_ = false; - color start_color_ = color::white; + color start_color_ = color::white(); bool end_color_assigned_ = false; - color end_color_ = color::black; + color end_color_ = color::black(); double gradient_path_left_ = 0; double gradient_path_right_ = 0; double gradient_path_top_ = 0; diff --git a/include/xlnt/styles/font.hpp b/include/xlnt/styles/font.hpp index b4f154fc..21b35c0b 100644 --- a/include/xlnt/styles/font.hpp +++ b/include/xlnt/styles/font.hpp @@ -48,6 +48,7 @@ class font { bold_ = bold; } + bool is_bold() const { return bold_; @@ -57,6 +58,7 @@ class font { italic_ = italic; } + bool is_italic() const { return italic_; @@ -66,6 +68,7 @@ class font { strikethrough_ = strikethrough; } + bool is_strikethrough() const { return strikethrough_; @@ -75,20 +78,23 @@ class font { underline_ = new_underline; } + bool is_underline() const { return underline_ != underline_style::none; } + underline_style get_underline() const { return underline_; } - void set_size(int size) + void set_size(std::size_t size) { size_ = size; } - int get_size() const + + std::size_t get_size() const { return size_; } @@ -106,11 +112,13 @@ class font { color_ = c; } - void set_family(int family) + + void set_family(std::size_t family) { has_family_ = true; family_ = family; } + void set_scheme(const std::string &scheme) { has_scheme_ = true; @@ -126,7 +134,8 @@ class font { return has_family_; } - int get_family() const + + std::size_t get_family() const { return family_; } @@ -143,10 +152,12 @@ class font seed = seed << 1 & superscript_; seed = seed << 1 & subscript_; seed = seed << 1 & strikethrough_; + hash_combine(seed, name_); hash_combine(seed, size_); hash_combine(seed, static_cast(underline_)); hash_combine(seed, static_cast(color_.get_type())); + switch (color_.get_type()) { case color::type::indexed: @@ -158,6 +169,7 @@ class font default: break; } + hash_combine(seed, family_); hash_combine(seed, scheme_); @@ -173,7 +185,7 @@ class font friend class style; std::string name_ = "Calibri"; - int size_ = 11; + std::size_t size_ = 11; bool bold_ = false; bool italic_ = false; bool superscript_ = false; @@ -182,7 +194,7 @@ class font bool strikethrough_ = false; color color_; bool has_family_ = false; - int family_; + std::size_t family_; bool has_scheme_ = false; std::string scheme_; }; diff --git a/include/xlnt/styles/number_format.hpp b/include/xlnt/styles/number_format.hpp index f342816e..c0ebc30e 100644 --- a/include/xlnt/styles/number_format.hpp +++ b/include/xlnt/styles/number_format.hpp @@ -67,21 +67,36 @@ class number_format static const number_format currency_usd(); static const number_format currency_eur_simple(); - static number_format from_builtin_id(int builtin_id); + static number_format from_builtin_id(std::size_t builtin_id); number_format(); - number_format(int builtin_id); - number_format(const std::string &code, int custom_id = -1); + number_format(std::size_t builtin_id); + number_format(const std::string &code); + number_format(const std::string &code, std::size_t custom_id); - void set_format_string(const std::string &format_code, int id = -1); + void set_format_string(const std::string &format_code); + void set_format_string(const std::string &format_code, std::size_t custom_id); + std::string get_format_string() const; - void set_id(int id) + bool has_id() const + { + return id_set_; + } + + void set_id(std::size_t id) { id_ = id; + id_set_ = true; } - int get_id() const + + std::size_t get_id() const { + if(!id_set_) + { + throw std::runtime_error("number format doesn't have an id"); + } + return id_; } @@ -93,7 +108,8 @@ class number_format } private: - int id_; + bool id_set_; + std::size_t id_; std::string format_string_; }; diff --git a/include/xlnt/styles/side.hpp b/include/xlnt/styles/side.hpp index fd536d39..673f8322 100644 --- a/include/xlnt/styles/side.hpp +++ b/include/xlnt/styles/side.hpp @@ -114,7 +114,7 @@ class side bool style_assigned_ = false; border_style style_ = border_style::none; bool color_assigned_ = false; - color color_ = color::black; + color color_ = color::black(); }; } // namespace xlnt diff --git a/include/xlnt/workbook/manifest.hpp b/include/xlnt/workbook/manifest.hpp index 73a2cf0d..7e0b55f0 100644 --- a/include/xlnt/workbook/manifest.hpp +++ b/include/xlnt/workbook/manifest.hpp @@ -75,4 +75,4 @@ class manifest std::vector override_types_; }; -} // namespace xlnt \ No newline at end of file +} // namespace xlnt diff --git a/include/xlnt/worksheet/column_properties.hpp b/include/xlnt/worksheet/column_properties.hpp index 09c411bb..d71a528b 100644 --- a/include/xlnt/worksheet/column_properties.hpp +++ b/include/xlnt/worksheet/column_properties.hpp @@ -28,7 +28,7 @@ namespace xlnt { class column_properties { public: - double width; + long double width; std::size_t style; bool custom; }; diff --git a/include/xlnt/worksheet/row_properties.hpp b/include/xlnt/worksheet/row_properties.hpp index 5d91d6a0..8c7fd9ab 100644 --- a/include/xlnt/worksheet/row_properties.hpp +++ b/include/xlnt/worksheet/row_properties.hpp @@ -28,7 +28,7 @@ namespace xlnt { class row_properties { public: - double height; + long double height; bool visible; int outline_level; bool collapsed; diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index d56df2e2..2dd75e31 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -125,10 +125,18 @@ bool is_date_format(const std::string &format_string) bool is_valid_color(const std::string &color) { - static const std::vector colors = { "Black", - "Green" - "White", - "Blue", "Magenta", "Yellow", "Cyan", "Red" }; + static const std::vector *colors = + new std::vector( + { + "Black", + "Green" + "White", + "Blue", + "Magenta", + "Yellow", + "Cyan", + "Red" + }); auto compare_color = [&](const std::string &other) { if (color.size() != other.size()) return false; @@ -144,7 +152,7 @@ bool is_valid_color(const std::string &color) return true; }; - return std::find_if(colors.begin(), colors.end(), compare_color) != colors.end(); + return std::find_if(colors->begin(), colors->end(), compare_color) != colors->end(); } bool parse_condition(const std::string &string, section &s) @@ -200,111 +208,120 @@ bool is_hex(char c) return false; } -const std::unordered_map known_locales = { { 0x401, "Arabic - Saudi Arabia" }, - { 0x402, "Bulgarian" }, - { 0x403, "Catalan" }, - { 0x404, "Chinese - Taiwan" }, - { 0x405, "Czech" }, - { 0x406, "Danish" }, - { 0x407, "German - Germany" }, - { 0x408, "Greek" }, - { 0x409, "English - United States" }, - { 0x410, "Italian - Italy" }, - { 0x411, "Japanese" }, - { 0x412, "Korean" }, - { 0x413, "Dutch - Netherlands" }, - { 0x414, "Norwegian - Bokml" }, - { 0x415, "Polish" }, - { 0x416, "Portuguese - Brazil" }, - { 0x417, "Raeto-Romance" }, - { 0x418, "Romanian - Romania" }, - { 0x419, "Russian" }, - { 0x420, "Urdu" }, - { 0x421, "Indonesian" }, - { 0x422, "Ukrainian" }, - { 0x423, "Belarusian" }, - { 0x424, "Slovenian" }, - { 0x425, "Estonian" }, - { 0x426, "Latvian" }, - { 0x427, "Lithuanian" }, - { 0x428, "Tajik" }, - { 0x429, "Farsi - Persian" }, - { 0x430, "Sesotho (Sutu)" }, - { 0x431, "Tsonga" }, - { 0x432, "Setsuana" }, - { 0x433, "Venda" }, - { 0x434, "Xhosa" }, - { 0x435, "Zulu" }, - { 0x436, "Afrikaans" }, - { 0x437, "Georgian" }, - { 0x438, "Faroese" }, - { 0x439, "Hindi" }, - { 0x440, "Kyrgyz - Cyrillic" }, - { 0x441, "Swahili" }, - { 0x442, "Turkmen" }, - { 0x443, "Uzbek - Latin" }, - { 0x444, "Tatar" }, - { 0x445, "Bengali - India" }, - { 0x446, "Punjabi" }, - { 0x447, "Gujarati" }, - { 0x448, "Oriya" }, - { 0x449, "Tamil" }, - { 0x450, "Mongolian" }, - { 0x451, "Tibetan" }, - { 0x452, "Welsh" }, - { 0x453, "Khmer" }, - { 0x454, "Lao" }, - { 0x455, "Burmese" }, - { 0x456, "Galician" }, - { 0x457, "Konkani" }, - { 0x458, "Manipuri" }, - { 0x459, "Sindhi" }, - { 0x460, "Kashmiri" }, - { 0x461, "Nepali" }, - { 0x462, "Frisian - Netherlands" }, - { 0x464, "Filipino" }, - { 0x465, "Divehi; Dhivehi; Maldivian" }, - { 0x466, "Edo" }, - { 0x470, "Igbo - Nigeria" }, - { 0x474, "Guarani - Paraguay" }, - { 0x476, "Latin" }, - { 0x477, "Somali" }, - { 0x481, "Maori" }, - { 0x801, "Arabic - Iraq" }, - { 0x804, "Chinese - China" }, - { 0x807, "German - Switzerland" }, - { 0x809, "English - Great Britain" }, - { 0x810, "Italian - Switzerland" }, - { 0x813, "Dutch - Belgium" }, - { 0x814, "Norwegian - Nynorsk" }, - { 0x816, "Portuguese - Portugal" }, - { 0x818, "Romanian - Moldova" }, - { 0x819, "Russian - Moldova" }, - { 0x843, "Uzbek - Cyrillic" }, - { 0x845, "Bengali - Bangladesh" }, - { 0x850, "Mongolian" }, - { 0x1001, "Arabic - Libya" }, - { 0x1004, "Chinese - Singapore" }, - { 0x1007, "German - Luxembourg" }, - { 0x1009, "English - Canada" }, - { 0x1401, "Arabic - Algeria" }, - { 0x1404, "Chinese - Macau SAR" }, - { 0x1407, "German - Liechtenstein" }, - { 0x1409, "English - New Zealand" }, - { 0x1801, "Arabic - Morocco" }, - { 0x1809, "English - Ireland" }, - { 0x2001, "Arabic - Oman" }, - { 0x2009, "English - Jamaica" }, - { 0x2401, "Arabic - Yemen" }, - { 0x2409, "English - Caribbean" }, - { 0x2801, "Arabic - Syria" }, - { 0x2809, "English - Belize" }, - { 0x3001, "Arabic - Lebanon" }, - { 0x3009, "English - Zimbabwe" }, - { 0x3401, "Arabic - Kuwait" }, - { 0x3409, "English - Phillippines" }, - { 0x3801, "Arabic - United Arab Emirates" }, - { 0x4001, "Arabic - Qatar" } }; +const std::unordered_map known_locales() +{ + const std::unordered_map *all = + new std::unordered_map( + { + { 0x401, "Arabic - Saudi Arabia" }, + { 0x402, "Bulgarian" }, + { 0x403, "Catalan" }, + { 0x404, "Chinese - Taiwan" }, + { 0x405, "Czech" }, + { 0x406, "Danish" }, + { 0x407, "German - Germany" }, + { 0x408, "Greek" }, + { 0x409, "English - United States" }, + { 0x410, "Italian - Italy" }, + { 0x411, "Japanese" }, + { 0x412, "Korean" }, + { 0x413, "Dutch - Netherlands" }, + { 0x414, "Norwegian - Bokml" }, + { 0x415, "Polish" }, + { 0x416, "Portuguese - Brazil" }, + { 0x417, "Raeto-Romance" }, + { 0x418, "Romanian - Romania" }, + { 0x419, "Russian" }, + { 0x420, "Urdu" }, + { 0x421, "Indonesian" }, + { 0x422, "Ukrainian" }, + { 0x423, "Belarusian" }, + { 0x424, "Slovenian" }, + { 0x425, "Estonian" }, + { 0x426, "Latvian" }, + { 0x427, "Lithuanian" }, + { 0x428, "Tajik" }, + { 0x429, "Farsi - Persian" }, + { 0x430, "Sesotho (Sutu)" }, + { 0x431, "Tsonga" }, + { 0x432, "Setsuana" }, + { 0x433, "Venda" }, + { 0x434, "Xhosa" }, + { 0x435, "Zulu" }, + { 0x436, "Afrikaans" }, + { 0x437, "Georgian" }, + { 0x438, "Faroese" }, + { 0x439, "Hindi" }, + { 0x440, "Kyrgyz - Cyrillic" }, + { 0x441, "Swahili" }, + { 0x442, "Turkmen" }, + { 0x443, "Uzbek - Latin" }, + { 0x444, "Tatar" }, + { 0x445, "Bengali - India" }, + { 0x446, "Punjabi" }, + { 0x447, "Gujarati" }, + { 0x448, "Oriya" }, + { 0x449, "Tamil" }, + { 0x450, "Mongolian" }, + { 0x451, "Tibetan" }, + { 0x452, "Welsh" }, + { 0x453, "Khmer" }, + { 0x454, "Lao" }, + { 0x455, "Burmese" }, + { 0x456, "Galician" }, + { 0x457, "Konkani" }, + { 0x458, "Manipuri" }, + { 0x459, "Sindhi" }, + { 0x460, "Kashmiri" }, + { 0x461, "Nepali" }, + { 0x462, "Frisian - Netherlands" }, + { 0x464, "Filipino" }, + { 0x465, "Divehi; Dhivehi; Maldivian" }, + { 0x466, "Edo" }, + { 0x470, "Igbo - Nigeria" }, + { 0x474, "Guarani - Paraguay" }, + { 0x476, "Latin" }, + { 0x477, "Somali" }, + { 0x481, "Maori" }, + { 0x801, "Arabic - Iraq" }, + { 0x804, "Chinese - China" }, + { 0x807, "German - Switzerland" }, + { 0x809, "English - Great Britain" }, + { 0x810, "Italian - Switzerland" }, + { 0x813, "Dutch - Belgium" }, + { 0x814, "Norwegian - Nynorsk" }, + { 0x816, "Portuguese - Portugal" }, + { 0x818, "Romanian - Moldova" }, + { 0x819, "Russian - Moldova" }, + { 0x843, "Uzbek - Cyrillic" }, + { 0x845, "Bengali - Bangladesh" }, + { 0x850, "Mongolian" }, + { 0x1001, "Arabic - Libya" }, + { 0x1004, "Chinese - Singapore" }, + { 0x1007, "German - Luxembourg" }, + { 0x1009, "English - Canada" }, + { 0x1401, "Arabic - Algeria" }, + { 0x1404, "Chinese - Macau SAR" }, + { 0x1407, "German - Liechtenstein" }, + { 0x1409, "English - New Zealand" }, + { 0x1801, "Arabic - Morocco" }, + { 0x1809, "English - Ireland" }, + { 0x2001, "Arabic - Oman" }, + { 0x2009, "English - Jamaica" }, + { 0x2401, "Arabic - Yemen" }, + { 0x2409, "English - Caribbean" }, + { 0x2801, "Arabic - Syria" }, + { 0x2809, "English - Belize" }, + { 0x3001, "Arabic - Lebanon" }, + { 0x3009, "English - Zimbabwe" }, + { 0x3401, "Arabic - Kuwait" }, + { 0x3409, "English - Phillippines" }, + { 0x3801, "Arabic - United Arab Emirates" }, + { 0x4001, "Arabic - Qatar" } + }); + + return *all; +} bool is_valid_locale(const std::string &locale_string) { @@ -317,7 +334,7 @@ bool is_valid_locale(const std::string &locale_string) for (auto c : country) { - if (!is_hex(std::toupper(c))) + if (!is_hex(static_cast(std::toupper(c)))) { return false; } @@ -325,7 +342,9 @@ bool is_valid_locale(const std::string &locale_string) auto index = std::stoi(country, 0, 16); - if (known_locales.find(index) == known_locales.end()) + auto known_locales_ = known_locales(); + + if (known_locales_.find(index) == known_locales_.end()) { return false; } @@ -359,7 +378,7 @@ section parse_section(const std::string §ion_string) bool in_quotes = false; bool in_brackets = false; - static const std::vector bracket_times = { "h", "hh", "m", "mm", "s", "ss" }; + const std::vector bracket_times = { "h", "hh", "m", "mm", "s", "ss" }; for (std::size_t i = 0; i < section_string.size(); i++) { @@ -502,9 +521,6 @@ format_sections parse_format_sections(const std::string &combined) return result; } -const std::vector MonthNames = { "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December" }; - std::string format_section(long double number, const section &format, xlnt::calendar base_date) { const std::string unquoted = "$+(:^'{<=-/)!&~}> "; @@ -516,10 +532,12 @@ std::string format_section(long double number, const section &format, xlnt::cale const std::string date_unquoted = ",-/: "; const std::vector dates = { "m", "mm", "mmm", "mmmmm", "mmmmmm", "d", "dd", "ddd", "dddd", "yy", - "yyyy" - "h", - "[h]", "hh", "m", "[m]", "mm", "s", "[s]", "ss", "AM/PM", "am/pm", - "A/P", "a/p" }; + "yyyy", "h", "[h]", "hh", "m", "[m]", "mm", "s", "[s]", "ss", "AM/PM", + "am/pm", "A/P", "a/p" }; + + const std::vector MonthNames = { "January", "February", "March", + "April", "May", "June", "July", "August", "September", "October", "November", "December" }; + auto split = split_string_any(format.value, date_unquoted); std::string::size_type index = 0, prev = 0; @@ -530,7 +548,7 @@ std::string format_section(long double number, const section &format, xlnt::cale std::string lower; lower.resize(s.size()); for (std::size_t i = 0; i < s.size(); i++) - lower[i] = std::tolower(s[i]); + lower[i] = static_cast(std::tolower(s[i])); return lower; }; @@ -563,12 +581,12 @@ std::string format_section(long double number, const section &format, xlnt::cale } else if (part == "mmm" && !processed_month) { - result.append(MonthNames.at(d.month - 1).substr(0, 3)); + result.append(MonthNames.at(static_cast(d.month - 1)).substr(0, 3)); processed_month = true; } else if (part == "mmmm" && !processed_month) { - result.append(MonthNames.at(d.month - 1)); + result.append(MonthNames.at(static_cast(d.month - 1))); processed_month = true; } else if (part == "d") @@ -765,11 +783,12 @@ namespace xlnt { const std::unordered_map &cell::error_codes() { - static const std::unordered_map codes = { { "#NULL!", 0 }, { "#DIV/0!", 1 }, { "#VALUE!", 2 }, + static const std::unordered_map *codes = + new std::unordered_map({ { "#NULL!", 0 }, { "#DIV/0!", 1 }, { "#VALUE!", 2 }, { "#REF!", 3 }, { "#NAME?", 4 }, { "#NUM!", 5 }, - { "#N/A!", 6 } }; + { "#N/A!", 6 } }); - return codes; + return *codes; }; cell::cell() : d_(nullptr) @@ -1177,13 +1196,17 @@ comment cell::get_comment() return comment(d_->comment_.get()); } +//TODO: this shares a lot of code with worksheet::get_point_pos, try to reduce repition std::pair cell::get_anchor() const { static const double DefaultColumnWidth = 51.85; static const double DefaultRowHeight = 15.0; - auto points_to_pixels = [](double value, double dpi) { return (int)std::ceil(value * dpi / 72); }; - + auto points_to_pixels = [](long double value, long double dpi) + { + return static_cast(std::ceil(value * dpi / 72)); + }; + auto left_columns = d_->column_ - 1; int left_anchor = 0; auto default_width = points_to_pixels(DefaultColumnWidth, 96.0); @@ -1208,7 +1231,7 @@ std::pair cell::get_anchor() const int top_anchor = 0; auto default_height = points_to_pixels(DefaultRowHeight, 96.0); - for (int row_index = 1; row_index <= (int)top_rows; row_index++) + for (row_t row_index = 1; row_index <= top_rows; row_index++) { if (get_parent().has_row_properties(row_index)) { @@ -1239,16 +1262,10 @@ void cell::set_data_type(type t) const number_format &cell::get_number_format() const { - static const number_format default_format; - if (d_->has_style_) { return get_parent().get_parent().get_number_format(d_->style_id_); } - else if (get_parent().get_parent().get_number_formats().empty()) - { - return default_format; - } else { return get_parent().get_parent().get_number_formats().front(); diff --git a/source/common/datetime.cpp b/source/common/datetime.cpp index f8bed2a3..5d665776 100644 --- a/source/common/datetime.cpp +++ b/source/common/datetime.cpp @@ -119,7 +119,7 @@ long double time::to_number() const microseconds += static_cast(second * 1e6); microseconds += static_cast(minute * 1e6 * 60); auto microseconds_per_hour = static_cast(1e6) * 60 * 60; - microseconds += static_cast(hour * microseconds_per_hour); + microseconds += static_cast(hour) * microseconds_per_hour; auto number = microseconds / (24.0L * microseconds_per_hour); auto hundred_billion = static_cast(1e9) * 100; number = std::floor(number * hundred_billion + 0.5L) / hundred_billion; @@ -182,7 +182,21 @@ double timedelta::to_number() const timedelta timedelta::from_number(long double number) { - return timedelta(static_cast(number), 0); + int days = static_cast(number); + number -= days; + number *= 24; + int hours = static_cast(number); + number -= hours; + number *= 60; + int minutes = static_cast(number); + number -= minutes; + number *= 60; + int seconds = static_cast(number); + number -= seconds; + number *= 1000000; + int microseconds = static_cast(number + 0.5); + + return timedelta(days, hours, minutes, seconds, microseconds); } } // namespace xlnt diff --git a/source/detail/constants.cpp b/source/detail/constants.cpp index 3e0337c8..c5a61b2b 100644 --- a/source/detail/constants.cpp +++ b/source/detail/constants.cpp @@ -13,36 +13,48 @@ const column_t constants::MaxColumn = LimitStyle == limit_style::excel ? (1u << 14) : LimitStyle == limit_style::openpyxl ? 18278 : UINT32_MAX; // constants -const std::string constants::PackageProps = "docProps"; -const std::string constants::PackageXl = "xl"; -const std::string constants::PackageRels = "_rels"; -const std::string constants::PackageTheme = PackageXl + "/" + "theme"; -const std::string constants::PackageWorksheets = PackageXl + "/" + "worksheets"; -const std::string constants::PackageDrawings = PackageXl + "/" + "drawings"; -const std::string constants::PackageCharts = PackageXl + "/" + "charts"; +const std::string constants::PackageProps() { return "docProps"; } +const std::string constants::PackageXl() { return "xl"; } +const std::string constants::PackageRels() { return "_rels"; } +const std::string constants::PackageTheme() { return PackageXl() + "/" + "theme"; } +const std::string constants::PackageWorksheets() { return PackageXl() + "/" + "worksheets"; } +const std::string constants::PackageDrawings() { return PackageXl() + "/" + "drawings"; } +const std::string constants::PackageCharts() { return PackageXl() + "/" + "charts"; } -const std::string constants::ArcContentTypes = "[Content_Types].xml"; -const std::string constants::ArcRootRels = PackageRels + "/.rels"; -const std::string constants::ArcWorkbookRels = PackageXl + "/" + PackageRels + "/workbook.xml.rels"; -const std::string constants::ArcCore = PackageProps + "/core.xml"; -const std::string constants::ArcApp = PackageProps + "/app.xml"; -const std::string constants::ArcWorkbook = PackageXl + "/workbook.xml"; -const std::string constants::ArcStyles = PackageXl + "/styles.xml"; -const std::string constants::ArcTheme = PackageTheme + "/theme1.xml"; -const std::string constants::ArcSharedString = PackageXl + "/sharedStrings.xml"; +const std::string constants::ArcContentTypes() { return "[Content_Types].xml"; } +const std::string constants::ArcRootRels() { return PackageRels() + "/.rels"; } +const std::string constants::ArcWorkbookRels() { return PackageXl() + "/" + PackageRels() + "/workbook.xml.rels"; } +const std::string constants::ArcCore() { return PackageProps() + "/core.xml"; } +const std::string constants::ArcApp() { return PackageProps() + "/app.xml"; } +const std::string constants::ArcWorkbook() { return PackageXl() + "/workbook.xml"; } +const std::string constants::ArcStyles() { return PackageXl() + "/styles.xml"; } +const std::string constants::ArcTheme() { return PackageTheme() + "/theme1.xml"; } +const std::string constants::ArcSharedString() { return PackageXl() + "/sharedStrings.xml"; } + +const std::unordered_map constants::Namespaces() +{ + const std::unordered_map namespaces = + { + { "spreadsheetml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main" }, + { "content-types", "http://schemas.openxmlformats.org/package/2006/content-types" }, + { "relationships", "http://schemas.openxmlformats.org/package/2006/relationships" }, + { "drawingml", "http://schemas.openxmlformats.org/drawingml/2006/main" }, + { "r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" }, + { "cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties" }, + { "dc", "http://purl.org/dc/elements/1.1/" }, + { "dcterms", "http://purl.org/dc/terms/" }, + { "dcmitype", "http://purl.org/dc/dcmitype/" }, + { "xsi", "http://www.w3.org/2001/XMLSchema-instance" }, + { "vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" }, + { "xml", "http://www.w3.org/XML/1998/namespace" } + }; + + return namespaces; +} + +const std::string constants::Namespace(const std::string &id) +{ + return Namespaces().find(id)->second; +} -const std::unordered_map constants::Namespaces = { - { "spreadsheetml", "http://schemas.openxmlformats.org/spreadsheetml/2006/main" }, - { "content-types", "http://schemas.openxmlformats.org/package/2006/content-types" }, - { "relationships", "http://schemas.openxmlformats.org/package/2006/relationships" }, - { "drawingml", "http://schemas.openxmlformats.org/drawingml/2006/main" }, - { "r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" }, - { "cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties" }, - { "dc", "http://purl.org/dc/elements/1.1/" }, - { "dcterms", "http://purl.org/dc/terms/" }, - { "dcmitype", "http://purl.org/dc/dcmitype/" }, - { "xsi", "http://www.w3.org/2001/XMLSchema-instance" }, - { "vt", "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" }, - { "xml", "http://www.w3.org/XML/1998/namespace" } -}; } diff --git a/source/detail/constants.hpp b/source/detail/constants.hpp index 86c7ae5d..d828b80b 100644 --- a/source/detail/constants.hpp +++ b/source/detail/constants.hpp @@ -15,25 +15,27 @@ struct constants static const column_t MaxColumn; // constants - static const std::string PackageProps; - static const std::string PackageXl; - static const std::string PackageRels; - static const std::string PackageTheme; - static const std::string PackageWorksheets; - static const std::string PackageDrawings; - static const std::string PackageCharts; + static const std::string PackageProps(); + static const std::string PackageXl(); + static const std::string PackageRels(); + static const std::string PackageTheme(); + static const std::string PackageWorksheets(); + static const std::string PackageDrawings(); + static const std::string PackageCharts(); - static const std::string ArcContentTypes; - static const std::string ArcRootRels; - static const std::string ArcWorkbookRels; - static const std::string ArcCore; - static const std::string ArcApp; - static const std::string ArcWorkbook; - static const std::string ArcStyles; - static const std::string ArcTheme; - static const std::string ArcSharedString; + static const std::string ArcContentTypes(); + static const std::string ArcRootRels(); + static const std::string ArcWorkbookRels(); + static const std::string ArcCore(); + static const std::string ArcApp(); + static const std::string ArcWorkbook(); + static const std::string ArcStyles(); + static const std::string ArcTheme(); + static const std::string ArcSharedString(); - static const std::unordered_map Namespaces; + static const std::unordered_map Namespaces(); + + static const std::string Namespace(const std::string &id); }; } // namespace xlnt diff --git a/source/serialization/excel_serializer.cpp b/source/serialization/excel_serializer.cpp index f73fb04f..26298832 100644 --- a/source/serialization/excel_serializer.cpp +++ b/source/serialization/excel_serializer.cpp @@ -44,14 +44,14 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl wb.set_guess_types(guess_types); wb.set_data_only(data_only); - if(!archive.has_file(xlnt::constants::ArcContentTypes)) + if(!archive.has_file(xlnt::constants::ArcContentTypes())) { throw xlnt::invalid_file_exception("missing [Content Types].xml"); } xlnt::manifest_serializer ms(wb.get_manifest()); xlnt::xml_document manifest_xml; - manifest_xml.from_string(archive.read(xlnt::constants::ArcContentTypes)); + manifest_xml.from_string(archive.read(xlnt::constants::ArcContentTypes())); ms.read_manifest(manifest_xml); if (ms.determine_document_type() != "excel") @@ -61,16 +61,16 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl wb.clear(); - if(archive.has_file(xlnt::constants::ArcCore)) + if(archive.has_file(xlnt::constants::ArcCore())) { xlnt::workbook_serializer workbook_serializer_(wb); xlnt::xml_document core_properties_xml; - core_properties_xml.from_string(archive.read(xlnt::constants::ArcCore)); + core_properties_xml.from_string(archive.read(xlnt::constants::ArcCore())); workbook_serializer_.read_properties_core(core_properties_xml); } auto workbook_relationships = - xlnt::relationship_serializer::read_relationships(archive, xlnt::constants::ArcWorkbook); + xlnt::relationship_serializer::read_relationships(archive, xlnt::constants::ArcWorkbook()); for (const auto &relationship : workbook_relationships) { @@ -78,7 +78,7 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl } xlnt::xml_document xml; - xml.from_string(archive.read(xlnt::constants::ArcWorkbook)); + xml.from_string(archive.read(xlnt::constants::ArcWorkbook())); auto root_node = xml.get_child("workbook"); @@ -88,12 +88,12 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl ? xlnt::calendar::mac_1904 : xlnt::calendar::windows_1900; - if(archive.has_file(xlnt::constants::ArcSharedString)) + if(archive.has_file(xlnt::constants::ArcSharedString())) { xlnt::shared_strings_serializer shared_strings_serializer_; std::vector shared_strings; xlnt::xml_document shared_strings_xml; - shared_strings_xml.from_string(archive.read(xlnt::constants::ArcSharedString)); + shared_strings_xml.from_string(archive.read(xlnt::constants::ArcSharedString())); shared_strings_serializer_.read_shared_strings(shared_strings_xml, shared_strings); for (auto shared_string : shared_strings) @@ -104,7 +104,7 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl xlnt::style_serializer style_reader_(wb); xlnt::xml_document style_xml; - style_xml.from_string(archive.read(xlnt::constants::ArcStyles)); + style_xml.from_string(archive.read(xlnt::constants::ArcStyles())); style_reader_.read_stylesheet(style_xml); auto sheets_node = root_node.get_child("sheets"); @@ -113,12 +113,16 @@ bool load_workbook(xlnt::zip_file &archive, bool guess_types, bool data_only, xl { auto rel = wb.get_relationship(sheet_node.get_attribute("r:id")); auto ws = wb.create_sheet(sheet_node.get_attribute("name"), rel); + //TODO: this is really bad auto ws_filename = (rel.get_target_uri().substr(0, 3) != "xl/" ? "xl/" : "") + rel.get_target_uri(); auto sheet_type = wb.get_manifest().get_override_type(ws_filename); - - if(sheet_type != "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml") continue; + + if(rel.get_type() != xlnt::relationship::type::worksheet) + { + continue; + } xlnt::worksheet_serializer worksheet_serializer(ws); xlnt::xml_document worksheet_xml; @@ -154,9 +158,10 @@ bool excel_serializer::load_stream_workbook(std::istream &stream, bool guess_typ { std::vector bytes; + //TODO: inefficient? while (stream.good()) { - bytes.push_back(stream.get()); + bytes.push_back(static_cast(stream.get())); } return load_virtual_workbook(bytes, guess_types, data_only); @@ -192,28 +197,28 @@ void excel_serializer::write_data(bool /*as_template*/) relationship_serializer relationship_serializer_; relationship_serializer_.write_relationships(workbook_.get_root_relationships(), "", archive_); - relationship_serializer_.write_relationships(workbook_.get_relationships(), constants::ArcWorkbook, archive_); + relationship_serializer_.write_relationships(workbook_.get_relationships(), constants::ArcWorkbook(), archive_); xml_document properties_app_xml; workbook_serializer workbook_serializer_(workbook_); - archive_.writestr(constants::ArcApp, xml_serializer::serialize(workbook_serializer_.write_properties_app())); - archive_.writestr(constants::ArcCore, xml_serializer::serialize(workbook_serializer_.write_properties_core())); + archive_.writestr(constants::ArcApp(), xml_serializer::serialize(workbook_serializer_.write_properties_app())); + archive_.writestr(constants::ArcCore(), xml_serializer::serialize(workbook_serializer_.write_properties_core())); theme_serializer theme_serializer_; - archive_.writestr(constants::ArcTheme, theme_serializer_.write_theme(workbook_.get_loaded_theme()).to_string()); + archive_.writestr(constants::ArcTheme(), theme_serializer_.write_theme(workbook_.get_loaded_theme()).to_string()); xlnt::shared_strings_serializer shared_strings_serializer_; archive_.writestr( - constants::ArcSharedString, + constants::ArcSharedString(), xml_serializer::serialize(shared_strings_serializer_.write_shared_strings(workbook_.get_shared_strings()))); - archive_.writestr(constants::ArcWorkbook, xml_serializer::serialize(workbook_serializer_.write_workbook())); + archive_.writestr(constants::ArcWorkbook(), xml_serializer::serialize(workbook_serializer_.write_workbook())); style_serializer style_serializer_(workbook_); - archive_.writestr(constants::ArcStyles, style_serializer_.write_stylesheet().to_string()); + archive_.writestr(constants::ArcStyles(), style_serializer_.write_stylesheet().to_string()); manifest_serializer manifest_serializer_(workbook_.get_manifest()); - archive_.writestr(constants::ArcContentTypes, manifest_serializer_.write_manifest().to_string()); + archive_.writestr(constants::ArcContentTypes(), manifest_serializer_.write_manifest().to_string()); write_worksheets(); } diff --git a/source/serialization/manifest_serializer.cpp b/source/serialization/manifest_serializer.cpp index 493a56b4..b7a15164 100644 --- a/source/serialization/manifest_serializer.cpp +++ b/source/serialization/manifest_serializer.cpp @@ -33,7 +33,7 @@ xml_document manifest_serializer::write_manifest() const xml_document xml; auto root_node = xml.add_child("Types"); - xml.add_namespace("", constants::Namespaces.at("content-types")); + xml.add_namespace("", constants::Namespace("content-types")); for (const auto default_type : manifest_.get_default_types()) { @@ -54,12 +54,12 @@ xml_document manifest_serializer::write_manifest() const std::string manifest_serializer::determine_document_type() const { - if (!manifest_.has_override_type(constants::ArcWorkbook)) + if (!manifest_.has_override_type(constants::ArcWorkbook())) { return "unsupported"; } - std::string type = manifest_.get_override_type(constants::ArcWorkbook); + std::string type = manifest_.get_override_type(constants::ArcWorkbook()); if (type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml") { diff --git a/source/serialization/relationship_serializer.cpp b/source/serialization/relationship_serializer.cpp index 7d809116..d419b8cf 100644 --- a/source/serialization/relationship_serializer.cpp +++ b/source/serialization/relationship_serializer.cpp @@ -46,9 +46,9 @@ std::vector relationship_serializer::read_relationships(zip_file & std::string id = relationship_node.get_attribute("Id"); std::string type = relationship_node.get_attribute("Type"); - std::string target = relationship_node.get_attribute("Target"); + std::string rel_target = relationship_node.get_attribute("Target"); - relationships.push_back(xlnt::relationship(type, id, target)); + relationships.push_back(xlnt::relationship(type, id, rel_target)); } return relationships; @@ -61,16 +61,14 @@ bool relationship_serializer::write_relationships(const std::vectordoc.child(child_name.c_str()))); } -} // namespace xlnt \ No newline at end of file +} // namespace xlnt diff --git a/source/styles/number_format.cpp b/source/styles/number_format.cpp index e33d5df4..08894106 100644 --- a/source/styles/number_format.cpp +++ b/source/styles/number_format.cpp @@ -6,61 +6,63 @@ namespace { -const std::unordered_map &builtin_formats() +const std::unordered_map &builtin_formats() { - static const std::unordered_map formats = { - { 0, "General" }, - { 1, "0" }, - { 2, "0.00" }, - { 3, "#,##0" }, - { 4, "#,##0.00" }, - { 5, "\"$\"#,##0_);(\"$\"#,##0)" }, - { 6, "\"$\"#,##0_);[Red](\"$\"#,##0)" }, - { 7, "\"$\"#,##0.00_);(\"$\"#,##0.00)" }, - { 8, "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)" }, - { 9, "0%" }, - { 10, "0.00%" }, - { 11, "0.00E+00" }, - { 12, "# ?/?" }, - { 13, "# \?\?/??" }, // escape trigraph - { 14, "mm-dd-yy" }, - { 15, "d-mmm-yy" }, - { 16, "d-mmm" }, - { 17, "mmm-yy" }, - { 18, "h:mm AM/PM" }, - { 19, "h:mm:ss AM/PM" }, - { 20, "h:mm" }, - { 21, "h:mm:ss" }, - { 22, "m/d/yy h:mm" }, + static const std::unordered_map *formats = + new std::unordered_map + ({ + { 0, "General" }, + { 1, "0" }, + { 2, "0.00" }, + { 3, "#,##0" }, + { 4, "#,##0.00" }, + { 5, "\"$\"#,##0_);(\"$\"#,##0)" }, + { 6, "\"$\"#,##0_);[Red](\"$\"#,##0)" }, + { 7, "\"$\"#,##0.00_);(\"$\"#,##0.00)" }, + { 8, "\"$\"#,##0.00_);[Red](\"$\"#,##0.00)" }, + { 9, "0%" }, + { 10, "0.00%" }, + { 11, "0.00E+00" }, + { 12, "# ?/?" }, + { 13, "# \?\?/??" }, // escape trigraph + { 14, "mm-dd-yy" }, + { 15, "d-mmm-yy" }, + { 16, "d-mmm" }, + { 17, "mmm-yy" }, + { 18, "h:mm AM/PM" }, + { 19, "h:mm:ss AM/PM" }, + { 20, "h:mm" }, + { 21, "h:mm:ss" }, + { 22, "m/d/yy h:mm" }, - { 37, "#,##0_);(#,##0)" }, - { 38, "#,##0_);[Red](#,##0)" }, - { 39, "#,##0.00_);(#,##0.00)" }, - { 40, "#,##0.00_);[Red](#,##0.00)" }, + { 37, "#,##0_);(#,##0)" }, + { 38, "#,##0_);[Red](#,##0)" }, + { 39, "#,##0.00_);(#,##0.00)" }, + { 40, "#,##0.00_);[Red](#,##0.00)" }, - { 41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)" }, - { 42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)" }, - { 43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)" }, + { 41, "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)" }, + { 42, "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)" }, + { 43, "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)" }, - { 44, "_(\"$\"* #,##0.00_)_(\"$\"* \\(#,##0.00\\)_(\"$\"* \"-\"??_)_(@_)" }, - { 45, "mm:ss" }, - { 46, "[h]:mm:ss" }, - { 47, "mmss.0" }, - { 48, "##0.0E+0" }, - { 49, "@" } + { 44, "_(\"$\"* #,##0.00_)_(\"$\"* \\(#,##0.00\\)_(\"$\"* \"-\"??_)_(@_)" }, + { 45, "mm:ss" }, + { 46, "[h]:mm:ss" }, + { 47, "mmss.0" }, + { 48, "##0.0E+0" }, + { 49, "@" } - // EXCEL differs from the standard in the following: - //{14, "m/d/yyyy"}, - //{22, "m/d/yyyy h:mm"}, - //{37, "#,##0_);(#,##0)"}, - //{38, "#,##0_);[Red]"}, - //{39, "#,##0.00_);(#,##0.00)"}, - //{40, "#,##0.00_);[Red]"}, - //{47, "mm:ss.0"}, - //{55, "yyyy/mm/dd"} - }; + // EXCEL differs from the standard in the following: + //{14, "m/d/yyyy"}, + //{22, "m/d/yyyy h:mm"}, + //{37, "#,##0_);(#,##0)"}, + //{38, "#,##0_);[Red]"}, + //{39, "#,##0.00_);(#,##0.00)"}, + //{40, "#,##0.00_);[Red]"}, + //{47, "mm:ss.0"}, + //{55, "yyyy/mm/dd"} + }); - return formats; + return *formats; } } // namespace @@ -69,222 +71,227 @@ namespace xlnt { const number_format number_format::general() { - static const number_format format(builtin_formats().at(0), 0); - return format; + static const number_format *format = new number_format(builtin_formats().at(0), 0); + return *format; } const number_format number_format::text() { - static const number_format format(builtin_formats().at(49), 49); - return format; + static const number_format *format = new number_format(builtin_formats().at(49), 49); + return *format; } const number_format number_format::number() { - static const number_format format(builtin_formats().at(1), 1); - return format; + static const number_format *format = new number_format(builtin_formats().at(1), 1); + return *format; } const number_format number_format::number_00() { - static const number_format format(builtin_formats().at(2), 2); - return format; + static const number_format *format = new number_format(builtin_formats().at(2), 2); + return *format; } const number_format number_format::number_comma_separated1() { - static const number_format format(builtin_formats().at(4), 4); - return format; + static const number_format *format = new number_format(builtin_formats().at(4), 4); + return *format; } const number_format number_format::number_comma_separated2() { - static const number_format format("#,##0.00_-"); - return format; + static const number_format *format = new number_format("#,##0.00_-"); + return *format; } const number_format number_format::percentage() { - static const number_format format(builtin_formats().at(9), 9); - return format; + static const number_format *format = new number_format(builtin_formats().at(9), 9); + return *format; } const number_format number_format::percentage_00() { - static const number_format format(builtin_formats().at(10), 10); - return format; + static const number_format *format = new number_format(builtin_formats().at(10), 10); + return *format; } const number_format number_format::date_yyyymmdd2() { - static const number_format format("yyyy-mm-dd"); - return format; + static const number_format *format = new number_format("yyyy-mm-dd"); + return *format; } const number_format number_format::date_yyyymmdd() { - static const number_format format("yy-mm-dd"); - return format; + static const number_format *format = new number_format("yy-mm-dd"); + return *format; } const number_format number_format::date_ddmmyyyy() { - static const number_format format("dd/mm/yy"); - return format; + static const number_format *format = new number_format("dd/mm/yy"); + return *format; } const number_format number_format::date_dmyslash() { - static const number_format format("d/m/y"); - return format; + static const number_format *format = new number_format("d/m/y"); + return *format; } const number_format number_format::date_dmyminus() { - static const number_format format("d-m-y"); - return format; + static const number_format *format = new number_format("d-m-y"); + return *format; } const number_format number_format::date_dmminus() { - static const number_format format("d-m"); - return format; + static const number_format *format = new number_format("d-m"); + return *format; } const number_format number_format::date_myminus() { - static const number_format format("m-y"); - return format; + static const number_format *format = new number_format("m-y"); + return *format; } const number_format number_format::date_xlsx14() { - static const number_format format(builtin_formats().at(14), 14); - return format; + static const number_format *format = new number_format(builtin_formats().at(14), 14); + return *format; } const number_format number_format::date_xlsx15() { - static const number_format format(builtin_formats().at(15), 15); - return format; + static const number_format *format = new number_format(builtin_formats().at(15), 15); + return *format; } const number_format number_format::date_xlsx16() { - static const number_format format(builtin_formats().at(16), 16); - return format; + static const number_format *format = new number_format(builtin_formats().at(16), 16); + return *format; } const number_format number_format::date_xlsx17() { - static const number_format format(builtin_formats().at(17), 17); - return format; + static const number_format *format = new number_format(builtin_formats().at(17), 17); + return *format; } const number_format number_format::date_xlsx22() { - static const number_format format(builtin_formats().at(22), 22); - return format; + static const number_format *format = new number_format(builtin_formats().at(22), 22); + return *format; } const number_format number_format::date_datetime() { - static const number_format format("yyyy-mm-dd h:mm:ss"); - return format; + static const number_format *format = new number_format("yyyy-mm-dd h:mm:ss"); + return *format; } const number_format number_format::date_time1() { - static const number_format format(builtin_formats().at(18), 18); - return format; + static const number_format *format = new number_format(builtin_formats().at(18), 18); + return *format; } const number_format number_format::date_time2() { - static const number_format format(builtin_formats().at(19), 19); - return format; + static const number_format *format = new number_format(builtin_formats().at(19), 19); + return *format; } const number_format number_format::date_time3() { - static const number_format format(builtin_formats().at(20), 20); - return format; + static const number_format *format = new number_format(builtin_formats().at(20), 20); + return *format; } const number_format number_format::date_time4() { - static const number_format format(builtin_formats().at(21), 21); - return format; + static const number_format *format = new number_format(builtin_formats().at(21), 21); + return *format; } const number_format number_format::date_time5() { - static const number_format format(builtin_formats().at(45), 45); - return format; + static const number_format *format = new number_format(builtin_formats().at(45), 45); + return *format; } const number_format number_format::date_time6() { - static const number_format format(builtin_formats().at(21), 21); - return format; + static const number_format *format = new number_format(builtin_formats().at(21), 21); + return *format; } const number_format number_format::date_time7() { - static const number_format format("i:s.S"); - return format; + static const number_format *format = new number_format("i:s.S"); + return *format; } const number_format number_format::date_time8() { - static const number_format format("h:mm:ss@"); - return format; + static const number_format *format = new number_format("h:mm:ss@"); + return *format; } const number_format number_format::date_timedelta() { - static const number_format format("[hh]:mm:ss"); - return format; + static const number_format *format = new number_format("[hh]:mm:ss"); + return *format; } const number_format number_format::date_yyyymmddslash() { - static const number_format format("yy/mm/dd@"); - return format; + static const number_format *format = new number_format("yy/mm/dd@"); + return *format; } const number_format number_format::currency_usd_simple() { - static const number_format format("\"$\"#,##0.00_-"); - return format; + static const number_format *format = new number_format("\"$\"#,##0.00_-"); + return *format; } const number_format number_format::currency_usd() { - static const number_format format("$#,##0_-"); - return format; + static const number_format *format = new number_format("$#,##0_-"); + return *format; } const number_format number_format::currency_eur_simple() { - static const number_format format("[$EUR ]#,##0.00_-"); - return format; + static const number_format *format = new number_format("[$EUR ]#,##0.00_-"); + return *format; } number_format::number_format() : number_format(general()) { } -number_format::number_format(int id) : number_format(from_builtin_id(id)) +number_format::number_format(std::size_t id) : number_format(from_builtin_id(id)) { } -number_format::number_format(const std::string &format_string, int id) +number_format::number_format(const std::string &format_string) : id_set_(false), id_(0) +{ + set_format_string(format_string); +} + +number_format::number_format(const std::string &format_string, std::size_t id) : id_set_(false), id_(0) { set_format_string(format_string, id); } -number_format number_format::from_builtin_id(int builtin_id) +number_format number_format::from_builtin_id(std::size_t builtin_id) { if (builtin_formats().find(builtin_id) == builtin_formats().end()) { @@ -302,28 +309,35 @@ std::string number_format::get_format_string() const std::size_t number_format::hash() const { - std::size_t seed = static_cast(id_); + std::size_t seed = id_; hash_combine(seed, format_string_); return seed; } -void number_format::set_format_string(const std::string &format_string, int id) + +void number_format::set_format_string(const std::string &format_string) { format_string_ = format_string; - id_ = id; + id_ = 0; + id_set_ = false; - if (id_ == -1) + for (const auto &pair : builtin_formats()) { - for (const auto &pair : builtin_formats()) + if (pair.second == format_string) { - if (pair.second == format_string) - { - id_ = pair.first; - break; - } + id_ = pair.first; + id_set_ = true; + break; } } } +void number_format::set_format_string(const std::string &format_string, std::size_t id) +{ + format_string_ = format_string; + id_ = id; + id_set_ = true; +} + } // namespace xlnt diff --git a/source/styles/style.cpp b/source/styles/style.cpp index 9fc06341..1d81c7e6 100644 --- a/source/styles/style.cpp +++ b/source/styles/style.cpp @@ -16,8 +16,15 @@ void hash_combine(std::size_t &seed, const T &v) namespace xlnt { -const color color::black = color(color::type::indexed, 0); -const color color::white = color(color::type::indexed, 0); +const color color::black() +{ + return color(color::type::rgb, "ff000000"); +} + +const color color::white() +{ + return color(color::type::rgb, "ffffffff"); +} style::style() : id_(0), diff --git a/source/workbook/workbook.cpp b/source/workbook/workbook.cpp index 40e20f6f..b39c2093 100644 --- a/source/workbook/workbook.cpp +++ b/source/workbook/workbook.cpp @@ -47,6 +47,8 @@ workbook::workbook() : d_(new detail::workbook_impl()) create_relationship("rId3", "styles.xml", relationship::type::styles); create_relationship("rId4", "theme/theme1.xml", relationship::type::theme); + add_number_format(number_format::general()); + d_->manifest_.add_default_type("rels", "application/vnd.openxmlformats-package.relationships+xml"); d_->manifest_.add_default_type("xml", "application/xml"); @@ -572,9 +574,16 @@ void workbook::add_font(const font &font_) void workbook::add_number_format(const number_format &number_format_) { - d_->number_formats_.push_back(number_format_); + if (d_->number_formats_.size() == 1 && d_->number_formats_.front() == number_format::general()) + { + d_->number_formats_.front() = number_format_; + } + else + { + d_->number_formats_.push_back(number_format_); + } - if(number_format_.get_id() == -1) + if(!number_format_.has_id()) { d_->number_formats_.back().set_id(d_->next_custom_format_id_++); } @@ -623,13 +632,13 @@ const number_format &workbook::get_number_format(std::size_t style_id) const for (const auto &number_format_ : d_->number_formats_) { - if (static_cast(number_format_.get_id()) == number_format_id) + if (number_format_.has_id() && number_format_.get_id() == number_format_id) { return number_format_; } } - auto nf = number_format::from_builtin_id(static_cast(number_format_id)); + auto nf = number_format::from_builtin_id(number_format_id); d_->number_formats_.push_back(nf); return d_->number_formats_.back(); @@ -652,7 +661,7 @@ std::size_t workbook::set_font(const font &font_, std::size_t style_id) } else { - font_index = match - d_->fonts_.begin(); + font_index = static_cast(match - d_->fonts_.begin()); } auto existing_style = d_->styles_[style_id]; @@ -670,7 +679,7 @@ std::size_t workbook::set_font(const font &font_, std::size_t style_id) if (style_match != d_->styles_.end()) { - return style_match - d_->styles_.begin(); + return static_cast(style_match - d_->styles_.begin()); } d_->styles_.push_back(new_style); @@ -737,7 +746,7 @@ std::size_t workbook::set_number_format(const xlnt::number_format &format, std:: { d_->number_formats_.push_back(format); - if (format.get_id() == -1) + if (!format.has_id()) { d_->number_formats_.back().set_id(d_->next_custom_format_id_++); } diff --git a/source/worksheet/worksheet.cpp b/source/worksheet/worksheet.cpp index 8e5fcd34..8fba4e68 100644 --- a/source/worksheet/worksheet.cpp +++ b/source/worksheet/worksheet.cpp @@ -641,7 +641,10 @@ cell_reference worksheet::get_point_pos(int left, int top) const static const double DefaultColumnWidth = 51.85; static const double DefaultRowHeight = 15.0; - auto points_to_pixels = [](double value, double dpi) { return (int)std::ceil(value * dpi / 72); }; + auto points_to_pixels = [](long double value, long double dpi) + { + return static_cast(std::ceil(value * dpi / 72)); + }; auto default_height = points_to_pixels(DefaultRowHeight, 96.0); auto default_width = points_to_pixels(DefaultColumnWidth, 96.0);