FileWriter.cpp 1.1 KB

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