SerializableSectionPairDocument.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-16
  6. * Changed: 2023-05-19
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
  11. #include <ls-std/io/section-pair/serialization/SerializableSectionPairDocument.hpp>
  12. #include <string>
  13. using ls::std::core::Class;
  14. using ls::std::core::NullPointerArgumentEvaluator;
  15. using ls::std::core::type::byte_field;
  16. using ls::std::io::SectionPairDocument;
  17. using ls::std::io::SectionPairSection;
  18. using ls::std::io::SerializableSectionPairDocument;
  19. using ls::std::io::SerializableSectionPairParameter;
  20. using std::dynamic_pointer_cast;
  21. using std::make_shared;
  22. using std::shared_ptr;
  23. using std::string;
  24. using std::string_view;
  25. SerializableSectionPairDocument::SerializableSectionPairDocument(const SerializableSectionPairParameter &_parameter) : Class("SerializableSectionPairDocument"), parameter(_parameter)
  26. {
  27. string message = this->getClassName() + ": model reference is null!";
  28. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  29. }
  30. SerializableSectionPairDocument::~SerializableSectionPairDocument() noexcept = default;
  31. shared_ptr<Class> SerializableSectionPairDocument::getValue() const
  32. {
  33. return this->parameter.getValue();
  34. }
  35. byte_field SerializableSectionPairDocument::marshal()
  36. {
  37. shared_ptr<SectionPairDocument> document = dynamic_pointer_cast<SectionPairDocument>(this->parameter.getValue());
  38. string newLine = this->parameter.getNewLine();
  39. byte_field serializedDocument = document->getHeader() + newLine;
  40. for (const auto &_section : document->getSectionList())
  41. {
  42. _section->reserveNewLine(this->parameter.getNewLine());
  43. serializedDocument += _section->marshal();
  44. }
  45. return serializedDocument;
  46. }
  47. void SerializableSectionPairDocument::unmarshal(const byte_field &_data)
  48. {
  49. byte_field serializedDocument = _data;
  50. size_t headerSize = dynamic_pointer_cast<SectionPairDocument>(this->parameter.getValue())->getHeader().size() + this->parameter.getNewLine().size();
  51. serializedDocument = serializedDocument.substr(headerSize);
  52. byte_field serializedSection{};
  53. do
  54. {
  55. serializedSection = this->_getNextSerializedSection(serializedDocument);
  56. this->_addSection(serializedSection);
  57. serializedDocument = serializedDocument.substr(serializedSection.size());
  58. } while (!serializedDocument.empty());
  59. }
  60. void SerializableSectionPairDocument::_addSection(const byte_field &_serializedSection) const
  61. {
  62. auto section = make_shared<SectionPairSection>("tmp-id");
  63. section->reserveNewLine(this->parameter.getNewLine());
  64. section->unmarshal(_serializedSection);
  65. dynamic_pointer_cast<SectionPairDocument>(this->parameter.getValue())->add(section);
  66. }
  67. byte_field SerializableSectionPairDocument::_getCurrentRow(size_t _iterations, string_view _serializedDocument) const
  68. {
  69. string newLine = this->parameter.getNewLine();
  70. string currentRow{};
  71. if (_iterations == 1 || _serializedDocument.find('[') != string::npos)
  72. {
  73. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine + newLine) + 2 * newLine.size());
  74. }
  75. else
  76. {
  77. currentRow = _serializedDocument.substr(0, _serializedDocument.find(newLine) + newLine.size());
  78. }
  79. return currentRow;
  80. }
  81. byte_field SerializableSectionPairDocument::_getNextSerializedSection(const byte_field &_serializedDocument) const
  82. {
  83. byte_field serializedSection{};
  84. byte_field currentRow{};
  85. size_t iterations{};
  86. byte_field serializedDocument = _serializedDocument;
  87. bool isNotNewSection{};
  88. string newLine = this->parameter.getNewLine();
  89. do
  90. {
  91. ++iterations;
  92. currentRow = this->_getCurrentRow(iterations, serializedDocument);
  93. isNotNewSection = this->_isNotNewSection(currentRow) && !serializedDocument.empty() || iterations == 1;
  94. serializedDocument = serializedDocument.substr(currentRow.size());
  95. serializedSection += currentRow;
  96. } while (isNotNewSection);
  97. return serializedDocument.empty() ? serializedSection : serializedSection.substr(0, serializedSection.size() - newLine.size());
  98. }
  99. bool SerializableSectionPairDocument::_isNotNewSection(string_view _currentRow) const
  100. {
  101. string newLine = this->parameter.getNewLine();
  102. return _currentRow.find(newLine + newLine) == string::npos;
  103. }