FileOutputStream.cpp 1.6 KB

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