SectionPairFileReader.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-21
  6. * Changed: 2023-02-21
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/io/FileReader.hpp>
  11. #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
  12. #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
  13. #include <ls-std/io/section-pair/reader/SectionPairFileReader.hpp>
  14. #include <memory>
  15. ls::std::io::SectionPairFileReader::SectionPairFileReader(SectionPairFileReaderParameter _parameter) : ls::std::core::Class("SectionPairFileReader")
  16. {
  17. ls::std::io::FileExistenceEvaluator{_parameter.getFilePath()}.evaluate();
  18. this->parameter = _parameter;
  19. ls::std::core::ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
  20. ls::std::core::ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
  21. }
  22. ls::std::io::SectionPairFileReader::~SectionPairFileReader() = default;
  23. ::std::shared_ptr<ls::std::io::SectionPairDocument> ls::std::io::SectionPairFileReader::getDocument()
  24. {
  25. return this->parameter.getDocument();
  26. }
  27. ls::std::core::type::byte_field ls::std::io::SectionPairFileReader::read()
  28. {
  29. this->parameter.getDocument()->clear();
  30. ls::std::core::type::byte_field data = this->parameter.getReader()->read();
  31. this->parameter.getDocument()->unmarshal(data);
  32. return data;
  33. }
  34. void ls::std::io::SectionPairFileReader::_createDocument()
  35. {
  36. this->parameter.setDocument(::std::make_shared<ls::std::io::SectionPairDocument>());
  37. }
  38. void ls::std::io::SectionPairFileReader::_createReader()
  39. {
  40. ls::std::io::File file{this->parameter.getFilePath()};
  41. this->parameter.setReader(::std::make_shared<ls::std::io::FileReader>(file));
  42. }