AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AFileOutputStream.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13#include <cstdio>
   14#include "IOutputStream.h"
   15#include "AUI/Core.h"
   16
   17class AString;
   18
   23class API_AUI_CORE AFileOutputStream : public IOutputStream
   24{
   25private:
   26    FILE* mFile;
   27    AString mPath;
   28
   29public:
   30    AFileOutputStream(): mFile(nullptr) {}
   31    AFileOutputStream(AString path, bool append = false);
   32
   33    virtual ~AFileOutputStream();
   34
   35    void write(const char* src, size_t size) override;
   36    void close();
   37    void open(bool append = false);
   38
   39
   40    AFileOutputStream(AFileOutputStream&& rhs) noexcept {
   41        operator=(std::move(rhs));
   42    }
   43    AFileOutputStream& operator=(AFileOutputStream&& rhs) noexcept {
   44        mFile = rhs.mFile;
   45        mPath = std::move(rhs.mPath);
   46        rhs.mFile = nullptr;
   47        return *this;
   48    }
   49
   50    FILE* nativeHandle() const {
   51        return mFile;
   52    }
   53    const AString& path() const {
   54        return mPath;
   55    }
   56
   60    class WriteException: public AIOException {
   61    private:
   62        AString mPath;
   63
   64    public:
   65        WriteException(const AString &mPath) : mPath(mPath) {}
   66
   67        const AString& getPath() const {
   68            return mPath;
   69        }
   70
   71        AString getMessage() const noexcept override {
   72            return "failed to write to file: " + mPath;
   73        }
   74    };
   75};
Opens a file for a binary write.
Definition AFileOutputStream.h:24
void write(const char *src, size_t size) override
Writes exact size bytes to stream. Blocking (waiting for write all data) is allowed.
Represents a Unicode character string.
Definition AString.h:38
Definition IOutputStream.h:18