FileWriter.cpp 974 B

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