Skip to content

Commit

Permalink
2024-12-20 added bool XLXmlFile::valid() addressing issues #312 and -…
Browse files Browse the repository at this point in the history
… en passant - #310
  • Loading branch information
aral-matrix committed Dec 20, 2024
1 parent 77f4370 commit 02f7d42
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 89 deletions.
3 changes: 2 additions & 1 deletion OpenXLSX/headers/XLContentTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ namespace OpenXLSX
*/
std::vector<XLContentItem> getContentItems();

// ---------- Protected Member Functions ---------- //
private: // ---------- Private Member Variables ---------- //
static constexpr const char *m_classFile = "[Content_Types].xml"; // passed to XLXmlFile constructor for underlying m_xmlName
};
} // namespace OpenXLSX

Expand Down
8 changes: 6 additions & 2 deletions OpenXLSX/headers/XLProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace OpenXLSX
class OPENXLSX_EXPORT XLProperties : public XLXmlFile
{
private:
static constexpr const char *m_classFile = "docProps/core.xml"; // passed to XLXmlFile constructor for underlying m_xmlName

/**
* @brief constructor helper function: create core.xml content from template
* @param workbook
Expand All @@ -81,7 +83,7 @@ namespace OpenXLSX
/**
* @brief
*/
XLProperties() = default;
XLProperties();

/**
* @brief
Expand Down Expand Up @@ -169,6 +171,8 @@ namespace OpenXLSX
class OPENXLSX_EXPORT XLAppProperties : public XLXmlFile
{
private:
static constexpr const char *m_classFile = "docProps/app.xml"; // passed to XLXmlFile constructor for underlying m_xmlName

/**
* @brief constructor helper function: create app.xml content from template
* @param workbook
Expand All @@ -183,7 +187,7 @@ namespace OpenXLSX
/**
* @brief
*/
XLAppProperties() = default;
XLAppProperties();

/**
* @brief enable XLAppProperties to re-create a worksheet list in docProps/app.xml <TitlesOfParts> element from workbookXml
Expand Down
4 changes: 3 additions & 1 deletion OpenXLSX/headers/XLSharedStrings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace OpenXLSX
/**
* @brief
*/
XLSharedStrings() = default;
XLSharedStrings();

/**
* @brief
Expand Down Expand Up @@ -172,6 +172,8 @@ namespace OpenXLSX

private:
std::deque<std::string>* m_stringCache {}; /** < Each string must have an unchanging memory address; hence the use of std::deque */

static constexpr const char *m_classFile = "xl/sharedStrings.xml"; // passed to XLXmlFile constructor for underlying m_xmlName
};
} // namespace OpenXLSX

Expand Down
31 changes: 26 additions & 5 deletions OpenXLSX/headers/XLSheet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ namespace OpenXLSX
/**
* @brief Constructor
*/
XLSheetBase() : XLXmlFile(nullptr) {};
XLSheetBase() : XLXmlFile(nullptr, m_classFile) {};

/**
* @brief The constructor. There are no default constructor, so all parameters must be provided for
* constructing an XLAbstractSheet object. Since this is a pure abstract class, instantiation is only
* possible via one of the derived classes.
* @param xmlData
* @param classFile base name of the underlying base class (without sheet numbering)
*/
explicit XLSheetBase(XLXmlData* xmlData) : XLXmlFile(xmlData) {};
explicit XLSheetBase(XLXmlData* xmlData) : XLXmlFile(xmlData, m_classFile) {};
explicit XLSheetBase(XLXmlData* xmlData, std::string classFile) : XLXmlFile(xmlData, classFile) {};

/**
* @brief The copy constructor.
Expand Down Expand Up @@ -299,6 +301,12 @@ namespace OpenXLSX
.setParam("sheetID", relationshipID())
.setParam("cloneName", newName));
}

//----------------------------------------------------------------------------------------------------------------------
// Private Member Variables
//----------------------------------------------------------------------------------------------------------------------
private:
static constexpr const char *m_classFile = "(XLSheetBase: uninitialized)"; // passed to XLXmlFile constructor for underlying m_xmlName
};

/**
Expand All @@ -319,7 +327,7 @@ namespace OpenXLSX
/**
* @brief Default constructor
*/
XLWorksheet() : XLSheetBase(nullptr) {};
XLWorksheet() : XLSheetBase(nullptr, m_classFile) {};

/**
* @brief
Expand Down Expand Up @@ -578,7 +586,13 @@ namespace OpenXLSX
*/
bool setActive_impl();

//----------------------------------------------------------------------------------------------------------------------
// Private Member Variables
//----------------------------------------------------------------------------------------------------------------------
private:
XLMergeCells m_merges; /**< class handling the <mergeCells> */

static constexpr const char *m_classFile = "xl/worksheets/sheet#.xml"; // passed to XLXmlFile constructor for underlying m_xmlName
};

/**
Expand All @@ -597,7 +611,7 @@ namespace OpenXLSX
/**
* @brief Default constructor
*/
XLChartsheet() : XLSheetBase(nullptr) {};
XLChartsheet() : XLSheetBase(nullptr, m_classFile) {};

/**
* @brief
Expand Down Expand Up @@ -660,6 +674,12 @@ namespace OpenXLSX
* @param selected
*/
void setSelected_impl(bool selected);

//----------------------------------------------------------------------------------------------------------------------
// Private Member Variables
//----------------------------------------------------------------------------------------------------------------------
private:
static constexpr const char *m_classFile = "XLChartsheet class: refer to ::name() for details"; // passed to XLXmlFile constructor for underlying m_xmlName
};

/**
Expand Down Expand Up @@ -833,9 +853,10 @@ namespace OpenXLSX
//----------------------------------------------------------------------------------------------------------------------
// Private Member Variables
//----------------------------------------------------------------------------------------------------------------------

private:
std::variant<XLWorksheet, XLChartsheet> m_sheet; /**< */

static constexpr const char *m_classFile = "(XLSheet: uninitialized)"; // passed to XLXmlFile constructor for underlying m_xmlName
};
} // namespace OpenXLSX

Expand Down
24 changes: 13 additions & 11 deletions OpenXLSX/headers/XLStyles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ namespace OpenXLSX
/**
* @brief
*/
XLStyles() = default;
XLStyles();

/**
* @brief
Expand All @@ -2129,30 +2129,30 @@ namespace OpenXLSX
~XLStyles();

/**
* @brief
* @param other
* @brief The copy constructor.
* @param other an existing styles object other will be also referred by this
*/
XLStyles(const XLStyles& other) = default;
XLStyles(const XLStyles& other);

/**
* @brief
* @param other
* @brief The move constructor.
* @param other an existing styles object other will be assigned to this
*/
XLStyles(XLStyles&& other) noexcept = default;
XLStyles(XLStyles&& other) noexcept;

/**
* @brief
* @brief copy assignment
* @param other
* @return
*/
XLStyles& operator=(const XLStyles& other) = default;
XLStyles& operator=(const XLStyles& other);

/**
* @brief
* @brief move assignment
* @param other
* @return
*/
XLStyles& operator=(XLStyles&& other) noexcept = default;
XLStyles& operator=(XLStyles&& other) noexcept;

/**
* @brief Get the number formats object
Expand Down Expand Up @@ -2206,6 +2206,8 @@ namespace OpenXLSX
std::unique_ptr<XLCellFormats> m_cellStyleFormats; // handle to the underlying cell style formats descriptions
std::unique_ptr<XLCellFormats> m_cellFormats; // handle to the underlying cell formats descriptions
std::unique_ptr<XLCellStyles> m_cellStyles; // handle to the underlying cell styles

static constexpr const char *m_classFile = "xl/styles.xml"; // passed to XLXmlFile constructor for underlying m_xmlName
};
} // namespace OpenXLSX

Expand Down
10 changes: 8 additions & 2 deletions OpenXLSX/headers/XLWorkbook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ namespace OpenXLSX
/**
* @brief Default constructor. Creates an empty ('null') XLWorkbook object.
*/
XLWorkbook() = default;
XLWorkbook();

/**
* @brief Constructor. Takes a pointer to an XLXmlData object (stored in the parent XLDocument object).
* @param xmlData A pointer to the underlying XLXmlData object, which holds the XML data.
* @note Do not create an XLWorkbook object directly. Get access through the an XLDocument object.
* @param pathTo the path to the workbook file origin of xmlData, used to initialize m_classFile
* @note Do not create an XLWorkbook object directly. Access via XLDocument::workbook().
*/
explicit XLWorkbook(XLXmlData* xmlData);
explicit XLWorkbook(XLXmlData* xmlData, std::string pathTo);

/**
* @brief Copy Constructor.
Expand Down Expand Up @@ -394,6 +396,10 @@ namespace OpenXLSX
* @return true if sheetNode can be considered visible (and could be activated)
*/
bool isVisible(XMLNode const& sheetNode) const;

private: // ---------- Private Member Variables ---------- //
static constexpr const char * m_classFileDefault = "xl/workbook.xml";
std::string m_classFile; // passed to XLXmlFile constructor for underlying m_xmlName, defaults to m_classFileDefault
};
} // namespace OpenXLSX

Expand Down
21 changes: 19 additions & 2 deletions OpenXLSX/headers/XLXmlFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ namespace OpenXLSX
/**
* @brief Constructor. Creates an object based on the xmlData input.
* @param xmlData An XLXmlData object with the XML data to be represented by the object.
* @param xmlName optional name of the underlying XML file within the document
*/
explicit XLXmlFile(XLXmlData* xmlData);
explicit XLXmlFile(XLXmlData* xmlData, std::string xmlName);

/**
* @brief Copy constructor. Default implementation used.
Expand All @@ -98,6 +100,20 @@ namespace OpenXLSX
*/
~XLXmlFile();

/**
* @brief check whether class is linked to a valid XML file
* @return true if the class should have a link to valid data
* @return false if accessing any other sheet properties / methods could cause a segmentation fault
* @note for example, if an XLSheet is created with a default constructor, XLSheetBase::valid() (derived from XLXmlFile) would return false
*/
bool valid() const { return m_xmlData != nullptr; }

/**
* @brief Method to retrieve a descriptive name of the class link to the document XML
* @return A std::string with the document's XML file
*/
std::string const & xmlName() const { return m_xmlName; }

/**
* @brief The copy assignment operator. The default implementation has been used.
* @param other The object to copy.
Expand Down Expand Up @@ -156,8 +172,9 @@ namespace OpenXLSX
*/
const XMLDocument& xmlDocument() const;

protected: // ===== PRIVATE MEMBER VARIABLES
XLXmlData* m_xmlData { nullptr }; /**< The underlying XML data object. */
protected: // ===== PRIVATE MEMBER VARIABLES
XLXmlData* m_xmlData { nullptr }; /**< The underlying XML data object. */
std::string m_xmlName { "uninitialized" }; /**< informational: should be initialized with path to the underlying XML file within the document */
};
} // namespace OpenXLSX

Expand Down
4 changes: 2 additions & 2 deletions OpenXLSX/sources/XLContentTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ std::string XLContentItem::path() const { return m_contentNode->attribute("PartN
/**
* @details
*/
XLContentTypes::XLContentTypes() = default;
XLContentTypes::XLContentTypes() : XLXmlFile(nullptr, m_classFile) {}

/**
* @details
*/
XLContentTypes::XLContentTypes(XLXmlData* xmlData) : XLXmlFile(xmlData) {}
XLContentTypes::XLContentTypes(XLXmlData* xmlData) : XLXmlFile(xmlData, m_classFile) {}

/**
* @details
Expand Down
6 changes: 4 additions & 2 deletions OpenXLSX/sources/XLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ void XLDocument::open(const std::string& fileName)
}

// ===== Open the workbook and document property items
m_workbook = XLWorkbook(getXmlData(workbookPath));
m_workbook = XLWorkbook(getXmlData(workbookPath), workbookPath);
// 2024-05-31: moved XLWorkbook object creation up in code worksheets info can be used for XLAppProperties generation from scratch

// ===== 2024-06-03: creating core and extended properties if they do not exist
Expand Down Expand Up @@ -693,11 +693,13 @@ void XLDocument::create(const std::string& fileName) { create( fileName, XLForce
void XLDocument::close()
{
if (m_archive.isValid()) m_archive.close();
// m_suppressWarnings shall remain in the configured setting

m_filePath.clear();
m_data.clear();

m_xmlSavingDeclaration = XLXmlSavingDeclaration();

m_data.clear();
m_sharedStringCache.clear(); // 2024-12-18 BUGFIX: clear shared strings cache - addresses issue #283
m_sharedStrings = XLSharedStrings(); //

Expand Down
14 changes: 12 additions & 2 deletions OpenXLSX/sources/XLProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,15 @@ void XLProperties::createFromTemplate()
prop.text().set("2019-08-16T00:34:26Z");
}

/**
* @details default constructor
*/
XLProperties::XLProperties() : XLXmlFile(nullptr, m_classFile) {}

/**
* @details
*/
XLProperties::XLProperties(XLXmlData* xmlData) : XLXmlFile(xmlData)
XLProperties::XLProperties(XLXmlData* xmlData) : XLXmlFile(xmlData, m_classFile)
{
XMLNode doc = xmlData->getXmlDocument()->document_element();
XMLNode child = doc.first_child_of_type(pugi::node_element);
Expand Down Expand Up @@ -382,11 +387,16 @@ void XLAppProperties::createFromTemplate(XMLDocument const & workbookXml)
props.append_child("AppVersion").text().set("16.0300");
}

/**
* @details default constructor
*/
XLAppProperties::XLAppProperties() : XLXmlFile(nullptr, m_classFile) {}

/**
* @details
*/
XLAppProperties::XLAppProperties(XLXmlData* xmlData, XMLDocument const & workbookXml)
: XLXmlFile(xmlData)
: XLXmlFile(xmlData, m_classFile)
{
XMLNode doc = xmlData->getXmlDocument()->document_element();
XMLNode child = doc.first_child_of_type(pugi::node_element);
Expand Down
2 changes: 1 addition & 1 deletion OpenXLSX/sources/XLRelationships.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ bool XLRelationshipItem::empty() const { return m_relationshipNode->empty(); }
* used to return all relationship targets as absolute paths within the XLSX archive
*/
XLRelationships::XLRelationships(XLXmlData* xmlData, std::string pathTo)
: XLXmlFile(xmlData)
: XLXmlFile(xmlData, pathTo)
{
constexpr const char *relFolder = "_rels/"; // all relationships are stored in a (sub-)folder named "_rels/"
static const size_t relFolderLen = strlen(relFolder); // 2024-08-23: strlen seems to not be accepted in a constexpr in VS2019 with c++17
Expand Down
9 changes: 7 additions & 2 deletions OpenXLSX/sources/XLSharedStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,18 @@ YM M9 MM MM MM MM MM d' `MM. MM MM d' `MM.

using namespace OpenXLSX;

/**
* @details Default constructor
*/
XLSharedStrings::XLSharedStrings() : XLXmlFile(nullptr, m_classFile) {}

/**
* @details Constructs a new XLSharedStrings object. Only one (common) object is allowed per XLDocument instance.
* A filepath to the underlying XML file must be provided.
*/
XLSharedStrings::XLSharedStrings(XLXmlData* xmlData, std::deque<std::string>* stringCache)
: XLXmlFile(xmlData),
m_stringCache(stringCache)
: XLXmlFile(xmlData, m_classFile),
m_stringCache(stringCache)
{
XMLDocument & doc = xmlDocument();
if (doc.document_element().empty()) // handle a bad (no document element) xl/sharedStrings.xml
Expand Down
Loading

0 comments on commit 02f7d42

Please sign in to comment.