FileWriter.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-17
  6. * Changed: 2021-04-23
  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. bool succeeded;
  29. if (outputStream << _data)
  30. {
  31. succeeded = true;
  32. }
  33. else
  34. {
  35. throw ls_std::FileOperationException{};
  36. }
  37. outputStream.close();
  38. return succeeded;
  39. }
  40. void ls_std::FileWriter::_init(ls_std::File &_file)
  41. {
  42. if (!_file.exists())
  43. {
  44. throw ls_std::FileNotFoundException{};
  45. }
  46. }