SerializableSectionPairRow.cpp 3.6 KB

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