FileWriter.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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-07
  7. *
  8. * */
  9. #include <fstream>
  10. #include <ls-std/core/exception/FileNotFoundException.hpp>
  11. #include <ls-std/io/FileWriter.hpp>
  12. ls::std::io::FileWriter::FileWriter(ls::std::io::File &_file) : ls::std::core::Class("FileWriter"), file(_file)
  13. {
  14. ls::std::io::FileWriter::_init(_file);
  15. }
  16. ls::std::io::FileWriter::~FileWriter() = default;
  17. void ls::std::io::FileWriter::reset(ls::std::io::File &_file)
  18. {
  19. ls::std::io::FileWriter::_init(_file);
  20. this->file = _file;
  21. }
  22. bool ls::std::io::FileWriter::write(const ls::std::core::type::byte_field &_data)
  23. {
  24. ::std::ofstream outputStream{};
  25. outputStream.open(this->file.getAbsoluteFilePath());
  26. outputStream << _data;
  27. return !outputStream.fail();
  28. }
  29. void ls::std::io::FileWriter::_init(ls::std::io::File &_file)
  30. {
  31. if (!_file.exists())
  32. {
  33. throw ls::std::core::FileNotFoundException{"name: " + _file.getAbsoluteFilePath()};
  34. }
  35. }