KvParser.cpp 3.5 KB

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