KvParser.cpp 3.1 KB

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