FileReader.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <fstream>
  10. #include <ls-std/core/exception/FileOperationException.hpp>
  11. #include <ls-std/io/FileReader.hpp>
  12. #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
  13. using ls::std::core::Class;
  14. using ls::std::core::FileOperationException;
  15. using ls::std::core::type::byte_field;
  16. using ls::std::core::type::byte_type;
  17. using ls::std::io::File;
  18. using ls::std::io::FileExistenceEvaluator;
  19. using ls::std::io::FileReader;
  20. using std::ifstream;
  21. FileReader::FileReader(File &_file) : Class("FileReader"), file(_file)
  22. {
  23. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  24. }
  25. FileReader::~FileReader() noexcept = default;
  26. byte_field FileReader::read()
  27. {
  28. byte_type *data;
  29. ifstream inputStream{this->file.getAbsoluteFilePath(), ifstream::binary};
  30. int length = (int) this->file.getSize();
  31. data = new byte_type[length];
  32. inputStream.read(data, length);
  33. if (inputStream.fail())
  34. {
  35. throw FileOperationException{"operation: read"};
  36. }
  37. inputStream.close();
  38. byte_field readData = byte_field{data, (size_t) this->file.getSize()};
  39. delete[] data;
  40. return readData;
  41. }
  42. void FileReader::reset(File &_file)
  43. {
  44. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  45. this->file = _file;
  46. }