FileWriter.cpp 1.0 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: 2022-05-12
  7. *
  8. * */
  9. #include <fstream>
  10. #include <ls_std/io/FileWriter.hpp>
  11. #include <ls_std/core/exception/FileNotFoundException.hpp>
  12. #include <ls_std/core/exception/FileOperationException.hpp>
  13. ls::std::io::FileWriter::FileWriter(ls::std::io::File &_file)
  14. : ls::std::core::Class("FileWriter"),
  15. file(_file)
  16. {
  17. ls::std::io::FileWriter::_init(_file);
  18. }
  19. void ls::std::io::FileWriter::reset(ls::std::io::File &_file)
  20. {
  21. ls::std::io::FileWriter::_init(_file);
  22. this->file = _file;
  23. }
  24. bool ls::std::io::FileWriter::write(const ls::std::core::type::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::io::FileWriter::_init(ls::std::io::File &_file)
  32. {
  33. if (!_file.exists())
  34. {
  35. throw ls::std::core::FileNotFoundException{};
  36. }
  37. }