2021-04-22 22:01:32 +08:00
|
|
|
// Copyright (c) 2018-2019 HALX99
|
|
|
|
// Copyright (c) 2020 c4games.com
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "platform/CCFileStream.h"
|
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
#include <string>
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <direct.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
#include "platform/android/CCFileUtils-android.h"
|
|
|
|
#include <jni.h>
|
|
|
|
#include <android/asset_manager.h>
|
|
|
|
#include <android/asset_manager_jni.h>
|
|
|
|
#include "base/ZipUtils.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2021-07-04 21:18:14 +08:00
|
|
|
#include "win32-specific/ntcvt/ntcvt.hpp"
|
2021-04-22 22:01:32 +08:00
|
|
|
#define O_READ_FLAGS O_BINARY | O_RDONLY, S_IREAD
|
|
|
|
#define O_WRITE_FLAGS O_CREAT | O_RDWR | O_BINARY | O_TRUNC, S_IWRITE | S_IREAD
|
|
|
|
#define O_APPEND_FLAGS O_APPEND | O_CREAT | O_RDWR | O_BINARY, S_IWRITE | S_IREAD
|
|
|
|
|
|
|
|
#define O_OVERLAP_FLAGS O_CREAT | O_RDWR | O_BINARY, S_IWRITE | S_IREAD
|
2021-07-04 21:18:14 +08:00
|
|
|
#define posix_open_cxx(path, ...) ::_wopen(ntcvt::from_chars(path).c_str(), ##__VA_ARGS__)
|
|
|
|
#define posix_open(path, ...) ::_wopen(ntcvt::from_chars(path).c_str(), ##__VA_ARGS__)
|
2021-04-22 22:01:32 +08:00
|
|
|
#define posix_close ::_close
|
|
|
|
#define posix_lseek ::_lseek
|
2021-09-02 18:43:37 +08:00
|
|
|
#define posix_lseek64 ::_lseeki64
|
2021-04-22 22:01:32 +08:00
|
|
|
#define posix_read ::_read
|
|
|
|
#define posix_write ::_write
|
|
|
|
#define posix_fd2fh(fd) reinterpret_cast<HANDLE>(_get_osfhandle(fd))
|
|
|
|
#define posix_fsetsize(fd, size) ::_chsize(fd, size)
|
|
|
|
#else
|
2021-09-02 18:43:37 +08:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define posix_lseek64 ::lseek
|
|
|
|
#else
|
|
|
|
# define posix_lseek64 ::lseek64
|
|
|
|
#endif
|
|
|
|
|
2021-05-14 20:50:59 +08:00
|
|
|
#define O_READ_FLAGS O_RDONLY
|
2021-04-22 22:01:32 +08:00
|
|
|
#define O_WRITE_FLAGS O_CREAT | O_RDWR | O_TRUNC, S_IRWXU
|
|
|
|
#define O_APPEND_FLAGS O_APPEND | O_CREAT | O_RDWR, S_IRWXU
|
|
|
|
|
|
|
|
#define O_OVERLAP_FLAGS O_CREAT | O_RDWR, S_IRWXU
|
2021-07-04 21:18:14 +08:00
|
|
|
#define posix_open_cxx(path, ...) ::open(path.c_str(), ##__VA_ARGS__)
|
2021-04-22 22:01:32 +08:00
|
|
|
#define posix_open ::open
|
|
|
|
#define posix_close ::close
|
|
|
|
#define posix_lseek ::lseek
|
|
|
|
#define posix_read ::read
|
|
|
|
#define posix_write ::write
|
|
|
|
#define posix_fd2fh(fd) (fd)
|
2021-04-26 17:02:16 +08:00
|
|
|
#define posix_fsetsize(fd, size) (::ftruncate(fd, size), ::lseek(fd, 0, SEEK_SET))
|
2021-04-22 22:01:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
struct UnzFileStream;
|
|
|
|
union PXFileHandle {
|
|
|
|
int _fd = -1;
|
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
|
|
|
|
AAsset* _asset;
|
|
|
|
ZipFileStream _zfs;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PXIoF;
|
|
|
|
|
|
|
|
class CC_DLL PosixFileStream : public FileStream
|
|
|
|
{
|
|
|
|
public:
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream() = default;
|
|
|
|
virtual ~PosixFileStream();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream(const PosixFileStream& other) = delete;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream(PosixFileStream&& other) noexcept
|
|
|
|
: FileStream(std::move(other)),
|
|
|
|
_handle(std::move(other._handle)),
|
|
|
|
_iof(other._iof)
|
|
|
|
{
|
|
|
|
other.reset();
|
|
|
|
}
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream& operator=(const PosixFileStream& other) = delete;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
PosixFileStream& operator=(PosixFileStream&& other) noexcept
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
FileStream::operator =(std::move(other));
|
|
|
|
_handle = std::move(other._handle);
|
|
|
|
_iof = other._iof;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
other.reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
enum class Mode {
|
|
|
|
READ,
|
|
|
|
WRITE,
|
|
|
|
APPEND,
|
|
|
|
};
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 07:22:27 +08:00
|
|
|
bool open(const std::string& path, FileStream::Mode mode) override;
|
|
|
|
int close() override;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-09-02 13:39:28 +08:00
|
|
|
int seek(int64_t offset, int origin) override;
|
2021-04-24 07:22:27 +08:00
|
|
|
int read(void* buf, unsigned int size) override;
|
|
|
|
int write(const void* buf, unsigned int size) override;
|
2021-08-19 22:53:02 +08:00
|
|
|
int64_t tell() override;
|
|
|
|
int64_t size() override;
|
2021-04-24 07:22:27 +08:00
|
|
|
bool isOpen() const override;
|
2021-04-22 22:01:32 +08:00
|
|
|
|
|
|
|
private:
|
2021-04-24 07:22:27 +08:00
|
|
|
int internalClose();
|
|
|
|
void reset();
|
2021-04-22 22:01:32 +08:00
|
|
|
|
2021-04-24 18:15:54 +08:00
|
|
|
PXFileHandle _handle{};
|
|
|
|
const PXIoF* _iof{};
|
2021-04-22 22:01:32 +08:00
|
|
|
};
|
|
|
|
|
2021-04-26 17:02:16 +08:00
|
|
|
NS_CC_END
|