SectionPairFileReader.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-23
  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")
  32. {
  33. this->parameter = _parameter;
  34. ConditionalFunctionExecutor{this->parameter.getFileExistenceEvaluator() == nullptr}.execute([this] { _createFileExistenceEvaluator(); });
  35. this->parameter.getFileExistenceEvaluator()->evaluate();
  36. this->_checkFileExtension();
  37. ConditionalFunctionExecutor{this->parameter.getReader() == nullptr}.execute([this] { _createReader(); });
  38. ConditionalFunctionExecutor{this->parameter.getDocument() == nullptr}.execute([this] { _createDocument(); });
  39. }
  40. SectionPairFileReader::~SectionPairFileReader() noexcept = default;
  41. shared_ptr<SectionPairDocument> SectionPairFileReader::getDocument()
  42. {
  43. return this->parameter.getDocument();
  44. }
  45. byte_field SectionPairFileReader::read()
  46. {
  47. this->parameter.getDocument()->clear();
  48. byte_field data = this->parameter.getReader()->read();
  49. this->parameter.getDocument()->unmarshal(data);
  50. return data;
  51. }
  52. void SectionPairFileReader::_checkFileExtension()
  53. {
  54. if (!SectionPairFileExtensionValidator{this->parameter.getFilePath()}.isValid())
  55. {
  56. string message = "\"" + this->parameter.getFilePath() + "\" does not have a valid section pair file extension (.txt or .sp)!";
  57. throw IllegalArgumentException{message};
  58. }
  59. }
  60. void SectionPairFileReader::_createDocument()
  61. {
  62. this->parameter.setDocument(make_shared<SectionPairDocument>());
  63. }
  64. void SectionPairFileReader::_createFileExistenceEvaluator()
  65. {
  66. this->parameter.setFileExistenceEvaluator(make_shared<FileExistenceEvaluator>(this->parameter.getFilePath()));
  67. }
  68. void SectionPairFileReader::_createReader()
  69. {
  70. File file{this->parameter.getFilePath()};
  71. this->parameter.setReader(make_shared<FileReader>(file));
  72. }