Browse Source

Convert KvParseParameter into a class

Patrick-Christopher Mattulat 2 years ago
parent
commit
bb41d5189f

+ 2 - 0
CMakeLists.txt

@@ -180,6 +180,7 @@ set(SOURCE_FILES_IO
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/kv/KvDocument.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/kv/KvFileReader.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/kv/KvPair.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/kv/KvParseParameter.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/kv/KvParser.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/logging/Logger.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls-std/io/logging/LogLevel.cpp
@@ -256,6 +257,7 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvDocumentTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvFileReaderTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvPairTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvParseParameterTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KvParserTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LoggerTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LogLevelTest.cpp

+ 11 - 1
include/ls-std/io/kv/KvParseParameter.hpp

@@ -14,10 +14,20 @@
 
 namespace ls::std::io
 {
-  struct KvParseParameter // TODO: turn to class, rather than keeping it as struct
+  class KvParseParameter
   {
     public:
 
+      KvParseParameter();
+      ~KvParseParameter();
+
+      [[nodiscard]] ::std::string::size_type getIndex() const;
+      [[nodiscard]] ::std::string getLine();
+      void setIndex(const ::std::string::size_type &_index);
+      void setLine(const ::std::string &_line);
+
+    private:
+
       ::std::string::size_type index{};
       ::std::string line{};
   };

+ 3 - 3
include/ls-std/io/kv/KvParser.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2023-02-04
+ * Changed:         2023-02-05
  *
  * */
 
@@ -36,9 +36,9 @@ namespace ls::std::io
 
       void _assignDocument(const ::std::shared_ptr<ls::std::io::KvDocument> &_document);
       static bool _contains(const ::std::string &_text, const ::std::string &_searchText);
-      static bool _lineHasPair(const ls::std::io::KvParseParameter &_parseParameter);
+      static bool _lineHasPair(ls::std::io::KvParseParameter &_parseParameter);
       void _parse(const ls::std::core::type::byte_field &_data);
-      void _parsePair(const ls::std::io::KvParseParameter &_parseParameter);
+      void _parsePair(ls::std::io::KvParseParameter &_parseParameter);
       static ls::std::io::KvParseParameter _readLine(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index);
       static void _readLineWithUnixLineBreak(ls::std::io::KvParseParameter &_parseParameter);
       static void _readLineWithWindowsLineBreak(ls::std::io::KvParseParameter &_parseParameter);

+ 34 - 0
source/ls-std/io/kv/KvParseParameter.cpp

@@ -0,0 +1,34 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-05
+* Changed:         2023-02-05
+*
+* */
+
+#include <ls-std/io/kv/KvParseParameter.hpp>
+
+ls::std::io::KvParseParameter::KvParseParameter() = default;
+
+ls::std::io::KvParseParameter::~KvParseParameter() = default;
+
+::std::string::size_type ls::std::io::KvParseParameter::getIndex() const
+{
+  return this->index;
+}
+
+::std::string ls::std::io::KvParseParameter::getLine()
+{
+  return this->line;
+}
+
+void ls::std::io::KvParseParameter::setIndex(const ::std::string::size_type &_index)
+{
+  this->index = _index;
+}
+
+void ls::std::io::KvParseParameter::setLine(const ::std::string &_line)
+{
+  this->line = _line;
+}

+ 16 - 16
source/ls-std/io/kv/KvParser.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2023-02-04
+ * Changed:         2023-02-05
  *
  * */
 
@@ -48,9 +48,9 @@ bool ls::std::io::KvParser::_contains(const ::std::string &_text, const ::std::s
   return _text.find(_searchText) != ::std::string::npos;
 }
 
-bool ls::std::io::KvParser::_lineHasPair(const ls::std::io::KvParseParameter &_parseParameter)
+bool ls::std::io::KvParser::_lineHasPair(ls::std::io::KvParseParameter &_parseParameter)
 {
-  return ls::std::io::KvParser::_contains(_parseParameter.line, "=") && ls::std::io::KvParser::_contains(_parseParameter.line, ";");
+  return ls::std::io::KvParser::_contains(_parseParameter.getLine(), "=") && ls::std::io::KvParser::_contains(_parseParameter.getLine(), ";");
 }
 
 void ls::std::io::KvParser::_parse(const ls::std::core::type::byte_field &_data)
@@ -59,17 +59,17 @@ void ls::std::io::KvParser::_parse(const ls::std::core::type::byte_field &_data)
   {
     ls::std::io::KvParseParameter parseParameter = ls::std::io::KvParser::_readLine(_data, index);
     this->_parsePair(parseParameter);
-    index = parseParameter.index;
+    index = parseParameter.getIndex();
   }
 }
 
-void ls::std::io::KvParser::_parsePair(const ls::std::io::KvParseParameter &_parseParameter)
+void ls::std::io::KvParser::_parsePair(ls::std::io::KvParseParameter &_parseParameter)
 {
   if (ls::std::io::KvParser::_lineHasPair(_parseParameter))
   {
-    size_t equalSignPosition = _parseParameter.line.find('=');
-    ls::std::core::type::kv_key key = _parseParameter.line.substr(0, equalSignPosition);
-    ls::std::core::type::kv_value value = _parseParameter.line.substr(equalSignPosition + 1);
+    size_t equalSignPosition = _parseParameter.getLine().find('=');
+    ls::std::core::type::kv_key key = _parseParameter.getLine().substr(0, equalSignPosition);
+    ls::std::core::type::kv_value value = _parseParameter.getLine().substr(equalSignPosition + 1);
     value = value.substr(0, value.find(';'));
 
     this->document->addPair(ls::std::io::KvPair{key, value});
@@ -79,32 +79,32 @@ void ls::std::io::KvParser::_parsePair(const ls::std::io::KvParseParameter &_par
 ls::std::io::KvParseParameter ls::std::io::KvParser::_readLine(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
 {
   ls::std::io::KvParseParameter parseParameter{};
-  parseParameter.line = _data.substr(_index);
+  parseParameter.setLine(_data.substr(_index));
 
-  if (ls::std::io::KvParser::_contains(parseParameter.line, ls::std::io::NewLine::getWindowsNewLine()))
+  if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getWindowsNewLine()))
   {
     ls::std::io::KvParser::_readLineWithWindowsLineBreak(parseParameter);
   }
   else
   {
-    if (ls::std::io::KvParser::_contains(parseParameter.line, ls::std::io::NewLine::getUnixNewLine()))
+    if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getUnixNewLine()))
     {
       ls::std::io::KvParser::_readLineWithUnixLineBreak(parseParameter);
     }
   }
 
-  parseParameter.index = _index + parseParameter.line.size();
+  parseParameter.setIndex(_index + parseParameter.getLine().size());
   return parseParameter;
 }
 
 void ls::std::io::KvParser::_readLineWithUnixLineBreak(ls::std::io::KvParseParameter &_parseParameter)
 {
-  size_t newLinePosition = _parseParameter.line.find(ls::std::io::NewLine::getUnixNewLine());
-  _parseParameter.line = _parseParameter.line.substr(0, newLinePosition);
+  size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getUnixNewLine());
+  _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition));
 }
 
 void ls::std::io::KvParser::_readLineWithWindowsLineBreak(ls::std::io::KvParseParameter &_parseParameter)
 {
-  size_t newLinePosition = _parseParameter.line.find(ls::std::io::NewLine::getWindowsNewLine());
-  _parseParameter.line = _parseParameter.line.substr(0, newLinePosition + 1);
+  size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getWindowsNewLine());
+  _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition + 1));
 }

+ 6 - 6
test/cases/io/kv/KvFileReaderTest.cpp

@@ -32,7 +32,7 @@ namespace
       void TearDown() override
       {}
 
-      static shared_ptr<KvFileReader> createTestKVFileReader()
+      static shared_ptr<KvFileReader> createTestKvFileReader()
       {
         string kvPath = TestHelper::getResourcesFolderLocation() + "server-settings.kv";
         shared_ptr<KvDocument> document = make_shared<KvDocument>();
@@ -76,7 +76,7 @@ namespace
 
   TEST_F(KvFileReaderTest, getDocument)
   {
-    const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
+    const shared_ptr<KvFileReader> &reader = createTestKvFileReader();
     ASSERT_TRUE(reader->getDocument() != nullptr);
   }
 
@@ -84,7 +84,7 @@ namespace
   {
     // preparation
 
-    const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
+    const shared_ptr<KvFileReader> &reader = createTestKvFileReader();
 
     // read file and check
 
@@ -103,7 +103,7 @@ namespace
 
   TEST_F(KvFileReaderTest, setFile_no_existing_file)
   {
-    const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
+    const shared_ptr<KvFileReader> &reader = createTestKvFileReader();
 
     EXPECT_THROW(
         {
@@ -123,7 +123,7 @@ namespace
   {
     // preparation
 
-    const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
+    const shared_ptr<KvFileReader> &reader = createTestKvFileReader();
 
     // set new document and check
 
@@ -134,7 +134,7 @@ namespace
 
   TEST_F(KvFileReaderTest, setDocument_no_reference)
   {
-    const shared_ptr<KvFileReader> &reader = createTestKVFileReader();
+    const shared_ptr<KvFileReader> &reader = createTestKvFileReader();
 
     EXPECT_THROW(
         {

+ 1 - 2
test/cases/io/kv/KvPairTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-12-25
- * Changed:         2023-02-04
+ * Changed:         2023-02-05
  *
  * */
 
@@ -21,7 +21,6 @@ namespace
     protected:
 
       KvPairTest() = default;
-
       ~KvPairTest() override = default;
 
       void SetUp() override

+ 59 - 0
test/cases/io/kv/KvParseParameterTest.cpp

@@ -0,0 +1,59 @@
+/*
+* Author:          Patrick-Christopher Mattulat
+* Company:         Lynar Studios
+* E-Mail:          webmaster@lynarstudios.com
+* Created:         2023-02-05
+* Changed:         2023-02-05
+*
+* */
+
+#include <gtest/gtest.h>
+#include <ls-std/ls-std-io.hpp>
+#include <string>
+
+using namespace ls::std::io;
+using namespace ::std;
+
+namespace
+{
+  class KvParseParameterTest : public ::testing::Test
+  {
+    protected:
+
+      KvParseParameterTest() = default;
+      ~KvParseParameterTest() override = default;
+
+      void SetUp() override
+      {}
+
+      void TearDown() override
+      {}
+  };
+
+  TEST_F(KvParseParameterTest, getIndex)
+  {
+    ASSERT_EQ(0, KvParseParameter{}.getIndex());
+  }
+
+  TEST_F(KvParseParameterTest, getLine)
+  {
+    ASSERT_TRUE(KvParseParameter{}.getLine().empty());
+  }
+
+  TEST_F(KvParseParameterTest, setIndex)
+  {
+    KvParseParameter parameter{};
+    parameter.setIndex(3);
+
+    ASSERT_EQ(3, parameter.getIndex());
+  }
+
+  TEST_F(KvParseParameterTest, setLine)
+  {
+    KvParseParameter parameter{};
+    string line = "key=value";
+    parameter.setLine(line);
+
+    ASSERT_STREQ(line.c_str(), parameter.getLine().c_str());
+  }
+}