StorableFile.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-19
  6. * Changed: 2023-02-23
  7. *
  8. * */
  9. #include <ls-std/io/FileReader.hpp>
  10. #include <ls-std/io/FileWriter.hpp>
  11. #include <ls-std/io/StorableFile.hpp>
  12. using ls::std::core::type::byte_field;
  13. using ls::std::io::File;
  14. using ls::std::io::FileReader;
  15. using ls::std::io::FileWriter;
  16. using ls::std::io::StorableFile;
  17. using std::make_shared;
  18. using std::shared_ptr;
  19. using std::string;
  20. StorableFile::StorableFile(const string &_path)
  21. {
  22. this->_init(_path);
  23. }
  24. StorableFile::~StorableFile() noexcept = default;
  25. shared_ptr<File> StorableFile::getFile()
  26. {
  27. return this->file;
  28. }
  29. byte_field StorableFile::load()
  30. {
  31. FileReader reader{*this->file};
  32. return reader.read();
  33. }
  34. void StorableFile::reset(const string &_path)
  35. {
  36. this->_init(_path);
  37. }
  38. void StorableFile::save(const byte_field &_data)
  39. {
  40. FileWriter writer{*this->file};
  41. writer.write(_data);
  42. }
  43. void StorableFile::_init(const string &_path)
  44. {
  45. if (this->file == nullptr)
  46. {
  47. this->file = make_shared<File>(_path);
  48. }
  49. else
  50. {
  51. this->file->reset(_path);
  52. }
  53. }