FileReader.cpp 1.5 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: 2023-02-07
  7. *
  8. * */
  9. #include <fstream>
  10. #include <ls-std/core/exception/FileNotFoundException.hpp>
  11. #include <ls-std/core/exception/FileOperationException.hpp>
  12. #include <ls-std/io/FileReader.hpp>
  13. ls::std::io::FileReader::FileReader(ls::std::io::File &_file) : ls::std::core::Class("FileReader"), file(_file)
  14. {
  15. ls::std::io::FileReader::_init(_file);
  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::FileReader::_init(_file);
  37. this->file = _file;
  38. }
  39. void ls::std::io::FileReader::_init(ls::std::io::File &_file)
  40. {
  41. if (!_file.exists())
  42. {
  43. throw ls::std::core::FileNotFoundException{"name: " + _file.getAbsoluteFilePath()};
  44. }
  45. }