SerializableSectionPairSection.cpp 8.0 KB

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