#ifndef KYTY_COMMON_FILE_H_ #define KYTY_COMMON_FILE_H_ #include "common/common.h" #include "common/dateTime.h" #include #include #include #include #include #include #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS #ifdef CreateDirectory #undef CreateDirectory #endif #ifdef DeleteFile #undef DeleteFile #endif #ifdef CopyFile #undef CopyFile #endif #endif namespace Common { // SUBSYSTEM_DEFINE(File); class File { public: enum class Mode { Read, Write, ReadWrite, WriteRead }; struct DirEntry { std::string name; bool is_file; }; struct Info { bool is_file; uint64_t size; }; File(); explicit File(const std::filesystem::path& name); File(const std::filesystem::path& name, Mode mode); virtual ~File(); bool Create(const std::filesystem::path& name); bool Open(const std::filesystem::path& name, Mode mode); bool OpenInMem(void* buf, uint32_t buf_size); bool CreateInMem(); void Close(); bool Flush(); [[nodiscard]] uint64_t Size() const; [[nodiscard]] uint64_t Remaining() const; bool Seek(uint64_t offset); [[nodiscard]] uint64_t Tell() const; bool Truncate(uint64_t size); bool Unlink(); [[nodiscard]] bool IsInvalid() const; [[nodiscard]] bool IsEOF() const { return Tell() >= Size(); } void GetLastAccessAndWriteTimeUTC(DateTime* access, DateTime* write); void Read(void* data, uint32_t size, uint32_t* bytes_read = nullptr); void Write(const void* data, uint32_t size, uint32_t* bytes_written = nullptr); void Printf(const char* format, ...) KYTY_FORMAT_PRINTF(2, 3); std::vector ReadWholeBuffer(); static uint64_t Size(const std::filesystem::path& name); static std::optional GetInfo(const std::filesystem::path& name); static bool IsDirectoryExisting(const std::filesystem::path& path); static bool IsFileExisting(const std::filesystem::path& name); static bool CreateDirectory(const std::filesystem::path& path); static bool CreateDirectories(const std::filesystem::path& path); static bool DeleteDirectory(const std::filesystem::path& path); static bool DeleteDirectories(const std::filesystem::path& path); static bool DeleteFile(const std::filesystem::path& name); static DateTime GetLastAccessTimeUTC(const std::filesystem::path& name); static DateTime GetLastWriteTimeUTC(const std::filesystem::path& name); static void GetLastAccessAndWriteTimeUTC(const std::filesystem::path& name, DateTime* access, DateTime* write); static bool SetLastAccessTimeUTC(const std::filesystem::path& name, const DateTime& dt); static bool SetLastWriteTimeUTC(const std::filesystem::path& name, const DateTime& dt); static bool SetLastAccessAndWriteTimeUTC(const std::filesystem::path& name, const DateTime& access, const DateTime& write); static std::vector GetDirEntries(const std::filesystem::path& path); static bool CopyFile(const std::filesystem::path& src, const std::filesystem::path& dst); static bool RenameFile(const std::filesystem::path& src, const std::filesystem::path& dst); static void RemoveReadonly(const std::filesystem::path& name); KYTY_CLASS_NO_COPY(File); private: struct FilePrivate; std::filesystem::path m_file_name; std::unique_ptr m_p; }; } // namespace Common #endif /* KYTY_COMMON_FILE_H_ */