2012-06-01 15:23:19 +00:00
|
|
|
// 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 --
|
2012-06-01 15:23:19 +00:00
|
|
|
* 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
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
virtual ~File();
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
/** 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;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
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;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
virtual bool flush() = 0;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
/** Current offset in file. Return npos if NA. */
|
2014-04-06 17:19:33 +00:00
|
|
|
virtual vfspos getpos() const = 0;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
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;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
/** 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;
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
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);
|
2012-06-01 15:23:19 +00:00
|
|
|
};
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
class DiskFile : public File
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-04-06 17:19:33 +00:00
|
|
|
DiskFile(const char *name);
|
|
|
|
virtual ~DiskFile();
|
2012-06-01 15:23:19 +00:00
|
|
|
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"; }
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
inline void *getFP() { return _fh; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void *_fh; // FILE*
|
|
|
|
};
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
class MemFile : public File
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-04-06 17:19:33 +00:00
|
|
|
enum DeleteMode
|
2012-06-01 15:23:19 +00:00
|
|
|
{
|
2014-04-06 17:19:33 +00:00
|
|
|
ON_CLOSE,
|
|
|
|
ON_DESTROY
|
2012-06-01 15:23:19 +00:00
|
|
|
};
|
2017-01-12 22:52:59 +00:00
|
|
|
/** Creates a virtual file from a memory buffer. The buffer is passed as-is,
|
2014-04-06 17:19:33 +00:00
|
|
|
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();
|
2017-01-12 22:52:59 +00:00
|
|
|
/** In order not to modify the passed buffer, MemFile does NOT respect the mode parameter. */
|
|
|
|
virtual bool open(const char *mode = NULL) { return !!_buf; }
|
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"; }
|
2012-06-01 15:23:19 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-04-06 17:19:33 +00:00
|
|
|
void _clearMem();
|
|
|
|
|
|
|
|
void *_buf;
|
2012-06-01 15:23:19 +00:00
|
|
|
vfspos _pos;
|
|
|
|
vfspos _size;
|
2014-04-06 17:19:33 +00:00
|
|
|
delete_func _delfunc;
|
|
|
|
DeleteMode _delmode;
|
2012-06-01 15:23:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
VFS_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|