FileOutputStream.cpp 1.4 KB

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