FileReader.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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-22
  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. ls::std::io::FileReader::FileReader(ls::std::io::File &_file) : ls::std::core::Class("FileReader"), file(_file)
  14. {
  15. ls::std::io::FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  16. }
  17. ls::std::io::FileReader::~FileReader() = default;
  18. ls::std::core::type::byte_field ls::std::io::FileReader::read()
  19. {
  20. ls::std::core::type::byte *data;
  21. ::std::ifstream inputStream{this->file.getAbsoluteFilePath(), ::std::ifstream::binary};
  22. int length = (int) this->file.getSize();
  23. data = new ls::std::core::type::byte[length];
  24. inputStream.read(data, length);
  25. if (inputStream.fail())
  26. {
  27. throw ls::std::core::FileOperationException{"operation: read"};
  28. }
  29. inputStream.close();
  30. ls::std::core::type::byte_field readData = ls::std::core::type::byte_field{data, (size_t) this->file.getSize()};
  31. delete[] data;
  32. return readData;
  33. }
  34. void ls::std::io::FileReader::reset(ls::std::io::File &_file)
  35. {
  36. ls::std::io::FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  37. this->file = _file;
  38. }