SerializableSectionPairRow.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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-12
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/NewLine.hpp>
  11. #include <ls-std/io/section-pair/model/SectionPairRowListValue.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairRowSingleValue.hpp>
  13. #include <ls-std/io/section-pair/serialization/SerializableSectionPairRow.hpp>
  14. ls::std::io::SerializableSectionPairRow::SerializableSectionPairRow(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value)
  15. {
  16. this->_setValue(_value);
  17. }
  18. ls::std::io::SerializableSectionPairRow::~SerializableSectionPairRow() = default;
  19. ::std::shared_ptr<ls::std::io::SectionPairRow> ls::std::io::SerializableSectionPairRow::getValue()
  20. {
  21. return this->value;
  22. }
  23. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairRow::marshal()
  24. {
  25. ls::std::core::type::byte_field data = this->_marshalKey();
  26. return data + this->value->getValue()->marshal();
  27. }
  28. void ls::std::io::SerializableSectionPairRow::unmarshal(const ls::std::core::type::byte_field &_data)
  29. {
  30. if (this->value->isSingleValue())
  31. {
  32. this->_unmarshalSingleValue(_data);
  33. }
  34. if (this->value->isList())
  35. {
  36. this->_unmarshalListValue(_data);
  37. }
  38. }
  39. ::std::string ls::std::io::SerializableSectionPairRow::_marshalKey()
  40. {
  41. ::std::string serializedKey{};
  42. if (this->value->isSingleValue())
  43. {
  44. serializedKey = this->value->getKey() + "=";
  45. }
  46. if (this->value->isList())
  47. {
  48. serializedKey = this->value->getKey() + ":" + ls::std::io::NewLine::get();
  49. }
  50. return serializedKey;
  51. }
  52. void ls::std::io::SerializableSectionPairRow::_setValue(const ::std::shared_ptr<ls::std::io::SectionPairRow> &_value)
  53. {
  54. ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
  55. this->value = _value;
  56. }
  57. void ls::std::io::SerializableSectionPairRow::_unmarshalListValue(const ls::std::core::type::byte_field &_data)
  58. {
  59. ::std::string::size_type separatorPosition = _data.find(':');
  60. ls::std::io::section_pair_identifier identifier{};
  61. if (separatorPosition != ::std::string::npos)
  62. {
  63. this->value->setKey(_data.substr(0, separatorPosition));
  64. ::std::string::size_type newLinePosition = _data.find(NewLine::get()) + (NewLine::get().size() - 1);
  65. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowListValue>(this->value->getValue())->unmarshal(_data.substr(newLinePosition + 1));
  66. }
  67. }
  68. void ls::std::io::SerializableSectionPairRow::_unmarshalSingleValue(const ls::std::core::type::byte_field &_data)
  69. {
  70. ::std::string::size_type position = _data.find('=');
  71. ls::std::io::section_pair_identifier identifier{};
  72. if (position != ::std::string::npos)
  73. {
  74. this->value->setKey(_data.substr(0, position));
  75. ::std::dynamic_pointer_cast<ls::std::io::SectionPairRowSingleValue>(this->value->getValue())->unmarshal(_data.substr(position + 1));
  76. }
  77. }