KvParser.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-07
  7. *
  8. * */
  9. #include <ls-std/core/exception/IllegalArgumentException.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. if (_document == nullptr)
  32. {
  33. throw ls::std::core::IllegalArgumentException{"_document is null"};
  34. }
  35. this->document = _document;
  36. }
  37. bool ls::std::io::KvParser::_contains(const ::std::string &_text, const ::std::string &_searchText)
  38. {
  39. return _text.find(_searchText) != ::std::string::npos;
  40. }
  41. bool ls::std::io::KvParser::_lineHasPair(ls::std::io::KvParseParameter &_parseParameter)
  42. {
  43. return ls::std::io::KvParser::_contains(_parseParameter.getLine(), "=") && ls::std::io::KvParser::_contains(_parseParameter.getLine(), ";");
  44. }
  45. void ls::std::io::KvParser::_parse(const ls::std::core::type::byte_field &_data)
  46. {
  47. for (::std::string::size_type index = 0; index < _data.size(); index++)
  48. {
  49. ls::std::io::KvParseParameter parseParameter = ls::std::io::KvParser::_readLine(_data, index);
  50. this->_parsePair(parseParameter);
  51. index = parseParameter.getIndex();
  52. }
  53. }
  54. void ls::std::io::KvParser::_parsePair(ls::std::io::KvParseParameter &_parseParameter)
  55. {
  56. if (ls::std::io::KvParser::_lineHasPair(_parseParameter))
  57. {
  58. size_t equalSignPosition = _parseParameter.getLine().find('=');
  59. ls::std::core::type::kv_key key = _parseParameter.getLine().substr(0, equalSignPosition);
  60. ls::std::core::type::kv_value value = _parseParameter.getLine().substr(equalSignPosition + 1);
  61. value = value.substr(0, value.find(';'));
  62. this->document->addPair(ls::std::io::KvPair{key, value});
  63. }
  64. }
  65. ls::std::io::KvParseParameter ls::std::io::KvParser::_readLine(const ls::std::core::type::byte_field &_data, ::std::string::size_type _index)
  66. {
  67. ls::std::io::KvParseParameter parseParameter{};
  68. parseParameter.setLine(_data.substr(_index));
  69. if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getWindowsNewLine()))
  70. {
  71. ls::std::io::KvParser::_readLineWithWindowsLineBreak(parseParameter);
  72. }
  73. else
  74. {
  75. if (ls::std::io::KvParser::_contains(parseParameter.getLine(), ls::std::io::NewLine::getUnixNewLine()))
  76. {
  77. ls::std::io::KvParser::_readLineWithUnixLineBreak(parseParameter);
  78. }
  79. }
  80. parseParameter.setIndex(_index + parseParameter.getLine().size());
  81. return parseParameter;
  82. }
  83. void ls::std::io::KvParser::_readLineWithUnixLineBreak(ls::std::io::KvParseParameter &_parseParameter)
  84. {
  85. size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getUnixNewLine());
  86. _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition));
  87. }
  88. void ls::std::io::KvParser::_readLineWithWindowsLineBreak(ls::std::io::KvParseParameter &_parseParameter)
  89. {
  90. size_t newLinePosition = _parseParameter.getLine().find(ls::std::io::NewLine::getWindowsNewLine());
  91. _parseParameter.setLine(_parseParameter.getLine().substr(0, newLinePosition + 1));
  92. }