123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2023-02-14
- * Changed: 2023-02-22
- *
- * */
- #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
- #include <ls-std/io/section-pair/evaluator/SectionPairSectionArgumentEvaluator.hpp>
- #include <ls-std/io/section-pair/model/SectionPairRow.hpp>
- #include <ls-std/io/section-pair/model/SectionPairSection.hpp>
- #include <ls-std/io/section-pair/serialization/SerializableSectionPairSection.hpp>
- ls::std::io::SerializableSectionPairSection::SerializableSectionPairSection(const ls::std::io::SerializableSectionPairParameter &_parameter) : ls::std::core::Class("SerializableSectionPairSection")
- {
- ::std::string message = this->getClassName() + ": model reference is null!";
- ls::std::core::NullPointerArgumentEvaluator(_parameter.getValue(), message).evaluate();
- this->parameter = _parameter;
- }
- ls::std::io::SerializableSectionPairSection::~SerializableSectionPairSection() noexcept = default;
- ::std::shared_ptr<ls::std::core::Class> ls::std::io::SerializableSectionPairSection::getValue()
- {
- return this->parameter.getValue();
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::marshal()
- {
- ls::std::core::type::byte_field serializedSection{};
- serializedSection += this->_marshalSectionId();
- serializedSection += this->_marshalRows();
- return serializedSection;
- }
- void ls::std::io::SerializableSectionPairSection::unmarshal(const ls::std::core::type::byte_field &_data)
- {
- ls::std::io::SectionPairSectionArgumentEvaluator{_data}.evaluate();
- size_t sectionHeaderSize = this->_unmarshalSectionHeader(_data);
- this->_unmarshalRows(_data.substr(sectionHeaderSize));
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionRow(const ls::std::core::type::byte_field &_currentRows, ls::std::io::SectionPairRowEnumType &_type)
- {
- ::std::string row{};
- ::std::string newLine = this->parameter.getNewLine();
- ::std::string firstRow = _currentRows.substr(0, _currentRows.find(newLine) + newLine.size());
- if (ls::std::io::SerializableSectionPairSection::_isSingleValueRow(firstRow))
- {
- row = ls::std::io::SerializableSectionPairSection::_collectSectionSingleValueRow(firstRow, _type);
- }
- if (ls::std::io::SerializableSectionPairSection::_isListValueRow(firstRow))
- {
- row = this->_collectSectionListValueRow(_currentRows, _type);
- }
- return row;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionListValueRow(const ls::std::core::type::byte_field &_currentRows, ls::std::io::SectionPairRowEnumType &_type)
- {
- ls::std::core::type::byte_field currentRows = _currentRows;
- ls::std::core::type::byte_field currentRow{}, row{};
- ::std::string newLine = this->parameter.getNewLine();
- _type = ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_LIST_VALUE;
- size_t iterations{};
- bool isStillListRow{};
- do
- {
- if (currentRows.empty() && iterations > 1)
- {
- break;
- }
- ++iterations;
- currentRow = currentRows.substr(0, currentRows.find(newLine) + newLine.size());
- currentRows = currentRows.substr(currentRow.size());
- isStillListRow = !ls::std::io::SerializableSectionPairSection::_isStartingValueRow(currentRow) || iterations == 1;
- if (isStillListRow)
- {
- row += currentRow;
- }
- } while (isStillListRow);
- return row;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_collectSectionSingleValueRow(const ls::std::core::type::byte_field &_firstRow, ls::std::io::SectionPairRowEnumType &_type)
- {
- _type = ls::std::io::SectionPairRowEnumType::SECTION_PAIR_ROW_SINGLE_VALUE;
- return _firstRow;
- }
- size_t ls::std::io::SerializableSectionPairSection::_getNthSubStringPosition(const ls::std::core::type::byte_field &_text, const ls::std::core::type::byte_field &_subText)
- {
- size_t position = -1;
- size_t amount{};
- for (int index = 0; index < (_text.size() - _subText.size()); index++)
- {
- if (_text.substr(index, _subText.size()) == _subText)
- {
- ++amount;
- }
- if (amount == 2)
- {
- position = index;
- break;
- }
- }
- return position;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_getSectionHeader(const ls::std::core::type::byte_field &_data)
- {
- ls::std::core::type::byte_field sectionHeader{};
- ::std::string newLine = this->parameter.getNewLine();
- size_t position = ls::std::io::SerializableSectionPairSection::_getNthSubStringPosition(_data, newLine);
- if (position != -1)
- {
- sectionHeader = _data.substr(0, position + 2 * newLine.size());
- }
- return sectionHeader;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_getSectionId(const ls::std::core::type::byte_field &_sectionHeader)
- {
- ls::std::core::type::byte_field sectionId = _sectionHeader.substr(_sectionHeader.find('[') + 1);
- return sectionId.substr(0, sectionId.find(']'));
- }
- bool ls::std::io::SerializableSectionPairSection::_isListValueRow(const ::std::string &_currentRow)
- {
- return _currentRow.find(':') != ::std::string::npos;
- }
- bool ls::std::io::SerializableSectionPairSection::_isStartingValueRow(const ::std::string &_currentRow)
- {
- bool isSingleValue = ls::std::io::SerializableSectionPairSection::_isSingleValueRow(_currentRow);
- bool isListValue = ls::std::io::SerializableSectionPairSection::_isListValueRow(_currentRow);
- return isSingleValue || isListValue;
- }
- bool ls::std::io::SerializableSectionPairSection::_isSingleValueRow(const ::std::string &_currentRow)
- {
- return _currentRow.find('=') != ::std::string::npos;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_marshalRows()
- {
- ls::std::core::type::byte_field serializedSectionRows{};
- for (const auto &_row : ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->getList())
- {
- _row->reserveNewLine(this->parameter.getNewLine());
- serializedSectionRows += _row->marshal();
- }
- return serializedSectionRows;
- }
- ls::std::core::type::byte_field ls::std::io::SerializableSectionPairSection::_marshalSectionId()
- {
- ::std::string newLine = this->parameter.getNewLine();
- return newLine + "[" + ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->getSectionId() + "]" + newLine + newLine;
- }
- void ls::std::io::SerializableSectionPairSection::_unmarshalRow(const ::std::string &_sectionRow, ls::std::io::SectionPairRowEnumType _type)
- {
- ls::std::io::section_pair_row_list_element row = ::std::make_shared<ls::std::io::SectionPairRow>("tmp-dir", _type);
- row->reserveNewLine(this->parameter.getNewLine());
- row->unmarshal(_sectionRow);
- ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->add(row);
- }
- void ls::std::io::SerializableSectionPairSection::_unmarshalRows(const ls::std::core::type::byte_field &_serializedRows)
- {
- ::std::string currentRows = _serializedRows;
- ls::std::io::SectionPairRowEnumType type{};
- while (!currentRows.empty())
- {
- ::std::string sectionRow = this->_collectSectionRow(currentRows, type);
- this->_unmarshalRow(sectionRow, type);
- currentRows = currentRows.substr(sectionRow.size());
- }
- }
- size_t ls::std::io::SerializableSectionPairSection::_unmarshalSectionHeader(const ls::std::core::type::byte_field &_data)
- {
- ls::std::core::type::byte_field sectionHeader = this->_getSectionHeader(_data);
- ::std::dynamic_pointer_cast<ls::std::io::SectionPairSection>(this->parameter.getValue())->setSectionId(ls::std::io::SerializableSectionPairSection::_getSectionId(sectionHeader));
- return sectionHeader.size();
- }
|