StorableFile.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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-19
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <ls-std/io/FileReader.hpp>
  11. #include <ls-std/io/FileWriter.hpp>
  12. #include <ls-std/io/StorableFile.hpp>
  13. using ls::standard::core::type::byte_field;
  14. using ls::standard::io::File;
  15. using ls::standard::io::FileReader;
  16. using ls::standard::io::FileWriter;
  17. using ls::standard::io::StorableFile;
  18. using std::make_shared;
  19. using std::shared_ptr;
  20. using std::string;
  21. StorableFile::StorableFile(const string &_path)
  22. {
  23. this->_init(_path);
  24. }
  25. StorableFile::~StorableFile() noexcept = default;
  26. shared_ptr<File> StorableFile::getFile() const
  27. {
  28. return this->file;
  29. }
  30. byte_field StorableFile::load()
  31. {
  32. FileReader reader{*this->file};
  33. return reader.read();
  34. }
  35. void StorableFile::reset(const string &_path)
  36. {
  37. this->_init(_path);
  38. }
  39. void StorableFile::save(const byte_field &_data)
  40. {
  41. FileWriter writer{*this->file};
  42. writer.write(_data);
  43. }
  44. void StorableFile::_init(const string &_path)
  45. {
  46. if (this->file == nullptr)
  47. {
  48. this->file = make_shared<File>(_path);
  49. }
  50. else
  51. {
  52. this->file->reset(_path);
  53. }
  54. }