SerializableSectionPairDocument.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-16
  6. * Changed: 2023-02-16
  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/SectionPairDocument.hpp>
  12. #include <ls-std/io/section-pair/serialization/SerializableSectionPairDocument.hpp>
  13. ls::std::io::SerializableSectionPairDocument::SerializableSectionPairDocument(const ::std::shared_ptr<ls::std::core::Class> &_value)
  14. {
  15. this->_setValue(_value);
  16. }
  17. ls::std::io::SerializableSectionPairDocument::~SerializableSectionPairDocument() = default;
  18. ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairDocument::getValue()
  19. {
  20. return this->value;
  21. }
  22. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::marshal()
  23. {
  24. ls::std::core::type::byte_field serializedDocument{};
  25. for (const auto &_section : ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->getSectionList())
  26. {
  27. serializedDocument += _section->marshal();
  28. }
  29. return serializedDocument;
  30. }
  31. void ls::std::io::SerializableSectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
  32. {
  33. ls::std::core::type::byte_field serializedDocument = _data;
  34. ls::std::core::type::byte_field serializedSection{};
  35. do
  36. {
  37. serializedSection = ls::std::io::SerializableSectionPairDocument::_getNextSerializedSection(serializedDocument);
  38. this->_addSection(serializedSection);
  39. serializedDocument = serializedDocument.substr(serializedSection.size());
  40. } while (!serializedDocument.empty());
  41. }
  42. void ls::std::io::SerializableSectionPairDocument::_addSection(const ls::std::core::type::byte_field &_serializedSection)
  43. {
  44. ::std::shared_ptr<ls::std::io::SectionPairSection> section = ::std::make_shared<ls::std::io::SectionPairSection>("tmp-id");
  45. section->unmarshal(_serializedSection);
  46. ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->add(section);
  47. }
  48. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::_getCurrentRow(size_t _iterations, const ls::std::core::type::byte_field &_serializedDocument)
  49. {
  50. ::std::string newLine = ls::std::io::NewLine::get();
  51. ::std::string currentRow{};
  52. if (_iterations == 1 || _serializedDocument.find('[') != ::std::string::npos)
  53. {
  54. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine + newLine) + 2 * newLine.size());
  55. }
  56. else
  57. {
  58. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine) + newLine.size());
  59. }
  60. return currentRow;
  61. }
  62. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::_getNextSerializedSection(const ls::std::core::type::byte_field &_serializedDocument)
  63. {
  64. ls::std::core::type::byte_field serializedSection{}, currentRow{};
  65. size_t iterations{};
  66. ls::std::core::type::byte_field serializedDocument = _serializedDocument;
  67. bool isNotNewSection{};
  68. ::std::string newLine = ls::std::io::NewLine::get();
  69. do
  70. {
  71. ++iterations;
  72. currentRow = ls::std::io::SerializableSectionPairDocument::_getCurrentRow(iterations, serializedDocument);
  73. isNotNewSection = ls::std::io::SerializableSectionPairDocument::_isNotNewSection(currentRow) && !serializedDocument.empty() || iterations == 1;
  74. serializedDocument = serializedDocument.substr(currentRow.size());
  75. serializedSection += currentRow;
  76. } while (isNotNewSection);
  77. return serializedDocument.empty() ? serializedSection : serializedSection.substr(0, serializedSection.size() - newLine.size());
  78. }
  79. bool ls::std::io::SerializableSectionPairDocument::_isNotNewSection(const ls::std::core::type::byte_field &_currentRow)
  80. {
  81. ::std::string newLine = ls::std::io::NewLine::get();
  82. return _currentRow.find(newLine + newLine) == ::std::string::npos;
  83. }
  84. void ls::std::io::SerializableSectionPairDocument::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
  85. {
  86. ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
  87. this->value = _value;
  88. }