123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-20
- * Changed: 2023-05-16
- *
- * */
- #include <exception>
- #include <iostream>
- #include <ls-std/core/exception/FileOperationException.hpp>
- #include <ls-std/io/FileOutputStream.hpp>
- #include <ls-std/io/evaluator/FileExistenceEvaluator.hpp>
- using ls::std::core::Class;
- using ls::std::core::FileOperationException;
- using ls::std::core::type::byte_field;
- using ls::std::io::File;
- using ls::std::io::FileExistenceEvaluator;
- using ls::std::io::FileOutputStream;
- using std::cout;
- using std::endl;
- using std::exception;
- using std::ios;
- FileOutputStream::FileOutputStream(const File &_file) : Class("FileOutputStream"), file(_file)
- {
- this->_init();
- }
- FileOutputStream::FileOutputStream(const File &_file, bool _append) : Class("FileOutputStream"), append(_append), file(_file)
- {
- this->_init();
- }
- FileOutputStream::~FileOutputStream() noexcept
- {
- try
- {
- this->_close();
- }
- catch (const exception &_exception)
- {
- cout << "could not close file output stream: " << _exception.what() << endl;
- }
- }
- void FileOutputStream::close()
- {
- this->_close();
- }
- bool FileOutputStream::write(const byte_field &_data)
- {
- bool succeeded{};
- if (this->outputStream.is_open())
- {
- if (this->outputStream << _data)
- {
- succeeded = true;
- }
- }
- else
- {
- throw FileOperationException{"operation: write"};
- }
- return succeeded;
- }
- void FileOutputStream::_close()
- {
- if (this->outputStream.is_open())
- {
- this->outputStream.close();
- }
- }
- void FileOutputStream::_init()
- {
- FileExistenceEvaluator{this->file.getAbsoluteFilePath()}.evaluate();
- if (this->append)
- {
- this->outputStream.open(this->file.getAbsoluteFilePath(), ios::out | ios::app);
- }
- else
- {
- this->outputStream.open(this->file.getAbsoluteFilePath());
- }
- }
|