SectionPairFileReader.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(const ls::std::io::SectionPairFileReaderParameter &_parameter) : ls::std::core::Class("SectionPairFileReader")
  16. {
  17. this->parameter = _parameter;
  18. ls::std::core::ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
  19. this->parameter.getFileExistenceEvaluator()->evaluate();
  20. ls::std::core::ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
  21. ls::std::core::ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
  22. }
  23. ls::std::io::SectionPairFileReader::~SectionPairFileReader() = default;
  24. ::std::shared_ptr<ls::std::io::SectionPairDocument> ls::std::io::SectionPairFileReader::getDocument()
  25. {
  26. return this->parameter.getDocument();
  27. }
  28. ls::std::core::type::byte_field ls::std::io::SectionPairFileReader::read()
  29. {
  30. this->parameter.getDocument()->clear();
  31. ls::std::core::type::byte_field data = this->parameter.getReader()->read();
  32. this->parameter.getDocument()->unmarshal(data);
  33. return data;
  34. }
  35. void ls::std::io::SectionPairFileReader::_createDocument()
  36. {
  37. this->parameter.setDocument(::std::make_shared<ls::std::io::SectionPairDocument>());
  38. }
  39. void ls::std::io::SectionPairFileReader::_createFileExistenceEvaluator()
  40. {
  41. this->parameter.setFileExistenceEvaluator(::std::make_shared<FileExistenceEvaluator>(this->parameter.getFilePath()));
  42. }
  43. void ls::std::io::SectionPairFileReader::_createReader()
  44. {
  45. ls::std::io::File file{this->parameter.getFilePath()};
  46. this->parameter.setReader(::std::make_shared<ls::std::io::FileReader>(file));
  47. }