SerializableSectionPairDocument.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-23
  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. SerializableSectionPairDocument::SerializableSectionPairDocument(const SerializableSectionPairParameter &_parameter) : Class("SerializableSectionPairDocument")
  25. {
  26. string message = this->getClassName() + ": model reference is null!";
  27. NullPointerArgumentEvaluator{_parameter.getValue(), message}.evaluate();
  28. this->parameter = _parameter;
  29. }
  30. SerializableSectionPairDocument::~SerializableSectionPairDocument() noexcept = default;
  31. shared_ptr<Class> SerializableSectionPairDocument::getValue()
  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)
  61. {
  62. shared_ptr<SectionPairSection> 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, const byte_field &_serializedDocument)
  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)
  82. {
  83. byte_field serializedSection{}, currentRow{};
  84. size_t iterations{};
  85. byte_field serializedDocument = _serializedDocument;
  86. bool isNotNewSection{};
  87. string newLine = this->parameter.getNewLine();
  88. do
  89. {
  90. ++iterations;
  91. currentRow = this->_getCurrentRow(iterations, serializedDocument);
  92. isNotNewSection = this->_isNotNewSection(currentRow) && !serializedDocument.empty() || iterations == 1;
  93. serializedDocument = serializedDocument.substr(currentRow.size());
  94. serializedSection += currentRow;
  95. } while (isNotNewSection);
  96. return serializedDocument.empty() ? serializedSection : serializedSection.substr(0, serializedSection.size() - newLine.size());
  97. }
  98. bool SerializableSectionPairDocument::_isNotNewSection(const byte_field &_currentRow)
  99. {
  100. string newLine = this->parameter.getNewLine();
  101. return _currentRow.find(newLine + newLine) == string::npos;
  102. }