SerializableSectionPairDocument.cpp 4.4 KB

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