From 6aaf380c26ab9956a64957012c41877b77f7d96b Mon Sep 17 00:00:00 2001 From: Thomas Fussell Date: Sat, 7 Nov 2015 20:00:54 -0500 Subject: [PATCH] fix windows build for dll and static lib --- cmake/xlnt.cmake | 9 +- cmake/xlnt.test.cmake | 4 + include/xlnt/cell/cell.hpp | 19 ++-- include/xlnt/packaging/relationship.hpp | 27 +++--- include/xlnt/utils/string.hpp | 10 +-- include/xlnt/workbook/workbook.hpp | 4 +- include/xlnt/worksheet/cell_vector.hpp | 2 +- include/xlnt/worksheet/range.hpp | 2 +- include/xlnt/xlnt_config.hpp | 4 +- source/cell/cell.cpp | 115 +++++++++++++++--------- source/packaging/relationship.cpp | 4 +- source/utils/string.cpp | 42 ++++----- 12 files changed, 140 insertions(+), 102 deletions(-) diff --git a/cmake/xlnt.cmake b/cmake/xlnt.cmake index 46532695..d1f8b0f9 100644 --- a/cmake/xlnt.cmake +++ b/cmake/xlnt.cmake @@ -60,6 +60,11 @@ SET(PUGIXML ../third-party/pugixml/src/pugixml.hpp ../third-party/pugixml/src/pu if(SHARED) add_library(xlnt SHARED ${HEADERS} ${SOURCES} ${MINIZ} ${PUGIXML}) + target_compile_definitions(xlnt PRIVATE XLNT_EXPORT=1) + add_definitions(-DXLNT_SHARED) + if(MSVC) + set_target_properties(xlnt PROPERTIES COMPILE_FLAGS "/wd\"4251\"") + endif() else() add_library(xlnt STATIC ${HEADERS} ${SOURCES} ${MINIZ} ${PUGIXML}) endif() @@ -106,7 +111,7 @@ configure_file( IMMEDIATE @ONLY) -INSTALL(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../include/xlnt +install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/../include/xlnt DESTINATION include PATTERN ".DS_Store" EXCLUDE ) @@ -116,7 +121,7 @@ install(TARGETS xlnt ARCHIVE DESTINATION ${LIB_DEST_DIR} ) -INSTALL(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc" +install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION ${LIB_DEST_DIR}/pkgconfig ) diff --git a/cmake/xlnt.test.cmake b/cmake/xlnt.test.cmake index df04fe87..a69f12de 100644 --- a/cmake/xlnt.test.cmake +++ b/cmake/xlnt.test.cmake @@ -45,6 +45,10 @@ source_group(runner FILES ${RUNNER}) target_link_libraries(xlnt.test xlnt) +if(MSVC) + set_target_properties(xlnt.test PROPERTIES COMPILE_FLAGS "/wd\"4251\"") +endif() + # Needed for PathFileExists in path_helper (test helper) if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC") target_link_libraries(xlnt.test Shlwapi) diff --git a/include/xlnt/cell/cell.hpp b/include/xlnt/cell/cell.hpp index fddd1ab7..f0f92c5d 100644 --- a/include/xlnt/cell/cell.hpp +++ b/include/xlnt/cell/cell.hpp @@ -381,13 +381,19 @@ public: /// /// Return true if this cell is uninitialized. /// - friend bool operator==(std::nullptr_t, const cell &cell); + friend XLNT_FUNCTION bool operator==(std::nullptr_t, const cell &cell); /// /// Return the result of left.get_reference() < right.get_reference(). /// What's the point of this? /// - friend bool operator<(cell left, cell right); + friend XLNT_FUNCTION bool operator<(const cell left, const cell right); + + /// + /// Convenience function for writing cell to an ostream. + /// Uses cell::to_string() internally. + /// + friend XLNT_FUNCTION std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell); private: // make these friends so they can use the private constructor @@ -406,13 +412,4 @@ private: detail::cell_impl *d_; }; -/// -/// Convenience function for writing cell to an ostream. -/// Uses cell::to_string() internally. -/// -inline std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) -{ - return stream << std::string(cell.to_string().data()); -} - } // namespace xlnt diff --git a/include/xlnt/packaging/relationship.hpp b/include/xlnt/packaging/relationship.hpp index 9d2768fa..7e04621a 100644 --- a/include/xlnt/packaging/relationship.hpp +++ b/include/xlnt/packaging/relationship.hpp @@ -46,10 +46,10 @@ enum class target_mode /// Represents an association between a source Package or part, and a target object which can be a part or external /// resource. /// -class relationship +class XLNT_CLASS relationship { public: - enum class type + enum class XLNT_CLASS type { invalid, hyperlink, @@ -140,17 +140,19 @@ class relationship } } - relationship(); - relationship(const string &t, const string &r_id = "", const string &target_uri = "") + relationship(); + + relationship(const string &t, const string &r_id = "", const string &target_uri = "") : relationship(type_from_string(t), r_id, target_uri) { } - relationship(type t, const string &r_id = "", const string &target_uri = ""); + + relationship(type t, const string &r_id = "", const string &target_uri = ""); /// /// gets a string that identifies the relationship. /// - string get_id() const + string get_id() const { return id_; } @@ -158,7 +160,7 @@ class relationship /// /// gets the URI of the package or part that owns the relationship. /// - string get_source_uri() const + string get_source_uri() const { return source_uri_; } @@ -166,7 +168,7 @@ class relationship /// /// gets a value that indicates whether the target of the relationship is or External to the Package. /// - target_mode get_target_mode() const + target_mode get_target_mode() const { return target_mode_; } @@ -174,21 +176,22 @@ class relationship /// /// gets the URI of the target resource of the relationship. /// - string get_target_uri() const + string get_target_uri() const { return target_uri_; } - type get_type() const + type get_type() const { return type_; } - string get_type_string() const + + string get_type_string() const { return type_to_string(type_); } - friend bool operator==(const relationship &left, const relationship &right) + friend XLNT_FUNCTION bool operator==(const relationship &left, const relationship &right) { return left.type_ == right.type_ && left.id_ == right.id_ diff --git a/include/xlnt/utils/string.hpp b/include/xlnt/utils/string.hpp index 644b86cf..7fcddeee 100644 --- a/include/xlnt/utils/string.hpp +++ b/include/xlnt/utils/string.hpp @@ -278,12 +278,12 @@ public: bool operator<(const string &other) const; - friend void swap(string &left, string &right); - friend std::ostream &operator<<(std::ostream &left, string &right); - friend string operator+(const char *left, const string &right); + friend XLNT_FUNCTION void swap(string &left, string &right); + friend XLNT_FUNCTION std::ostream & operator<<(std::ostream &left, string &right); + friend XLNT_FUNCTION string operator+(const char *left, const string &right); - friend bool operator==(const char *left, const string &right) { return right == left; } - friend bool operator!=(const char *left, const string &right) { return right != left; } + friend XLNT_FUNCTION bool operator==(const char *left, const string &right) { return right == left; } + friend XLNT_FUNCTION bool operator!=(const char *left, const string &right) { return right != left; } private: explicit string(size_type initial_size); diff --git a/include/xlnt/workbook/workbook.hpp b/include/xlnt/workbook/workbook.hpp index b88e3f50..45c4cc1d 100644 --- a/include/xlnt/workbook/workbook.hpp +++ b/include/xlnt/workbook/workbook.hpp @@ -65,7 +65,7 @@ namespace detail { struct workbook_impl; } // namespace detail class XLNT_CLASS workbook { public: - class iterator + class XLNT_CLASS iterator { public: iterator(workbook &wb, std::size_t index); @@ -85,7 +85,7 @@ class XLNT_CLASS workbook std::size_t index_; }; - class const_iterator + class XLNT_CLASS const_iterator { public: const_iterator(const workbook &wb, std::size_t index); diff --git a/include/xlnt/worksheet/cell_vector.hpp b/include/xlnt/worksheet/cell_vector.hpp index 1a44f251..c7eb183e 100644 --- a/include/xlnt/worksheet/cell_vector.hpp +++ b/include/xlnt/worksheet/cell_vector.hpp @@ -18,7 +18,7 @@ class XLNT_CLASS cell_vector { public: template - class common_iterator : public std::iterator + class XLNT_CLASS common_iterator : public std::iterator { public: common_iterator(worksheet ws, const cell_reference &start_cell, major_order order = major_order::row) diff --git a/include/xlnt/worksheet/range.hpp b/include/xlnt/worksheet/range.hpp index 217147f3..ff47041c 100644 --- a/include/xlnt/worksheet/range.hpp +++ b/include/xlnt/worksheet/range.hpp @@ -40,7 +40,7 @@ class XLNT_CLASS range { public: template - class common_iterator : public std::iterator + class XLNT_CLASS common_iterator : public std::iterator { public: common_iterator(worksheet ws, const range_reference &start_cell, major_order order = major_order::row) diff --git a/include/xlnt/xlnt_config.hpp b/include/xlnt/xlnt_config.hpp index 0916fcb7..e910f48a 100644 --- a/include/xlnt/xlnt_config.hpp +++ b/include/xlnt/xlnt_config.hpp @@ -22,8 +22,6 @@ // @author: see AUTHORS file #pragma once -#include - // Change these values for programs using this library. namespace xlnt { @@ -64,6 +62,8 @@ const limit_style LimitStyle = limit_style::openpyxl; #else #define XLNT_API __declspec(dllimport) #endif // XLNT_EXPORT +#else +#define XLNT_API #endif // XLNT_SHARED #else #define XLNT_API diff --git a/source/cell/cell.cpp b/source/cell/cell.cpp index 4651772c..4a610c41 100644 --- a/source/cell/cell.cpp +++ b/source/cell/cell.cpp @@ -810,116 +810,117 @@ bool cell::garbage_collectible() const } template <> -void cell::set_value(bool b) +XLNT_FUNCTION void cell::set_value(bool b) { d_->value_numeric_ = b ? 1 : 0; d_->type_ = type::boolean; } template <> -void cell::set_value(std::int8_t i) +XLNT_FUNCTION void cell::set_value(std::int8_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::int16_t i) +XLNT_FUNCTION void cell::set_value(std::int16_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::int32_t i) +XLNT_FUNCTION void cell::set_value(std::int32_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::int64_t i) +XLNT_FUNCTION void cell::set_value(std::int64_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::uint8_t i) +XLNT_FUNCTION void cell::set_value(std::uint8_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::uint16_t i) +XLNT_FUNCTION void cell::set_value(std::uint16_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::uint32_t i) +XLNT_FUNCTION void cell::set_value(std::uint32_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(std::uint64_t i) +XLNT_FUNCTION void cell::set_value(std::uint64_t i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } -#ifdef _WIN32 +#ifdef _MSC_VER template <> -void cell::set_value(unsigned long i) +XLNT_FUNCTION void cell::set_value(unsigned long i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } -#endif +#endif // _MSC_VER +//TODO: base this on 64-bit model (i.e. LLP64/LP64) rather than system/compiler #ifdef __linux template <> -void cell::set_value(long long i) +XLNT_FUNCTION void cell::set_value(long long i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } template <> -void cell::set_value(unsigned long long i) +XLNT_FUNCTION void cell::set_value(unsigned long long i) { d_->value_numeric_ = static_cast(i); d_->type_ = type::numeric; } -#endif +#endif // __linux template <> -void cell::set_value(float f) +XLNT_FUNCTION void cell::set_value(float f) { d_->value_numeric_ = static_cast(f); d_->type_ = type::numeric; } template <> -void cell::set_value(double d) +XLNT_FUNCTION void cell::set_value(double d) { d_->value_numeric_ = static_cast(d); d_->type_ = type::numeric; } template <> -void cell::set_value(long double d) +XLNT_FUNCTION void cell::set_value(long double d) { d_->value_numeric_ = static_cast(d); d_->type_ = type::numeric; } template <> -void XLNT_FUNCTION cell::set_value(string s) +XLNT_FUNCTION void cell::set_value(string s) { d_->set_string(s, get_parent().get_parent().get_guess_types()); @@ -930,13 +931,13 @@ void XLNT_FUNCTION cell::set_value(string s) } template <> -void XLNT_FUNCTION cell::set_value(char const *c) +XLNT_FUNCTION void cell::set_value(char const *c) { set_value(string(c)); } template <> -void cell::set_value(cell c) +XLNT_FUNCTION void cell::set_value(cell c) { d_->type_ = c.d_->type_; d_->value_numeric_ = c.d_->value_numeric_; @@ -949,7 +950,7 @@ void cell::set_value(cell c) } template <> -void cell::set_value(date d) +XLNT_FUNCTION void cell::set_value(date d) { d_->type_ = type::numeric; d_->value_numeric_ = d.to_number(get_base_date()); @@ -957,7 +958,7 @@ void cell::set_value(date d) } template <> -void cell::set_value(datetime d) +XLNT_FUNCTION void cell::set_value(datetime d) { d_->type_ = type::numeric; d_->value_numeric_ = d.to_number(get_base_date()); @@ -965,7 +966,7 @@ void cell::set_value(datetime d) } template <> -void cell::set_value(time t) +XLNT_FUNCTION void cell::set_value(time t) { d_->type_ = type::numeric; d_->value_numeric_ = t.to_number(); @@ -973,13 +974,21 @@ void cell::set_value(time t) } template <> -void cell::set_value(timedelta t) +XLNT_FUNCTION void cell::set_value(timedelta t) { d_->type_ = type::numeric; d_->value_numeric_ = t.to_number(); set_number_format(number_format::date_timedelta()); } +#ifdef XLNT_STD_STRING +template<> +XLNT_FUNCTION void cell::set_value(std::string str) +{ + set_value(string(str.data())); +} +#endif + row_t cell::get_row() const { return d_->row_; @@ -1044,11 +1053,21 @@ cell &cell::operator=(const cell &rhs) return *this; } -bool operator<(cell left, cell right) +XLNT_FUNCTION bool operator==(std::nullptr_t, const cell &cell) +{ + return cell == nullptr; +} + +XLNT_FUNCTION bool operator<(const cell left, const cell right) { return left.get_reference() < right.get_reference(); } +XLNT_FUNCTION std::ostream &operator<<(std::ostream &stream, const xlnt::cell &cell) +{ + return stream << std::string(cell.to_string().data()); +} + string cell::to_repr() const { return "parent_).get_title() + "." + get_reference().to_string() + ">"; @@ -1309,105 +1328,105 @@ void cell::clear_value() } template <> -bool cell::get_value() const +XLNT_FUNCTION bool cell::get_value() const { return d_->value_numeric_ != 0; } template <> -std::int8_t cell::get_value() const +XLNT_FUNCTION std::int8_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::int16_t cell::get_value() const +XLNT_FUNCTION std::int16_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::int32_t cell::get_value() const +XLNT_FUNCTION std::int32_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::int64_t cell::get_value() const +XLNT_FUNCTION std::int64_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::uint8_t cell::get_value() const +XLNT_FUNCTION std::uint8_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::uint16_t cell::get_value() const +XLNT_FUNCTION std::uint16_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::uint32_t cell::get_value() const +XLNT_FUNCTION std::uint32_t cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -std::uint64_t cell::get_value() const +XLNT_FUNCTION std::uint64_t cell::get_value() const { return static_cast(d_->value_numeric_); } #ifdef __linux template <> -long long int cell::get_value() const +XLNT_FUNCTION long long int cell::get_value() const { return static_cast(d_->value_numeric_); } #endif template <> -float cell::get_value() const +XLNT_FUNCTION float cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -double cell::get_value() const +XLNT_FUNCTION double cell::get_value() const { return static_cast(d_->value_numeric_); } template <> -long double cell::get_value() const +XLNT_FUNCTION long double cell::get_value() const { return d_->value_numeric_; } template <> -time cell::get_value() const +XLNT_FUNCTION time cell::get_value() const { return time::from_number(d_->value_numeric_); } template <> -datetime cell::get_value() const +XLNT_FUNCTION datetime cell::get_value() const { return datetime::from_number(d_->value_numeric_, get_base_date()); } template <> -date cell::get_value() const +XLNT_FUNCTION date cell::get_value() const { return date::from_number(static_cast(d_->value_numeric_), get_base_date()); } template <> -timedelta cell::get_value() const +XLNT_FUNCTION timedelta cell::get_value() const { return timedelta::from_number(d_->value_numeric_); } @@ -1419,11 +1438,19 @@ void cell::set_number_format(const number_format &number_format_) } template <> -string cell::get_value() const +XLNT_FUNCTION string cell::get_value() const { return d_->value_string_; } +#ifdef XLNT_STD_STRING +template<> +XLNT_FUNCTION std::string cell::get_value() const +{ + return std::string(d_->value_string_.data()); +} +#endif + bool cell::has_value() const { return d_->type_ != cell::type::null; diff --git a/source/packaging/relationship.cpp b/source/packaging/relationship.cpp index 6bd07a5a..204ece19 100644 --- a/source/packaging/relationship.cpp +++ b/source/packaging/relationship.cpp @@ -2,7 +2,7 @@ namespace xlnt { -relationship::relationship(type t, const string &r_id, const string &target_uri) +XLNT_FUNCTION relationship::relationship(type t, const string &r_id, const string &target_uri) : type_(t), id_(r_id), source_uri_(""), target_uri_(target_uri), target_mode_(target_mode::internal) { if (t == type::hyperlink) @@ -11,7 +11,7 @@ relationship::relationship(type t, const string &r_id, const string &target_uri) } } -relationship::relationship() +XLNT_FUNCTION relationship::relationship() : type_(type::invalid), id_(""), source_uri_(""), target_uri_(""), target_mode_(target_mode::internal) { } diff --git a/source/utils/string.cpp b/source/utils/string.cpp index 8907ad74..5aee36ef 100644 --- a/source/utils/string.cpp +++ b/source/utils/string.cpp @@ -99,11 +99,13 @@ string string::from(std::uint64_t i) return string(std::to_string(i).c_str()); } +#ifndef _MSC_VER template<> string string::from(std::size_t i) { return string(std::to_string(i).c_str()); } +#endif template<> string string::from(float i) @@ -213,8 +215,8 @@ xlnt::string::const_iterator &xlnt::string::const_iterator::operator++() string::const_iterator &string::const_iterator::operator+=(int offset) { - auto new_index = index_ + offset; - new_index = std::max(0, std::min(parent_->length(), new_index)); + auto new_index = static_cast(index_) + offset; + new_index = std::max(0, std::min(static_cast(parent_->length()), new_index)); index_ = static_cast(new_index); return *this; @@ -224,8 +226,8 @@ xlnt::string::iterator xlnt::string::iterator::operator+(int offset) { iterator copy = *this; - auto new_index = index_ + offset; - new_index = std::max(0, std::min(parent_->length(), new_index)); + auto new_index = static_cast(index_) + offset; + new_index = std::max(0, std::min(static_cast(parent_->length()), new_index)); copy.index_ = static_cast(new_index); return copy; @@ -235,8 +237,8 @@ xlnt::string::const_iterator xlnt::string::const_iterator::operator+(int offset) { const_iterator copy = *this; - auto new_index = index_ + offset; - new_index = std::max(0, std::min(parent_->length(), new_index)); + auto new_index = static_cast(index_) + offset; + new_index = std::max(0, std::min(static_cast(parent_->length()), new_index)); copy.index_ = static_cast(new_index); return copy; @@ -378,10 +380,10 @@ string string::substr(size_type offset, size_type len) const { if(len != npos && offset + len < length()) { - return string(begin() + offset, begin() + (offset + len)); + return string(begin() + static_cast(offset), begin() + static_cast(offset + len)); } - return string(begin() + offset, end()); + return string(begin() + static_cast(offset), end()); } string::code_point string::back() const @@ -411,7 +413,7 @@ string::size_type string::find(const string &str) const string::size_type string::find(code_point c, size_type offset) const { - auto iter = begin() + offset; + auto iter = begin() + static_cast(offset); while (iter != end()) { @@ -464,7 +466,7 @@ string::size_type string::find_last_of(const string &str) const string::size_type string::find_last_of(code_point c, size_type offset) const { - auto stop = begin() + offset; + auto stop = begin() + static_cast(offset); auto iter = end() - 1; while (iter != stop) @@ -486,7 +488,7 @@ string::size_type string::find_last_of(char c, size_type offset) const string::size_type string::find_last_of(const string &str, size_type offset) const { - auto stop = begin() + offset; + auto stop = begin() + static_cast(offset); auto iter = end() - 1; while (iter != stop) @@ -509,7 +511,7 @@ string::size_type string::find_first_of(const string &str) const string::size_type string::find_first_of(const string &str, size_type offset) const { - auto iter = begin() + offset; + auto iter = begin() + static_cast(offset); while (iter != end()) { @@ -531,7 +533,7 @@ string::size_type string::find_first_not_of(const string &str) const string::size_type string::find_first_not_of(const string &str, size_type offset) const { - auto iter = begin() + offset; + auto iter = begin() + static_cast(offset); while (iter != end()) { @@ -553,7 +555,7 @@ string::size_type string::find_last_not_of(const string &str) const string::size_type string::find_last_not_of(const string &str, size_type offset) const { - auto stop = begin() + offset; + auto stop = begin() + static_cast(offset); auto iter = end() - 1; while (iter != stop) @@ -767,8 +769,8 @@ void string::replace(size_type index, utf32_char value) auto data_start = code_point_byte_offsets_->at(index); auto data_end = code_point_byte_offsets_->at(index + 1); - auto previous_len = data_end - data_start; - int difference = encoded_len - previous_len; + auto previous_len = static_cast(data_end - data_start); + int difference = static_cast(encoded_len) - previous_len; if(difference < 0) { @@ -866,7 +868,7 @@ string &string::operator+=(const string &rhs) return *this; } -void swap(string &left, string &right) +XLNT_FUNCTION void swap(string &left, string &right) { using std::swap; @@ -874,7 +876,7 @@ void swap(string &left, string &right) swap(left.code_point_byte_offsets_, right.code_point_byte_offsets_); } -std::ostream &operator<<(std::ostream &left, string &right) +XLNT_FUNCTION std::ostream &operator<<(std::ostream &left, string &right) { auto d = right.data(); std::size_t i = 0; @@ -888,12 +890,12 @@ std::ostream &operator<<(std::ostream &left, string &right) return left; } -string operator+(const char *left, const string &right) +XLNT_FUNCTION string operator+(const char *left, const string &right) { return string(left) + right; } -bool string::operator<(const string &other) const +XLNT_FUNCTION bool string::operator<(const string &other) const { return std::string(data()) < std::string(other.data()); }