FileReader.cpp 1.4 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: 2022-05-12
  7. *
  8. * */
  9. #include <fstream>
  10. #include <ls_std/io/FileReader.hpp>
  11. #include <ls_std/core/exception/FileNotFoundException.hpp>
  12. #include <ls_std/core/exception/FileOperationException.hpp>
  13. ls::std::io::FileReader::FileReader(ls::std::io::File &_file)
  14. : ls::std::core::Class("FileReader"),
  15. file(_file)
  16. {
  17. ls::std::io::FileReader::_init(_file);
  18. }
  19. ls::std::core::type::byte_field ls::std::io::FileReader::read()
  20. {
  21. ls::std::core::type::byte *data;
  22. ::std::ifstream inputStream{this->file.getAbsoluteFilePath(), ::std::ifstream::binary};
  23. int length = (int) this->file.getSize();
  24. data = new ls::std::core::type::byte[length];
  25. inputStream.read(data, length);
  26. if (inputStream.fail())
  27. {
  28. throw ls::std::core::FileOperationException{};
  29. }
  30. inputStream.close();
  31. ls::std::core::type::byte_field readData = ls::std::core::type::byte_field{data, (size_t) this->file.getSize()};
  32. delete[] data;
  33. return readData;
  34. }
  35. void ls::std::io::FileReader::reset(ls::std::io::File &_file)
  36. {
  37. ls::std::io::FileReader::_init(_file);
  38. this->file = _file;
  39. }
  40. void ls::std::io::FileReader::_init(ls::std::io::File &_file)
  41. {
  42. if (!_file.exists())
  43. {
  44. throw ls::std::core::FileNotFoundException{};
  45. }
  46. }