SerializableSectionPairRowListValue.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-11
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/EmptyStringArgumentEvaluator.hpp>
  10. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  11. #include <ls-std/io/NewLine.hpp>
  12. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRowListValue.hpp>
  13. ls::std::io::SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value)
  14. {
  15. this->_setValue(_value);
  16. }
  17. ls::std::io::SerializableSectionPairRowListValue::~SerializableSectionPairRowListValue() = default;
  18. ::std::shared_ptr<ls::std::io::SectionPairRowListValue> ls::std::io::SerializableSectionPairRowListValue::getValue()
  19. {
  20. return this->value;
  21. }
  22. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRowListValue::marshal()
  23. {
  24. ls::std::core::type::byte_field data{};
  25. for (const auto &_value : this->value->getList())
  26. {
  27. data += " " + _value + ls::std::io::NewLine::get();
  28. }
  29. return data;
  30. }
  31. void ls::std::io::SerializableSectionPairRowListValue::unmarshal(const ls::std::core::type::byte_field &_data)
  32. {
  33. ls::std::core::EmptyStringArgumentEvaluator{_data}.evaluate(); // TODO necessary? evaluators should be used outside of serializable
  34. ls::std::core::type::byte_field searchText = _data;
  35. while (!searchText.empty() && searchText != ls::std::io::NewLine::get())
  36. {
  37. ::std::string::size_type positionOfNewLine = searchText.find(ls::std::io::NewLine::get());
  38. ::std::string line = ls::std::io::SerializableSectionPairRowListValue::_getLine(positionOfNewLine, searchText);
  39. line = line.substr(2);
  40. this->value->add(line);
  41. ls::std::io::SerializableSectionPairRowListValue::_updateSearchText(positionOfNewLine, searchText);
  42. }
  43. }
  44. ::std::string ls::std::io::SerializableSectionPairRowListValue::_getLine(::std::string::size_type _position, const ls::std::core::type::byte_field &_searchText)
  45. {
  46. ::std::string line{};
  47. if (_position != ::std::string::npos)
  48. {
  49. line = _searchText.substr(0, _position + (ls::std::io::NewLine::get().size() - 1));
  50. }
  51. else
  52. {
  53. line = _searchText;
  54. }
  55. return line;
  56. }
  57. void ls::std::io::SerializableSectionPairRowListValue::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRowListValue> &_value)
  58. {
  59. ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
  60. this->value = _value;
  61. }
  62. void ls::std::io::SerializableSectionPairRowListValue::_updateSearchText(::std::string::size_type _position, ls::std::core::type::byte_field &_searchText)
  63. {
  64. if (_position != ::std::string::npos)
  65. {
  66. _searchText = _searchText.substr(_position + ls::std::io::NewLine::get().size());
  67. }
  68. else
  69. {
  70. _searchText = "";
  71. }
  72. }