Parcourir la source

Extended XMLReader class (incomplete)

- added XMLParseMode enum
- started "_readDeclaration" method
Patrick-Laptop il y a 3 ans
Parent
commit
f15bda4d3e

+ 2 - 1
CMakeLists.txt

@@ -122,7 +122,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDeclaration.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLDocument.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLReader.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/xml/XMLParseMode.hpp)
 
 set(LIBRARY_SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/lib/tiny_xml_2/include/tinyxml2.h

+ 22 - 0
source/io/xml/XMLParseMode.hpp

@@ -0,0 +1,22 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-10-13
+ * Changed:         2020-10-13
+ *
+ * */
+
+#ifndef LS_STD_XML_PARSE_MODE_HPP
+#define LS_STD_XML_PARSE_MODE_HPP
+
+namespace ls_std {
+  enum XMLParseMode {
+    XML_PARSE_MODE_ANALYZE = 0,
+    XML_PARSE_MODE_DECLARATION,
+    XML_PARSE_MODE_OPENING_TAG,
+    XML_PARSE_MODE_CLOSING_TAG
+  };
+}
+
+#endif

+ 44 - 11
source/io/xml/XMLReader.cpp

@@ -53,29 +53,62 @@ void ls_std::XMLReader::_checkFileExistence(ls_std::File _xmlFile)
   }
 }
 
-void ls_std::XMLReader::_read(const ls_std::byte_field &_data)
+void ls_std::XMLReader::_isClosingTag(const ls_std::byte_field &_data, std::string::size_type _index)
 {
-  uint8_t mode {};
+  if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 2) == "</") {
+    this->mode = XML_PARSE_MODE_CLOSING_TAG;
+  }
+}
 
-  for(std::string::size_type i = 0 ; i < _data.size() ; i++) {
-    switch(mode)
-    {
-      case 1: // xml declaration
-      {
+void ls_std::XMLReader::_isDeclaration(const ls_std::byte_field& _data, std::string::size_type _index)
+{
+  if(_data.substr(_index, 5) == "<?xml") {
+    this->mode = XML_PARSE_MODE_DECLARATION;
+  }
+}
+
+void ls_std::XMLReader::_isOpeningTag(const ls_std::byte_field &_data, std::string::size_type _index)
+{
+  if(this->mode == XML_PARSE_MODE_ANALYZE && _data.substr(_index, 1) == "<") {
+    this->mode = XML_PARSE_MODE_OPENING_TAG;
+  }
+}
 
+void ls_std::XMLReader::_read(const ls_std::byte_field &_data)
+{
+  for(std::string::size_type index = 0 ; index < _data.size() ; index++) {
+    switch(this->mode) {
+      case XML_PARSE_MODE_ANALYZE:
+      {
+        this->_isDeclaration(_data, index);
+        this->_isClosingTag(_data, index);
+        this->_isOpeningTag(_data, index);
       } break;
-      case 2: // xml opening tag
+      case XML_PARSE_MODE_DECLARATION:
       {
-
+        index = ls_std::XMLReader::_readDeclaration(_data, index);
       } break;
-      case 3: // xml closing tag
+      case XML_PARSE_MODE_OPENING_TAG:
       {
 
       } break;
-      case 4: // xml attribute
+      case XML_PARSE_MODE_CLOSING_TAG:
       {
 
       } break;
     }
   }
 }
+
+size_t ls_std::XMLReader::_readDeclaration(const ls_std::byte_field &_data, std::string::size_type _index)
+{
+  size_t closingTagPosition = _data.find('>');
+  std::string declarationString {};
+
+  if(closingTagPosition != std::string::npos) {
+    declarationString = _data.substr(_index, closingTagPosition - _index);
+  }
+
+  return (closingTagPosition == std::string::npos) ? _index : closingTagPosition;
+}
+

+ 6 - 0
source/io/xml/XMLReader.hpp

@@ -14,6 +14,7 @@
 #include "XMLDocument.hpp"
 #include "../IReader.hpp"
 #include "../File.hpp"
+#include "XMLParseMode.hpp"
 
 namespace ls_std {
   class XMLReader : public Class, IReader {
@@ -34,11 +35,16 @@ namespace ls_std {
     private:
 
       std::shared_ptr<ls_std::XMLDocument> document {};
+      ls_std::XMLParseMode mode {};
       ls_std::File xmlFile;
 
       static void _checkDocumentExistence(const std::shared_ptr<ls_std::XMLDocument>& _document);
       static void _checkFileExistence(ls_std::File _xmlFile);
+      void _isClosingTag(const ls_std::byte_field& _data, std::string::size_type _index);
+      void _isDeclaration(const ls_std::byte_field& _data, std::string::size_type _index);
+      void _isOpeningTag(const ls_std::byte_field& _data, std::string::size_type _index);
       void _read(const ls_std::byte_field& _data);
+      static size_t _readDeclaration(const ls_std::byte_field& _data, std::string::size_type _index);
   };
 }
 

+ 1 - 1
test/TestHelper.hpp

@@ -38,7 +38,7 @@ class TestHelper {
       std::string location {};
 
       #ifdef _WIN32
-        location = R"(C:\Users\drums\CLionProjects\lynar-studios-standard-library\test\)";
+        location = R"(C:\Users\Admin\CLionProjects\lynar-studios-standard-library\test\)";
       #endif
       #ifdef unix
         location = R"(/home/patrick/CLionProjects/lynar-studios-standard-library/test/)";