2020-08-27 00:21:44 +08:00
|
|
|
// Copyright (c) 2018-2019 HALX99
|
2022-04-25 19:15:46 +08:00
|
|
|
// Copyright (c) 2020 C4games Ltd
|
2019-11-24 14:54:45 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2021-04-22 22:01:32 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2019-11-24 14:54:45 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-24 14:54:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL FileStream
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-24 14:54:45 +08:00
|
|
|
public:
|
2021-04-22 01:15:49 +08:00
|
|
|
virtual ~FileStream() = default;
|
2020-08-27 11:35:55 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
enum class Mode
|
|
|
|
{
|
2020-08-27 10:19:21 +08:00
|
|
|
READ,
|
|
|
|
WRITE,
|
|
|
|
APPEND,
|
2021-07-04 16:40:34 +08:00
|
|
|
OVERLAPPED,
|
2019-11-24 14:54:45 +08:00
|
|
|
};
|
|
|
|
|
2021-04-25 13:27:25 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Open a file
|
|
|
|
* @param path file to open
|
|
|
|
* @param mode File open mode, being READ | WRITE | APPEND
|
|
|
|
* @return true if successful, false if not
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual bool open(std::string_view path, FileStream::Mode mode) = 0;
|
2021-04-25 13:27:25 +08:00
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Close a file stream
|
|
|
|
* @return 0 if successful, -1 if not
|
|
|
|
*/
|
2021-04-22 01:15:49 +08:00
|
|
|
virtual int close() = 0;
|
|
|
|
|
2021-04-25 13:27:25 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Seek to position in a file stream
|
|
|
|
* @param offset how many bytes to move within the stream
|
|
|
|
* @param origin SEEK_SET | SEEK_CUR | SEEK_END
|
|
|
|
* @return 0 if successful, -1 if not
|
|
|
|
*/
|
2021-09-02 13:39:28 +08:00
|
|
|
virtual int seek(int64_t offset, int origin) = 0;
|
2021-04-25 13:27:25 +08:00
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Read data from file stream
|
|
|
|
* @param buf pointer to data
|
|
|
|
* @param size the amount of data to read in bytes
|
|
|
|
* @return amount of data read successfully, -1 if error
|
|
|
|
*/
|
2021-04-22 01:15:49 +08:00
|
|
|
virtual int read(void* buf, unsigned int size) = 0;
|
|
|
|
|
2021-04-25 13:27:25 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Write data to file stream
|
|
|
|
* @param buf pointer to data
|
|
|
|
* @param size the amount of data to write in bytes
|
|
|
|
* @return amount of data written successfully, -1 if error
|
|
|
|
*/
|
2021-04-22 01:15:49 +08:00
|
|
|
virtual int write(const void* buf, unsigned int size) = 0;
|
2021-04-25 13:27:25 +08:00
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Get the current position in the file stream
|
|
|
|
* @return current position, -1 if error
|
|
|
|
*/
|
2021-08-19 22:53:02 +08:00
|
|
|
virtual int64_t tell() = 0;
|
2021-04-25 13:27:25 +08:00
|
|
|
|
2021-05-31 15:08:44 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Get the size of the file stream
|
|
|
|
* @return stream size, -1 if error (Mode::WRITE and Mode::APPEND may return -1)
|
|
|
|
*/
|
2021-08-19 22:53:02 +08:00
|
|
|
virtual int64_t size() = 0;
|
2021-05-31 15:08:44 +08:00
|
|
|
|
2021-04-25 13:27:25 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Get status of file stream
|
|
|
|
* @return true if open, false if closed
|
|
|
|
*/
|
2021-04-22 01:15:49 +08:00
|
|
|
virtual bool isOpen() const = 0;
|
2021-04-24 07:22:27 +08:00
|
|
|
|
|
|
|
virtual operator bool() const { return isOpen(); }
|
|
|
|
|
|
|
|
protected:
|
2021-12-25 10:04:45 +08:00
|
|
|
FileStream(){};
|
2021-04-22 01:15:49 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|