StorableFile.cpp 1.0 KB

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