SerializableSectionPairDocument.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. ::std::shared_ptr<ls::std::io::SectionPairDocument> document = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value);
  25. ::std::string newLine = ls::std::io::NewLine::get();
  26. ls::std::core::type::byte_field serializedDocument = document->getHeader() + newLine + newLine;
  27. for (const auto &_section : document->getSectionList())
  28. {
  29. serializedDocument += _section->marshal();
  30. }
  31. return serializedDocument;
  32. }
  33. void ls::std::io::SerializableSectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
  34. {
  35. ls::std::core::type::byte_field serializedDocument = _data;
  36. size_t headerSize = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->getHeader().size() + 2 * ls::std::io::NewLine::get().size();
  37. serializedDocument = serializedDocument.substr(headerSize);
  38. ls::std::core::type::byte_field serializedSection{};
  39. do
  40. {
  41. serializedSection = ls::std::io::SerializableSectionPairDocument::_getNextSerializedSection(serializedDocument);
  42. this->_addSection(serializedSection);
  43. serializedDocument = serializedDocument.substr(serializedSection.size());
  44. } while (!serializedDocument.empty());
  45. }
  46. void ls::std::io::SerializableSectionPairDocument::_addSection(const ls::std::core::type::byte_field &_serializedSection)
  47. {
  48. ::std::shared_ptr<ls::std::io::SectionPairSection> section = ::std::make_shared<ls::std::io::SectionPairSection>("tmp-id");
  49. section->unmarshal(_serializedSection);
  50. ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->add(section);
  51. }
  52. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::_getCurrentRow(size_t _iterations, const ls::std::core::type::byte_field &_serializedDocument)
  53. {
  54. ::std::string newLine = ls::std::io::NewLine::get();
  55. ::std::string currentRow{};
  56. if (_iterations == 1 || _serializedDocument.find('[') != ::std::string::npos)
  57. {
  58. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine + newLine) + 2 * newLine.size());
  59. }
  60. else
  61. {
  62. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine) + newLine.size());
  63. }
  64. return currentRow;
  65. }
  66. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::_getNextSerializedSection(const ls::std::core::type::byte_field &_serializedDocument)
  67. {
  68. ls::std::core::type::byte_field serializedSection{}, currentRow{};
  69. size_t iterations{};
  70. ls::std::core::type::byte_field serializedDocument = _serializedDocument;
  71. bool isNotNewSection{};
  72. ::std::string newLine = ls::std::io::NewLine::get();
  73. do
  74. {
  75. ++iterations;
  76. currentRow = ls::std::io::SerializableSectionPairDocument::_getCurrentRow(iterations, serializedDocument);
  77. isNotNewSection = ls::std::io::SerializableSectionPairDocument::_isNotNewSection(currentRow) && !serializedDocument.empty() || iterations == 1;
  78. serializedDocument = serializedDocument.substr(currentRow.size());
  79. serializedSection += currentRow;
  80. } while (isNotNewSection);
  81. return serializedDocument.empty() ? serializedSection : serializedSection.substr(0, serializedSection.size() - newLine.size());
  82. }
  83. bool ls::std::io::SerializableSectionPairDocument::_isNotNewSection(const ls::std::core::type::byte_field &_currentRow)
  84. {
  85. ::std::string newLine = ls::std::io::NewLine::get();
  86. return _currentRow.find(newLine + newLine) == ::std::string::npos;
  87. }
  88. void ls::std::io::SerializableSectionPairDocument::_setValue(const ::std::shared_ptr<ls::std::core::Class> &_value)
  89. {
  90. ls::std::core::NullPointerArgumentEvaluator{_value}.evaluate();
  91. this->value = _value;
  92. }