SerializableSectionPairRowListValue.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-11
  6. * Changed: 2023-02-17
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  12. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
  13. ls::std::io::SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const ls::std::io::SerializableSectionPairParameter &_parameter) : ls::std::core::Class("SerializableSectionPairRowListValue")
  14. {
  15. ::std::string message = this->getClassName() + ": model reference is null!";
  16. ls::std::core::NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  17. this->parameter = _parameter;
  18. }
  19. ls::std::io::SerializableSectionPairRowListValue::~SerializableSectionPairRowListValue() = default;
  20. ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairRowListValue::getValue()
  21. {
  22. return this->parameter.getValue();
  23. }
  24. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRowListValue::marshal()
  25. {
  26. ls::std::core::type::byte_field data{};
  27. for (const auto &_value : ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->getValue())->getList())
  28. {
  29. data += " " + _value + this->parameter.getNewLine();
  30. }
  31. return data;
  32. }
  33. void ls::std::io::SerializableSectionPairRowListValue::unmarshal(const ls::std::core::type::byte_field &_data)
  34. {
  35. ls::std::core::EmptyStringArgumentEvaluator{_data}.evaluate();
  36. ls::std::core::type::byte_field searchText = _data;
  37. ::std::string newLine = this->parameter.getNewLine();
  38. while (!searchText.empty() && searchText != newLine)
  39. {
  40. ::std::string::size_type positionOfNewLine = searchText.find(newLine);
  41. ::std::string line = ls::std::io::SerializableSectionPairRowListValue::_getLine(positionOfNewLine, searchText);
  42. line = line.substr(2);
  43. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->getValue())->add(line);
  44. this->_updateSearchText(positionOfNewLine, searchText);
  45. }
  46. }
  47. ::std::string ls::std::io::SerializableSectionPairRowListValue::_getLine(::std::string::size_type _position, const ls::std::core::type::byte_field &_searchText)
  48. {
  49. ::std::string line{};
  50. if (_position != ::std::string::npos)
  51. {
  52. line = _searchText.substr(0, _position);
  53. }
  54. else
  55. {
  56. line = _searchText;
  57. }
  58. return line;
  59. }
  60. void ls::std::io::SerializableSectionPairRowListValue::_updateSearchText(::std::string::size_type _position, ls::std::core::type::byte_field &_searchText)
  61. {
  62. if (_position != ::std::string::npos)
  63. {
  64. _searchText = _searchText.substr(_position + this->parameter.getNewLine().size());
  65. }
  66. else
  67. {
  68. _searchText = "";
  69. }
  70. }