SerializableSectionPairSection.cpp 8.0 KB

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