FileWriter.cpp 930 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2020-08-19
  7. *
  8. * */
  9. #include <fstream>
  10. #include "FileWriter.hpp"
  11. #include "../exception/FileNotFoundException.hpp"
  12. ls_std::FileWriter::FileWriter(ls_std::File &_file) : Class("FileWriter"),
  13. file(_file)
  14. {
  15. ls_std::FileWriter::_init(_file);
  16. }
  17. void ls_std::FileWriter::reset(File &_file)
  18. {
  19. ls_std::FileWriter::_init(_file);
  20. this->file = _file;
  21. }
  22. bool ls_std::FileWriter::write(const ls_std::byte* _data)
  23. {
  24. std::ofstream outputStream {};
  25. outputStream.open(this->file.getAbsoluteFilePath());
  26. bool succeeded {};
  27. if(outputStream << _data) {
  28. succeeded = true;
  29. }
  30. outputStream.close();
  31. return succeeded;
  32. }
  33. void ls_std::FileWriter::_init(File &_file)
  34. {
  35. if(!_file.exists()) {
  36. throw ls_std::FileNotFoundException {};
  37. }
  38. }