FileReader.cpp 1.2 KB

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