SectionPairFileReader.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2023-02-21
  6. * Changed: 2023-05-18
  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. using ls::std::core::Class;
  18. using ls::std::core::ConditionalFunctionExecutor;
  19. using ls::std::core::IllegalArgumentException;
  20. using ls::std::core::type::byte_field;
  21. using ls::std::io::File;
  22. using ls::std::io::FileExistenceEvaluator;
  23. using ls::std::io::FileReader;
  24. using ls::std::io::SectionPairDocument;
  25. using ls::std::io::SectionPairFileExtensionValidator;
  26. using ls::std::io::SectionPairFileReader;
  27. using ls::std::io::SectionPairFileReaderParameter;
  28. using std::make_shared;
  29. using std::shared_ptr;
  30. using std::string;
  31. SectionPairFileReader::SectionPairFileReader(const SectionPairFileReaderParameter &_parameter) : Class("SectionPairFileReader"), parameter(_parameter)
  32. {
  33. ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
  34. this->parameter.getFileExistenceEvaluator()->evaluate();
  35. this->_checkFileExtension();
  36. ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
  37. ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
  38. }
  39. SectionPairFileReader::~SectionPairFileReader() noexcept = default;
  40. shared_ptr<SectionPairDocument> SectionPairFileReader::getDocument() const
  41. {
  42. return this->parameter.getDocument();
  43. }
  44. byte_field SectionPairFileReader::read()
  45. {
  46. this->parameter.getDocument()->clear();
  47. byte_field data = this->parameter.getReader()->read();
  48. this->parameter.getDocument()->unmarshal(data);
  49. return data;
  50. }
  51. void SectionPairFileReader::_checkFileExtension() const
  52. {
  53. if (!SectionPairFileExtensionValidator{this->parameter.getFilePath()}.isValid())
  54. {
  55. string message = "\"" + this->parameter.getFilePath() + "\" does not have a valid section pair file extension (.txt or .sp)!";
  56. throw IllegalArgumentException{message};
  57. }
  58. }
  59. void SectionPairFileReader::_createDocument()
  60. {
  61. this->parameter.setDocument(make_shared<SectionPairDocument>());
  62. }
  63. void SectionPairFileReader::_createFileExistenceEvaluator()
  64. {
  65. this->parameter.setFileExistenceEvaluator(make_shared<FileExistenceEvaluator>(this->parameter.getFilePath()));
  66. }
  67. void SectionPairFileReader::_createReader()
  68. {
  69. File file{this->parameter.getFilePath()};
  70. this->parameter.setReader(make_shared<FileReader>(file));
  71. }