KvParser.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-25
  6. * Changed: 2023-02-08
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/NewLine.hpp>
  11. #include <ls-std/io/kv/KvParser.hpp>
  12. ls::std::io::KvParser::KvParser(const ::std::shared_ptr<ls::std::io::KvDocument> &_document) : ls::std::core::Class("KvParser")
  13. {
  14. this->_assignDocument(_document);
  15. }
  16. ls::std::io::KvParser::~KvParser() = default;
  17. ::std::shared_ptr<ls::std::io::KvDocument> ls::std::io::KvParser::getDocument()
  18. {
  19. return this->document;
  20. }
  21. void ls::std::io::KvParser::parse(const ls::std::core::type::byte_field &_data)
  22. {
  23. this->_parse(_data);
  24. }
  25. void ls::std::io::KvParser::setDocument(const ::std::shared_ptr<ls::std::io::KvDocument> &_document)
  26. {
  27. this->_assignDocument(_document);
  28. }
  29. void ls::std::io::KvParser::_assignDocument(const ::std::shared_ptr<ls::std::io::KvDocument> &_document)
  30. {
  31. ls::std::core::NullPointerArgumentEvaluator{_document, "reference to document is null!"}.evaluate();
  32. this->document = _document;
  33. }
  34. bool ls::std::io::KvParser::_contains(const ::std::string &_text, const ::std::string &_searchText)
  35. {
  36. return _text.find(_searchText) != ::std::string::npos;
  37. }
  38. bool ls::std::io::KvParser::_lineHasPair(ls::std::io::KvParseParameter &_parseParameter)
  39. {
  40. return ls::std::io::KvParser::_contains(_parseParameter.getLine(), "=") && ls::std::io::KvParser::_contains(_parseParameter.getLine(), ";");
  41. }
  42. void ls::std::io::KvParser::_parse(const ls::std::core::type::byte_field &_data)
  43. {
  44. for (::std::string::size_type index = 0; index < _data.size(); index++)
  45. {
  46. ls::std::io::KvParseParameter parseParameter = ls::std::io::KvParser::_readLine(_data, index);
  47. this->_parsePair(parseParameter);
  48. index = parseParameter.getIndex();
  49. }
  50. }
  51. void ls::std::io::KvParser::_parsePair(ls::std::io::KvParseParameter &_parseParameter)
  52. {
  53. if (ls::std::io::KvParser::_lineHasPair(_parseParameter))
  54. {
  55. size_t equalSignPosition = _parseParameter.getLine().find('=');
  56. ls::std::core::type::kv_key key = _parseParameter.getLine().substr(0, equalSignPosition);
  57. ls::std::core::type::kv_value value = _parseParameter.getLine().substr(equalSignPosition + 1);
  58. value = value.substr(0, value.find(';'));
  59. this->document->addPair(ls::std::io::KvPair{key, value});
  60. }
  61. }
  62. ls::std::io::KvParseParameter ls::std::io::KvParser::_readLine(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  63. {
  64. ls::std::io::KvParseParameter parseParameter{};
  65. parseParameter.setLine(_data.substr(_index));
  66. if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getWindowsNewLine()))
  67. {
  68. ls::std::io::KvParser::_readLineWithWindowsLineBreak(parseParameter);
  69. }
  70. else
  71. {
  72. if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getUnixNewLine()))
  73. {
  74. ls::std::io::KvParser::_readLineWithUnixLineBreak(parseParameter);
  75. }
  76. }
  77. parseParameter.setIndex(_index + parseParameter.getLine().size());
  78. return parseParameter;
  79. }
  80. void ls::std::io::KvParser::_readLineWithUnixLineBreak(ls::std::io::KvParseParameter &_parseParameter)
  81. {
  82. size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getUnixNewLine());
  83. _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition));
  84. }
  85. void ls::std::io::KvParser::_readLineWithWindowsLineBreak(ls::std::io::KvParseParameter &_parseParameter)
  86. {
  87. size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getWindowsNewLine());
  88. _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition + 1));
  89. }