FileReader.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2020-08-25
  7. *
  8. * */
  9. #include <fstream>
  10. #include "FileReader.hpp"
  11. #include "../exception/FileNotFoundException.hpp"
  12. #include "../exception/FileOperationException.hpp"
  13. ls_std::FileReader::FileReader(File &_file) : 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. return ls_std::byte_field {data, (size_t) this->file.getSize()};
  30. }
  31. void ls_std::FileReader::reset(File &_file)
  32. {
  33. ls_std::FileReader::_init(_file);
  34. this->file = _file;
  35. }
  36. void ls_std::FileReader::_init(File &_file)
  37. {
  38. if(!_file.exists()) {
  39. throw ls_std::FileNotFoundException {};
  40. }
  41. }