SectionPairFileReader.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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-22
  7. *
  8. * */
  9. #include <ls-std/core/ConditionalFunctionExecutor.hpp>
  10. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  11. #include <ls-std/io/FileReader.hpp>
  12. #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
  13. #include <ls-std/io/section-pair/model/SectionPairDocument.hpp>
  14. #include <ls-std/io/section-pair/reader/SectionPairFileReader.hpp>
  15. #include <ls-std/io/section-pair/validator/SectionPairFileExtensionValidator.hpp>
  16. #include <memory>
  17. ls::std::io::SectionPairFileReader::SectionPairFileReader(const ls::std::io::SectionPairFileReaderParameter &_parameter) : ls::std::core::Class("SectionPairFileReader")
  18. {
  19. this->parameter = _parameter;
  20. ls::std::core::ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
  21. this->parameter.getFileExistenceEvaluator()->evaluate();
  22. this->_checkFileExtension();
  23. ls::std::core::ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
  24. ls::std::core::ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
  25. }
  26. ls::std::io::SectionPairFileReader::~SectionPairFileReader() noexcept = default;
  27. ::std::shared_ptr<ls::std::io::SectionPairDocument> ls::std::io::SectionPairFileReader::getDocument()
  28. {
  29. return this->parameter.getDocument();
  30. }
  31. ls::std::core::type::byte_field ls::std::io::SectionPairFileReader::read()
  32. {
  33. this->parameter.getDocument()->clear();
  34. ls::std::core::type::byte_field data = this->parameter.getReader()->read();
  35. this->parameter.getDocument()->unmarshal(data);
  36. return data;
  37. }
  38. void ls::std::io::SectionPairFileReader::_checkFileExtension()
  39. {
  40. if (!ls::std::io::SectionPairFileExtensionValidator{this->parameter.getFilePath()}.isValid())
  41. {
  42. ::std::string message = "\"" + this->parameter.getFilePath() + "\" does not have a valid section pair file extension (.txt or .sp)!";
  43. throw ls::std::core::IllegalArgumentException{message};
  44. }
  45. }
  46. void ls::std::io::SectionPairFileReader::_createDocument()
  47. {
  48. this->parameter.setDocument(::std::make_shared<ls::std::io::SectionPairDocument>());
  49. }
  50. void ls::std::io::SectionPairFileReader::_createFileExistenceEvaluator()
  51. {
  52. this->parameter.setFileExistenceEvaluator(::std::make_shared<FileExistenceEvaluator>(this->parameter.getFilePath()));
  53. }
  54. void ls::std::io::SectionPairFileReader::_createReader()
  55. {
  56. ls::std::io::File file{this->parameter.getFilePath()};
  57. this->parameter.setReader(::std::make_shared<ls::std::io::FileReader>(file));
  58. }