FileOutputStream.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <ls-std/core/exception/FileOperationException.hpp>
  10. #include <ls-std/io/FileOutputStream.hpp>
  11. #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
  12. ls::std::io::FileOutputStream::FileOutputStream(ls::std::io::File &_file) : ls::std::core::Class("FileOutputStream"), file(_file)
  13. {
  14. this->_init();
  15. }
  16. ls::std::io::FileOutputStream::FileOutputStream(ls::std::io::File &_file, bool _append) : ls::std::core::Class("FileOutputStream"), append(_append), file(_file)
  17. {
  18. this->_init();
  19. }
  20. ls::std::io::FileOutputStream::~FileOutputStream() noexcept
  21. {
  22. this->_close();
  23. }
  24. void ls::std::io::FileOutputStream::close()
  25. {
  26. this->_close();
  27. }
  28. bool ls::std::io::FileOutputStream::write(const ls::std::core::type::byte_field &_data)
  29. {
  30. bool succeeded{};
  31. if (this->outputStream.is_open())
  32. {
  33. if (this->outputStream << _data)
  34. {
  35. succeeded = true;
  36. }
  37. }
  38. else
  39. {
  40. throw ls::std::core::FileOperationException{"operation: write"};
  41. }
  42. return succeeded;
  43. }
  44. void ls::std::io::FileOutputStream::_close()
  45. {
  46. if (this->outputStream.is_open())
  47. {
  48. this->outputStream.close();
  49. }
  50. }
  51. void ls::std::io::FileOutputStream::_init()
  52. {
  53. ls::std::io::FileExistenceEvaluator{this->file.getAbsoluteFilePath()}.evaluate();
  54. if (this->append)
  55. {
  56. this->outputStream.open(this->file.getAbsoluteFilePath(), ::std::ios::out | ::std::ios::app);
  57. }
  58. else
  59. {
  60. this->outputStream.open(this->file.getAbsoluteFilePath());
  61. }
  62. }