StorableFile.cpp 1.1 KB

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