1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-27 07:06:00 +00:00
Aquaria/ExternalLibs/ttvfs/VFSFile.h

124 lines
4 KiB
C
Raw Normal View History

// VFSFile.h - basic file interface + classes
// For conditions of distribution and use, see copyright notice in VFS.h
#ifndef VFSFILE_H
#define VFSFILE_H
#include "VFSBase.h"
VFS_NAMESPACE_START
2014-04-06 17:19:33 +00:00
/** -- File basic interface --
* All functions that return bool should return true on success and false on failure.
* If an operation is not necessary or irrelevant (for example, files in memory can't be closed),
* it is useful to return true anyways, because this operation did not fail, technically.
* (Common sense here!)
* An int/vfspos value of 0 indicates failure, except the size/seek/getpos functions, where npos means failure.
* Only the functions required or applicable need to be implemented, for unsupported operations
* the default implementation should be sufficient.
**/
2014-04-06 17:19:33 +00:00
class File : public VFSBase
{
public:
2014-04-06 17:19:33 +00:00
virtual ~File();
/** Open a file.
Mode can be "r", "w", "rb", "rb", and possibly other things that fopen supports.
It is the subclass's choice to support other modes. Default is "rb".
Closes and reopens if already open (even in the same mode). */
2014-04-06 17:19:33 +00:00
virtual bool open(const char *mode = NULL) = 0;
2014-04-06 17:19:33 +00:00
virtual bool isopen() const = 0;
virtual bool iseof() const = 0;
virtual void close() = 0;
virtual bool seek(vfspos pos, int whence) = 0;
2014-04-06 17:19:33 +00:00
virtual bool flush() = 0;
/** Current offset in file. Return npos if NA. */
2014-04-06 17:19:33 +00:00
virtual vfspos getpos() const = 0;
2014-04-06 17:19:33 +00:00
virtual size_t read(void *dst, size_t bytes) = 0;
virtual size_t write(const void *src, size_t bytes) = 0;
/** Return file size. If NA, return npos. If size is not yet known,
open() and close() may be called (with default args) to find out the size.
The file is supposed to be in its old state when the function returns,
2014-04-06 17:19:33 +00:00
that is in the same open state and seek position. */
virtual vfspos size() = 0;
protected:
2014-04-06 17:19:33 +00:00
/** The ctor is expected to set both name() and fullname();
The name must remain static throughout the object's lifetime. */
File(const char *fn);
};
2014-04-06 17:19:33 +00:00
class DiskFile : public File
{
public:
2014-04-06 17:19:33 +00:00
DiskFile(const char *name);
virtual ~DiskFile();
virtual bool open(const char *mode = NULL);
2014-04-06 17:19:33 +00:00
virtual bool isopen() const;
virtual bool iseof() const;
virtual void close();
virtual bool seek(vfspos pos, int whence);
virtual bool flush();
virtual vfspos getpos() const;
virtual size_t read(void *dst, size_t bytes);
virtual size_t write(const void *src, size_t bytes);
virtual vfspos size();
virtual const char *getType() const { return "DiskFile"; }
inline void *getFP() { return _fh; }
protected:
void *_fh; // FILE*
void *_buf;
};
2014-04-06 17:19:33 +00:00
class MemFile : public File
{
public:
2014-04-06 17:19:33 +00:00
enum DeleteMode
{
2014-04-06 17:19:33 +00:00
ON_CLOSE,
ON_DESTROY
};
2014-04-06 17:19:33 +00:00
/* Creates a virtual file from a memory buffer. The buffer is passed as-is,
so for text files you should make sure it ends with a \0 character.
A deletor function can be passed optionally, that the buffer will be passed to
when the memory file is destroyed. Pass NULL or leave away to keep the buffer alive. */
MemFile(const char *name, void *buf, unsigned int size, delete_func delfunc = NULL, DeleteMode delmode = ON_CLOSE);
virtual ~MemFile();
virtual bool open(const char *mode = NULL) { return true; }
2014-04-06 17:19:33 +00:00
virtual bool isopen() const { return !!_buf; } // always open
virtual bool iseof() const { return _pos >= _size; }
virtual void close();
virtual bool seek(vfspos pos, int whence);
virtual bool flush() { return true; }
virtual vfspos getpos() const { return _pos; }
virtual size_t read(void *dst, size_t bytes);
virtual size_t write(const void *src, size_t bytes);
virtual vfspos size() { return _size; }
virtual const char *getType() const { return "MemFile"; }
protected:
2014-04-06 17:19:33 +00:00
void _clearMem();
void *_buf;
vfspos _pos;
vfspos _size;
2014-04-06 17:19:33 +00:00
delete_func _delfunc;
DeleteMode _delmode;
};
VFS_NAMESPACE_END
#endif