SerializableSectionPairRowListValue.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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-23
  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. using ls::std::core::Class;
  14. using ls::std::core::EmptyStringArgumentEvaluator;
  15. using ls::std::core::NullPointerArgumentEvaluator;
  16. using ls::std::core::type::byte_field;
  17. using ls::std::io::SectionPairRowListValue;
  18. using ls::std::io::SerializableSectionPairParameter;
  19. using ls::std::io::SerializableSectionPairRowListValue;
  20. using std::dynamic_pointer_cast;
  21. using std::shared_ptr;
  22. using std::string;
  23. SerializableSectionPairRowListValue::SerializableSectionPairRowListValue(const SerializableSectionPairParameter &_parameter) : Class("SerializableSectionPairRowListValue")
  24. {
  25. string message = this->getClassName() + ": model reference is null!";
  26. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  27. this->parameter = _parameter;
  28. }
  29. SerializableSectionPairRowListValue::~SerializableSectionPairRowListValue() noexcept = default;
  30. shared_ptr<Class> SerializableSectionPairRowListValue::getValue()
  31. {
  32. return this->parameter.getValue();
  33. }
  34. byte_field SerializableSectionPairRowListValue::marshal()
  35. {
  36. byte_field data{};
  37. for (const auto &_value : dynamic_pointer_cast<SectionPairRowListValue>(this->getValue())->getList())
  38. {
  39. data += " " + _value + this->parameter.getNewLine();
  40. }
  41. return data;
  42. }
  43. void SerializableSectionPairRowListValue::unmarshal(const byte_field &_data)
  44. {
  45. EmptyStringArgumentEvaluator{_data}.evaluate();
  46. byte_field searchText = _data;
  47. string newLine = this->parameter.getNewLine();
  48. while (!searchText.empty() && searchText != newLine)
  49. {
  50. string::size_type positionOfNewLine = searchText.find(newLine);
  51. string line = SerializableSectionPairRowListValue::_getLine(positionOfNewLine, searchText);
  52. line = line.substr(2);
  53. dynamic_pointer_cast<SectionPairRowListValue>(this->getValue())->add(line);
  54. this->_updateSearchText(positionOfNewLine, searchText);
  55. }
  56. }
  57. string SerializableSectionPairRowListValue::_getLine(string::size_type _position, const byte_field &_searchText)
  58. {
  59. string line{};
  60. if (_position != string::npos)
  61. {
  62. line = _searchText.substr(0, _position);
  63. }
  64. else
  65. {
  66. line = _searchText;
  67. }
  68. return line;
  69. }
  70. void SerializableSectionPairRowListValue::_updateSearchText(string::size_type _position, byte_field &_searchText)
  71. {
  72. if (_position != string::npos)
  73. {
  74. _searchText = _searchText.substr(_position + this->parameter.getNewLine().size());
  75. }
  76. else
  77. {
  78. _searchText = "";
  79. }
  80. }