// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #pragma once #include #include #include #include #include #include "common/types.h" namespace Common::FS { enum class OpenMode : u32 { Read = 0x1, Write = 0x2, ReadWrite = Read | Write }; enum class SeekMode : u32 { Set, Cur, End, }; class File { public: File(); explicit File(const std::string& path, OpenMode mode = OpenMode::Read); ~File(); bool open(const std::string& path, OpenMode mode = OpenMode::Read); bool close(); bool read(void* data, u64 size) const; bool write(std::span data); bool seek(s64 offset, SeekMode mode); u64 getFileSize(); u64 tell() const; template bool read(T& data) const { return read(&data, sizeof(T)); } template bool read(std::vector& data) const { return read(data.data(), data.size() * sizeof(T)); } bool isOpen() const { return m_file != nullptr; } const char* getOpenMode(OpenMode mode) const { switch (mode) { case OpenMode::Read: return "rb"; case OpenMode::Write: return "wb"; case OpenMode::ReadWrite: return "r+b"; default: return "r"; } } int getSeekMode(SeekMode mode) const { switch (mode) { case SeekMode::Set: return SEEK_SET; case SeekMode::Cur: return SEEK_CUR; case SeekMode::End: return SEEK_END; default: return SEEK_SET; } } [[nodiscard]] std::FILE* fileDescr() const { return m_file; } private: std::FILE* m_file{}; }; } // namespace Common::FS