SerializableSectionPairSection.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-14
  6. * Changed: 2023-02-21
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp>
  11. #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
  13. #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
  14. ls::std::io::SerializableSectionPairSection::SerializableSectionPairSection(const ls::std::io::SerializableSectionPairParameter &_parameter) : ls::std::core::Class("SerializableSectionPairSection")
  15. {
  16. ::std::string message = this->getClassName() + ": model reference is null!";
  17. ls::std::core::NullPointerArgumentEvaluator(_parameter.getValue(), message).evaluate();
  18. this->parameter = _parameter;
  19. }
  20. ls::std::io::SerializableSectionPairSection::~SerializableSectionPairSection() = default;
  21. ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairSection::getValue()
  22. {
  23. return this->parameter.getValue();
  24. }
  25. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::marshal()
  26. {
  27. ls::std::core::type::byte_field serializedSection{};
  28. serializedSection += this->_marshalSectionId();
  29. serializedSection += this->_marshalRows();
  30. return serializedSection;
  31. }
  32. void ls::std::io::SerializableSectionPairSection::unmarshal(const ls::std::core::type::byte_field &_data)
  33. {
  34. ls::std::io::SectionPairSectionArgumentEvaluator{_data}.evaluate();
  35. size_t sectionHeaderSize = this->_unmarshalSectionHeader(_data);
  36. this->_unmarshalRows(_data.substr(sectionHeaderSize));
  37. }
  38. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionRow(const ls::std::core::type::byte_field &_currentRows, ls::std::io::SectionPairRowEnumType &_type)
  39. {
  40. ::std::string row{};
  41. ::std::string newLine = this->parameter.getNewLine();
  42. ::std::string firstRow = _currentRows.substr(0, _currentRows.find(newLine) + newLine.size());
  43. if (ls::std::io::SerializableSectionPairSection::_isSingleValueRow(firstRow))
  44. {
  45. row = ls::std::io::SerializableSectionPairSection::_collectSectionSingleValueRow(firstRow, _type);
  46. }
  47. if (ls::std::io::SerializableSectionPairSection::_isListValueRow(firstRow))
  48. {
  49. row = this->_collectSectionListValueRow(_currentRows, _type);
  50. }
  51. return row;
  52. }
  53. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionListValueRow(const ls::std::core::type::byte_field &_currentRows, ls::std::io::SectionPairRowEnumType &_type)
  54. {
  55. ls::std::core::type::byte_field currentRows = _currentRows;
  56. ls::std::core::type::byte_field currentRow{}, row{};
  57. ::std::string newLine = this->parameter.getNewLine();
  58. _type = SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
  59. size_t iterations{};
  60. bool isStillListRow{};
  61. do
  62. {
  63. if (currentRows.empty() && iterations > 1)
  64. {
  65. break;
  66. }
  67. ++iterations;
  68. currentRow = currentRows.substr(0, currentRows.find(newLine) + newLine.size());
  69. currentRows = currentRows.substr(currentRow.size());
  70. isStillListRow = !ls::std::io::SerializableSectionPairSection::_isStartingValueRow(currentRow) || iterations == 1;
  71. if (isStillListRow)
  72. {
  73. row += currentRow;
  74. }
  75. } while (isStillListRow);
  76. return row;
  77. }
  78. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionSingleValueRow(const ls::std::core::type::byte_field &_firstRow, ls::std::io::SectionPairRowEnumType &_type)
  79. {
  80. _type = SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
  81. return _firstRow;
  82. }
  83. size_t ls::std::io::SerializableSectionPairSection::_getNthSubStringPosition(const ls::std::core::type::byte_field &_text, const ls::std::core::type::byte_field &_subText)
  84. {
  85. size_t position = -1;
  86. size_t amount{};
  87. for (int index = 0; index < (_text.size() - _subText.size()); index++)
  88. {
  89. if (_text.substr(index, _subText.size()) == _subText)
  90. {
  91. ++amount;
  92. }
  93. if (amount == 2)
  94. {
  95. position = index;
  96. break;
  97. }
  98. }
  99. return position;
  100. }
  101. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_getSectionHeader(const ls::std::core::type::byte_field &_data)
  102. {
  103. ls::std::core::type::byte_field sectionHeader{};
  104. ::std::string newLine = this->parameter.getNewLine();
  105. size_t position = ls::std::io::SerializableSectionPairSection::_getNthSubStringPosition(_data, newLine);
  106. if (position != -1)
  107. {
  108. sectionHeader = _data.substr(0, position + 2 * newLine.size());
  109. }
  110. return sectionHeader;
  111. }
  112. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_getSectionId(const ls::std::core::type::byte_field &_sectionHeader)
  113. {
  114. ls::std::core::type::byte_field sectionId = _sectionHeader.substr(_sectionHeader.find('[') + 1);
  115. return sectionId.substr(0, sectionId.find(']'));
  116. }
  117. bool ls::std::io::SerializableSectionPairSection::_isListValueRow(const ::std::string &_currentRow)
  118. {
  119. return _currentRow.find(':') != ::std::string::npos;
  120. }
  121. bool ls::std::io::SerializableSectionPairSection::_isStartingValueRow(const ::std::string &_currentRow)
  122. {
  123. bool isSingleValue = ls::std::io::SerializableSectionPairSection::_isSingleValueRow(_currentRow);
  124. bool isListValue = ls::std::io::SerializableSectionPairSection::_isListValueRow(_currentRow);
  125. return isSingleValue || isListValue;
  126. }
  127. bool ls::std::io::SerializableSectionPairSection::_isSingleValueRow(const ::std::string &_currentRow)
  128. {
  129. return _currentRow.find('=') != ::std::string::npos;
  130. }
  131. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_marshalRows()
  132. {
  133. ls::std::core::type::byte_field serializedSectionRows{};
  134. for (const auto &_row : ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->getList())
  135. {
  136. _row->reserveNewLine(this->parameter.getNewLine());
  137. serializedSectionRows += _row->marshal();
  138. }
  139. return serializedSectionRows;
  140. }
  141. ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_marshalSectionId()
  142. {
  143. ::std::string newLine = this->parameter.getNewLine();
  144. return newLine + "[" + ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->getSectionId() + "]" + newLine + newLine;
  145. }
  146. void ls::std::io::SerializableSectionPairSection::_unmarshalRow(const ::std::string &_sectionRow, ls::std::io::SectionPairRowEnumType _type)
  147. {
  148. ls::std::io::section_pair_row_list_element row = ::std::make_shared<ls::std::io::SectionPairRow>("tmp-dir", _type);
  149. row->reserveNewLine(this->parameter.getNewLine());
  150. row->unmarshal(_sectionRow);
  151. ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->add(row);
  152. }
  153. void ls::std::io::SerializableSectionPairSection::_unmarshalRows(const ls::std::core::type::byte_field &_serializedRows)
  154. {
  155. ::std::string currentRows = _serializedRows;
  156. ls::std::io::SectionPairRowEnumType type{};
  157. while (!currentRows.empty())
  158. {
  159. ::std::string sectionRow = this->_collectSectionRow(currentRows, type);
  160. this->_unmarshalRow(sectionRow, type);
  161. currentRows = currentRows.substr(sectionRow.size());
  162. }
  163. }
  164. size_t ls::std::io::SerializableSectionPairSection::_unmarshalSectionHeader(const ls::std::core::type::byte_field &_data)
  165. {
  166. ls::std::core::type::byte_field sectionHeader = this->_getSectionHeader(_data);
  167. ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->setSectionId(ls::std::io::SerializableSectionPairSection::_getSectionId(sectionHeader));
  168. return sectionHeader.size();
  169. }