SerializableSectionPairDocument.cpp 4.5 KB

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