1 // VFSFile.h - basic file interface + classes
2 // For conditions of distribution and use, see copyright notice in VFS.h
3 
4 #ifndef VFSFILE_H
5 #define VFSFILE_H
6 
7 #include "VFSBase.h"
8 
9 VFS_NAMESPACE_START
10 
11 
12 /** -- File basic interface --
13   * All functions that return bool should return true on success and false on failure.
14   * If an operation is not necessary or irrelevant (for example, files in memory can't be closed),
15   *    it is useful to return true anyways, because this operation did not fail, technically.
16   *    (Common sense here!)
17   * An int/vfspos value of 0 indicates failure, except the size/seek/getpos functions, where npos means failure.
18   * Only the functions required or applicable need to be implemented, for unsupported operations
19   *    the default implementation should be sufficient.
20   **/
21 class File : public VFSBase
22 {
23 public:
24 
25     virtual ~File();
26 
27     /** Open a file.
28         Mode can be "r", "w", "rb", "rb", and possibly other things that fopen supports.
29         It is the subclass's choice to support other modes. Default is "rb".
30         Closes and reopens if already open (even in the same mode). */
31     virtual bool open(const char *mode = NULL) = 0;
32 
33     virtual bool isopen() const = 0;
34     virtual bool iseof() const = 0;
35     virtual void close() = 0;
36     virtual bool seek(vfspos pos, int whence) = 0;
37 
38     virtual bool flush() = 0;
39 
40     /** Current offset in file. Return npos if NA. */
41     virtual vfspos getpos() const = 0;
42 
43     virtual size_t read(void *dst, size_t bytes) = 0;
44     virtual size_t write(const void *src, size_t bytes) = 0;
45 
46     /** Return file size. If NA, return npos. If size is not yet known,
47         open() and close() may be called (with default args) to find out the size.
48         The file is supposed to be in its old state when the function returns,
49         that is in the same open state and seek position. */
50     virtual vfspos size() = 0;
51 
52 protected:
53 
54     /** The ctor is expected to set both name() and fullname();
55     The name must remain static throughout the object's lifetime. */
56     File(const char *fn);
57 };
58 
59 class DiskFile : public File
60 {
61 public:
62     DiskFile(const char *name);
63     virtual ~DiskFile();
64     virtual bool open(const char *mode = NULL);
65     virtual bool isopen() const;
66     virtual bool iseof() const;
67     virtual void close();
68     virtual bool seek(vfspos pos, int whence);
69     virtual bool flush();
70     virtual vfspos getpos() const;
71     virtual size_t read(void *dst, size_t bytes);
72     virtual size_t write(const void *src, size_t bytes);
73     virtual vfspos size();
getType()74     virtual const char *getType() const { return "DiskFile"; }
75 
getFP()76     inline void *getFP() { return _fh; }
77 
78 protected:
79 
80     void *_fh; // FILE*
81 };
82 
83 class MemFile : public File
84 {
85 public:
86     enum DeleteMode
87     {
88         ON_CLOSE,
89         ON_DESTROY
90     };
91     /* Creates a virtual file from a memory buffer. The buffer is passed as-is,
92        so for text files you should make sure it ends with a \0 character.
93        A deletor function can be passed optionally, that the buffer will be passed to
94        when the memory file is destroyed. Pass NULL or leave away to keep the buffer alive. */
95     MemFile(const char *name, void *buf, unsigned int size, delete_func delfunc = NULL, DeleteMode delmode = ON_CLOSE);
96     virtual ~MemFile();
97     virtual bool open(const char *mode = NULL) { return true; }
isopen()98     virtual bool isopen() const { return !!_buf; } // always open
iseof()99     virtual bool iseof() const { return _pos >= _size; }
100     virtual void close();
101     virtual bool seek(vfspos pos, int whence);
flush()102     virtual bool flush() { return true; }
getpos()103     virtual vfspos getpos() const { return _pos; }
104     virtual size_t read(void *dst, size_t bytes);
105     virtual size_t write(const void *src, size_t bytes);
size()106     virtual vfspos size() { return _size; }
getType()107     virtual const char *getType() const { return "MemFile"; }
108 
109 protected:
110 
111     void _clearMem();
112 
113     void *_buf;
114     vfspos _pos;
115     vfspos _size;
116     delete_func _delfunc;
117     DeleteMode _delmode;
118 };
119 
120 VFS_NAMESPACE_END
121 
122 #endif
123