diff --git a/CMakeLists-xmlParser.txt b/CMakeLists-xmlParser.txt index 0151c3d..3d29bfa 100644 --- a/CMakeLists-xmlParser.txt +++ b/CMakeLists-xmlParser.txt @@ -1,14 +1,14 @@ -cmake_minimum_required( VERSION 2.8.7 ) +cmake_minimum_required(VERSION 3.11) -set(xmlParser_INCLUDE_DIRS "${CMAKE_BINARY_DIR}/xmlParser-src/include") +include(FetchContent) -include_directories(${xmlParser_INCLUDE_DIRS}) +FetchContent_Declare( + XMLParser + GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git + GIT_TAG v1.0.0 +) -configure_file(CMakeLists-xmlParser.txt.in ${CMAKE_BINARY_DIR}/xmlParser-download/CMakeLists.txt) -execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/xmlParser-download) -execute_process(COMMAND ${CMAKE_COMMAND} --build . - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/xmlParser-download) - -add_subdirectory(${CMAKE_BINARY_DIR}/xmlParser-src "${CMAKE_CURRENT_BINARY_DIR}/xmlParser-src") -set(xmlParser_LIB "${CMAKE_SHARED_LIBRARY_PREFIX}xmlParser${CMAKE_SHARED_LIBRARY_SUFFIX}") +FetchContent_MakeAvailable(XMLParser) + +# Assuming the target name is xmlParser +target_include_directories(xmlParser SYSTEM PUBLIC ${xmlParser_INCLUDE_DIRS}) \ No newline at end of file diff --git a/CMakeLists-xmlParser.txt.in b/CMakeLists-xmlParser.txt.in deleted file mode 100644 index c964359..0000000 --- a/CMakeLists-xmlParser.txt.in +++ /dev/null @@ -1,19 +0,0 @@ -cmake_minimum_required(VERSION 3.12) - -include(ExternalProject) - -ExternalProject_Add(XMLParser - GIT_REPOSITORY https://github.com/LBNL-ETA/XMLParser.git - GIT_TAG "v1.0.0" - - UPDATE_COMMAND "" - PATCH_COMMAND "" - - SOURCE_DIR "${CMAKE_BINARY_DIR}/xmlParser-src" - BINARY_DIR "${CMAKE_BINARY_DIR}/xmlParser-build" - - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - TEST_COMMAND "" - INSTALL_COMMAND "" -) diff --git a/CMakeLists.txt b/CMakeLists.txt index e236bd1..6646e47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -project( FileParse CXX ) +project(FileParse CXX) set(LIB_MAJOR_VERSION "1") set(LIB_MINOR_VERSION "1") @@ -35,16 +35,16 @@ include(CMakeLists-xmlParser.txt) add_subdirectory(include/fileParse) target_include_directories(${LIB_NAME} - PUBLIC - $ + PUBLIC + $ $ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ) -Option(BUILD_FileParse_tests "Build FileParse tests." ON) +option(BUILD_FileParse_tests "Build FileParse tests." ON) if(BUILD_FileParse_tests) enable_testing() - add_subdirectory( test ) + add_subdirectory(test) endif() \ No newline at end of file diff --git a/include/fileParse/Array.hxx b/include/fileParse/Array.hxx index d6f53ab..23ea3eb 100644 --- a/include/fileParse/Array.hxx +++ b/include/fileParse/Array.hxx @@ -1,4 +1,4 @@ -/// File: FP_Array.hxx +/// File: Array.hxx /// @brief Provides functionality to serialize and deserialize std::array and /// std::optional types in the FileParse namespace. diff --git a/include/fileParse/Base.hxx b/include/fileParse/Base.hxx index 200c6e5..2fbc20b 100644 --- a/include/fileParse/Base.hxx +++ b/include/fileParse/Base.hxx @@ -1,4 +1,4 @@ -/// File: FP_Base.hxx +/// File: Base.hxx /// @brief Provides a set of functions and templates for parsing and manipulating XML-like data /// structures. @@ -9,6 +9,7 @@ #include #include #include +#include #include "Formatter.hxx" diff --git a/include/fileParse/Common.hxx b/include/fileParse/Common.hxx index f8f84d5..da65fd3 100644 --- a/include/fileParse/Common.hxx +++ b/include/fileParse/Common.hxx @@ -1,4 +1,4 @@ -/// File: FP_Common.hxx +/// File: Common.hxx /// @brief Provides a set of functions and templates for parsing and manipulating XML-like data /// structures. diff --git a/include/fileParse/Enum.hxx b/include/fileParse/Enum.hxx index ab52512..d695165 100644 --- a/include/fileParse/Enum.hxx +++ b/include/fileParse/Enum.hxx @@ -1,4 +1,4 @@ -/// File: FP_Enum.hxx +/// File: Enum.hxx /// @brief Provides templates for serializing and deserializing enumeration types /// using NodeAdapter in the FileParse namespace. It includes functionality /// to convert enumeration types to and from their string representations diff --git a/include/fileParse/Formatter.cxx b/include/fileParse/Formatter.cxx index 707ddd1..0410b07 100644 --- a/include/fileParse/Formatter.cxx +++ b/include/fileParse/Formatter.cxx @@ -16,10 +16,13 @@ namespace FileParse } // Remove the decimal point if it's directly before 'e' or 'E' auto dotEPos = str.find_first_of(".eE", lastNonZero); - if(dotEPos != std::string::npos && str[dotEPos + 1] == 'e' || str[dotEPos + 1] == 'E') + if (dotEPos != std::string::npos && + (dotEPos + 1 < str.size()) && + (str[dotEPos + 1] == 'e' || str[dotEPos + 1] == 'E')) { str.erase(dotEPos, 1); } + } else { diff --git a/include/fileParse/INodeAdapter.hxx b/include/fileParse/INodeAdapter.hxx index 3098bf0..47a916f 100644 --- a/include/fileParse/INodeAdapter.hxx +++ b/include/fileParse/INodeAdapter.hxx @@ -1,4 +1,4 @@ -/// File: FP_INodeAdapter.hxx +/// File: INodeAdapter.hxx /// @brief Defines the INodeAdapter interface which provides a set of functions /// for node manipulation and data extraction, used by a general parser. @@ -48,4 +48,8 @@ public: /// Adds text content to the current node. virtual void addText(std::string_view text) = 0; + + // Adds content of the entire node structure with child into string (the same content + // that will be in the file). + [[nodiscard]] virtual std::string getContent() const = 0; }; diff --git a/include/fileParse/Map.hxx b/include/fileParse/Map.hxx index bd62b99..c798a51 100644 --- a/include/fileParse/Map.hxx +++ b/include/fileParse/Map.hxx @@ -1,4 +1,4 @@ -/// File: FP_Map.hxx +/// File: Map.hxx /// @brief: Provides serialization and deserialization functionalities for maps and enum maps /// in the FileParse namespace, including utility functions for handling child nodes /// and converting enum types to and from string representations. diff --git a/include/fileParse/Optional.hxx b/include/fileParse/Optional.hxx index 7796197..953da93 100644 --- a/include/fileParse/Optional.hxx +++ b/include/fileParse/Optional.hxx @@ -1,4 +1,4 @@ -/// File: FP_Optional.hxx +/// File: Optional.hxx /// @brief Provides functionality to serialize optional data types /// in the FileParse namespace. diff --git a/include/fileParse/Set.hxx b/include/fileParse/Set.hxx index a4a3ab0..0d15615 100644 --- a/include/fileParse/Set.hxx +++ b/include/fileParse/Set.hxx @@ -1,4 +1,4 @@ -/// File: FP_Set.hxx +/// File: Set.hxx /// @brief Provides serialization and deserialization functionalities for sets and enum sets /// in the FileParse namespace. diff --git a/include/fileParse/StringConversion.hxx b/include/fileParse/StringConversion.hxx index fa40806..f3d2226 100644 --- a/include/fileParse/StringConversion.hxx +++ b/include/fileParse/StringConversion.hxx @@ -1,4 +1,4 @@ -/// File: FP_StringConversion.hxx +/// File: StringConversion.hxx /// @brief Provides functionality to convert strings to various basic data types /// in the FileParse namespace. diff --git a/include/fileParse/Variant.hxx b/include/fileParse/Variant.hxx index 31c7b33..5440540 100644 --- a/include/fileParse/Variant.hxx +++ b/include/fileParse/Variant.hxx @@ -1,4 +1,4 @@ -/// File: FP_Variant.hxx +/// File: Variant.hxx /// @brief Provides functionality to serialize and deserialize std::variant and /// std::optional types in the FileParse namespace. diff --git a/include/fileParse/Vector.hxx b/include/fileParse/Vector.hxx index f0e9aa2..1b658d7 100644 --- a/include/fileParse/Vector.hxx +++ b/include/fileParse/Vector.hxx @@ -1,4 +1,4 @@ -/// File: FP_Vector.hxx +/// File: Vector.hxx /// @brief Provides functionality to serialize and deserialize std::vector and /// std::optional types in the FileParse namespace. diff --git a/include/fileParse/XMLNodeAdapter.cxx b/include/fileParse/XMLNodeAdapter.cxx index 2dec223..904be83 100644 --- a/include/fileParse/XMLNodeAdapter.cxx +++ b/include/fileParse/XMLNodeAdapter.cxx @@ -105,12 +105,17 @@ std::string XMLNodeAdapter::getCurrentTag() const return pimpl_->node_.getName(); } +std::string XMLNodeAdapter::getContent() const +{ + return pimpl_->node_.createXMLString(); +} + XMLNodeAdapter createTopNode(std::string_view topNodeName) { return XMLNodeAdapter(XMLParser::XMLNode::createXMLTopNode(topNodeName.data())); } -std::optional getTopNode(std::string_view fileName, std::string_view topNodeName) +std::optional getTopNodeFromFile(std::string_view fileName, std::string_view topNodeName) { if(auto node{XMLParser::XMLNode::openFileHelper(fileName.data(), topNodeName.data())}; !node.isEmpty()) @@ -119,3 +124,14 @@ std::optional getTopNode(std::string_view fileName, std::string_ } return std::nullopt; } + +std::optional getTopNodeFromString(std::string_view xml, + std::string_view topNodeName) +{ + if(auto node{XMLParser::XMLNode::parseString(xml.data(), topNodeName.data())}; + !node.isEmpty()) + { + return XMLNodeAdapter(node); + } + return std::nullopt; +} diff --git a/include/fileParse/XMLNodeAdapter.hxx b/include/fileParse/XMLNodeAdapter.hxx index 01de4be..96c3487 100644 --- a/include/fileParse/XMLNodeAdapter.hxx +++ b/include/fileParse/XMLNodeAdapter.hxx @@ -1,4 +1,4 @@ -/// File: FP_XMLNodeAdapter.hxx +/// File: XMLNodeAdapter.hxx /// @brief Provides a node adapter for XML parsing and serialization /// using an internal implementation of XMLNode. @@ -74,6 +74,8 @@ public: /// @return The number of characters written. [[nodiscard]] int writeToFile(std::string_view outString) const; + [[nodiscard]] std::string getContent() const override; + private: struct Impl; std::shared_ptr pimpl_; ///< Pointer to the implementation details. @@ -88,5 +90,8 @@ private: /// @param fileName The name of the file containing the XML data. /// @param topNodeName The name of the top node to retrieve. /// @return An optional containing the top node adapter if successful, std::nullopt otherwise. -[[nodiscard]] std::optional getTopNode(std::string_view fileName, +[[nodiscard]] std::optional getTopNodeFromFile(std::string_view fileName, std::string_view topNodeName); + +[[nodiscard]] std::optional getTopNodeFromString(std::string_view xml, + std::string_view topNodeName); \ No newline at end of file diff --git a/test/helper/MockNodeAdapter.cxx b/test/helper/MockNodeAdapter.cxx index 703ed2a..2653783 100644 --- a/test/helper/MockNodeAdapter.cxx +++ b/test/helper/MockNodeAdapter.cxx @@ -98,6 +98,12 @@ namespace Helper return *node_; } + // Not implemented yet since it is not used + std::string MockNodeAdapter::getContent() const + { + return ""; + } + MockNode & addChildNode(MockNode & parentNode, std::string_view tag, std::string_view text) { MockNode node; diff --git a/test/helper/MockNodeAdapter.hxx b/test/helper/MockNodeAdapter.hxx index d172288..91b3fd4 100644 --- a/test/helper/MockNodeAdapter.hxx +++ b/test/helper/MockNodeAdapter.hxx @@ -54,6 +54,8 @@ namespace Helper [[nodiscard]] MockNode getNode() const; + std::string getContent() const override; + private: MockNode * node_{nullptr}; }; diff --git a/test/helper/files/BaseElementXML.cxx b/test/helper/files/BaseElementXML.cxx index c033ae1..da12503 100644 --- a/test/helper/files/BaseElementXML.cxx +++ b/test/helper/files/BaseElementXML.cxx @@ -37,7 +37,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; BaseElement base; if(xmlNode.has_value()) diff --git a/test/helper/files/EnumElementXML.cxx b/test/helper/files/EnumElementXML.cxx index 103e93f..04af970 100644 --- a/test/helper/files/EnumElementXML.cxx +++ b/test/helper/files/EnumElementXML.cxx @@ -48,7 +48,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; EnumElement enumEl; if(xmlNode.has_value()) diff --git a/test/helper/files/MapElementXML.cxx b/test/helper/files/MapElementXML.cxx index d0b4bd5..8c183c5 100644 --- a/test/helper/files/MapElementXML.cxx +++ b/test/helper/files/MapElementXML.cxx @@ -154,7 +154,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; MapElementString element; if(xmlNode.has_value()) @@ -179,7 +179,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; MapElementOptionalString element; if(xmlNode.has_value()) @@ -205,7 +205,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; MapElementEnum element; if(xmlNode.has_value()) @@ -230,7 +230,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; MapElementDouble element; if(xmlNode.has_value()) @@ -256,7 +256,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; MapElementEnumDouble element; if(xmlNode.has_value()) @@ -283,7 +283,7 @@ namespace Helper using FileParse::Child; using FileParse::operator>>; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; CMAElement element; if(xmlNode.has_value()) diff --git a/test/helper/files/SetElementXML.cxx b/test/helper/files/SetElementXML.cxx index 0a1121b..1cd0fd3 100644 --- a/test/helper/files/SetElementXML.cxx +++ b/test/helper/files/SetElementXML.cxx @@ -106,7 +106,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; SetElementDouble element; if(xmlNode.has_value()) @@ -132,7 +132,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; SetElementOptionalDouble element; if(xmlNode.has_value()) @@ -159,7 +159,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; SetElementEnum element; if(xmlNode.has_value()) diff --git a/test/helper/files/VariantElementXML.cxx b/test/helper/files/VariantElementXML.cxx index 2bb18ec..28ac6ec 100644 --- a/test/helper/files/VariantElementXML.cxx +++ b/test/helper/files/VariantElementXML.cxx @@ -49,7 +49,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; VariantsAll element; if(xmlNode.has_value()) diff --git a/test/helper/files/VectorElementXML.cxx b/test/helper/files/VectorElementXML.cxx index bd1ba3d..8869ff8 100644 --- a/test/helper/files/VectorElementXML.cxx +++ b/test/helper/files/VectorElementXML.cxx @@ -107,7 +107,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; VectorElement element; if(xmlNode.has_value()) @@ -133,7 +133,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; OptionalVectorElement element; if(xmlNode.has_value()) @@ -159,7 +159,7 @@ namespace Helper { using FileParse::Child; - auto xmlNode{getTopNode(fileName, "Test")}; + auto xmlNode{getTopNodeFromFile(fileName, "Test")}; EnumVectorElement element; if(xmlNode.has_value())