/* * Author: Patrick-Christopher Mattulat * Company: Lynar Studios * E-Mail: webmaster@lynarstudios.com * Created: 2020-08-20 * Changed: 2023-05-16 * * */ #include #include #include #include #include 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()); } }