FileReader.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2023-05-16
  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. using std::string;
  22. FileReader::FileReader(const File &_file) : Class("FileReader"), file(_file)
  23. {
  24. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  25. }
  26. FileReader::~FileReader() noexcept = default;
  27. byte_field FileReader::read()
  28. {
  29. ifstream inputStream{this->file.getAbsoluteFilePath(), ifstream::binary};
  30. auto length = (int) this->file.getSize();
  31. auto data = string(length, 'x');
  32. inputStream.read(data.data(), length);
  33. if (inputStream.fail())
  34. {
  35. throw FileOperationException{"operation: read"};
  36. }
  37. inputStream.close();
  38. return data;
  39. }
  40. void FileReader::reset(const File &_file)
  41. {
  42. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  43. this->file = _file;
  44. }