FileWriter.cpp 1.1 KB

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