StorableFile.cpp 1.1 KB

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