FileReader.cpp 1.2 KB

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