FileReader.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-08-17
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <fstream>
  11. #include <limits>
  12. #include <ls-std/core/exception/FileOperationException.hpp>
  13. #include <ls-std/io/FileReader.hpp>
  14. #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
  15. #include <stdexcept>
  16. using ls::standard::core::Class;
  17. using ls::standard::core::FileOperationException;
  18. using ls::standard::core::type::byte_field;
  19. using ls::standard::core::type::byte_type;
  20. using ls::standard::io::File;
  21. using ls::standard::io::FileExistenceEvaluator;
  22. using ls::standard::io::FileReader;
  23. using std::ifstream;
  24. using std::numeric_limits;
  25. using std::overflow_error;
  26. using std::streamsize;
  27. using std::string;
  28. FileReader::FileReader(const File &_file) : Class("FileReader"), file(_file)
  29. {
  30. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  31. }
  32. FileReader::~FileReader() noexcept = default;
  33. byte_field FileReader::read()
  34. {
  35. ifstream inputStream{this->file.getAbsoluteFilePath(), ifstream::binary};
  36. const auto length = this->file.getSize();
  37. auto data = string(length, 'x');
  38. if (length > static_cast<size_t>(numeric_limits<streamsize>::max()))
  39. {
  40. throw overflow_error("file size too large to read");
  41. }
  42. inputStream.read(data.data(), static_cast<streamsize>(length));
  43. if (inputStream.fail())
  44. {
  45. throw FileOperationException{"operation: read"};
  46. }
  47. inputStream.close();
  48. return data;
  49. }
  50. void FileReader::reset(const File &_file)
  51. {
  52. FileExistenceEvaluator{_file.getAbsoluteFilePath()}.evaluate();
  53. this->file = _file;
  54. }