ScummVM API documentation
file.h
1 
2 /* ScummVM - Graphic Adventure Engine
3  *
4  * ScummVM is the legal property of its developers, whose names
5  * are too numerous to list here. Please refer to the COPYRIGHT
6  * file distributed with this source distribution.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef BAGEL_BOFLIB_FILE_H
24 #define BAGEL_BOFLIB_FILE_H
25 
26 #include "common/algorithm.h"
27 #include "common/memstream.h"
28 #include "common/stream.h"
29 #include "bagel/boflib/stdinc.h"
30 #include "bagel/boflib/object.h"
31 #include "bagel/boflib/error.h"
32 
33 namespace Bagel {
34 
35 #define CBF_TEXT 0x00000001
36 #define CBF_BINARY 0x00000002
37 #define CBF_READONLY 0x00000004
38 #define CBF_OVERWRITE 0x00000008
39 #define CBF_SHARED 0x00000010
40 #define CBF_CREATE 0x00000020
41 #define CBF_SAVEFILE 0x100
42 
43 #define CBF_DEFAULT (CBF_BINARY | CBF_READONLY)
44 
45 #define CBOFFILE_TEXT CBF_TEXT
46 #define CBOFFILE_READONLY CBF_READONLY
47 #define CBOFFILE_OVERWRITE CBF_OVERWRITE
48 
49 #define CBOFFILE_DEFAULT CBF_DEFAULT
50 
52 private:
54 public:
55  SaveReadStream(Common::MemoryWriteStreamDynamic *owner) : _owner(owner) {}
56 
57  bool eos() const override {
58  return _owner->pos() >= _owner->size();
59  }
60  uint32 read(void *dataPtr, uint32 dataSize) override {
61  int bytesToCopy = MIN<int>(dataSize, _owner->size() - _owner->pos());
62  const byte *src = _owner->getData() + _owner->pos();
63  Common::copy(src, src + bytesToCopy, (byte *)dataPtr);
64  seek(bytesToCopy, SEEK_CUR);
65  return bytesToCopy;
66  }
67 };
68 
74 private:
75  Common::WriteStream *_save;
76 
77 public:
79  Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES),
80  SaveReadStream(this), _save(save) {
81  }
83  _save->write(getData(), Common::MemoryWriteStreamDynamic::size());
84  delete _save;
85  }
86 
87  int64 pos() const override {
89  }
90  bool seek(int64 offset, int whence = SEEK_SET) override {
91  return Common::MemoryWriteStreamDynamic::seek(offset, whence);
92  }
93  int64 size() const override {
95  }
96 };
97 
98 class CBofFile : public CBofObject, public CBofError {
99 protected:
100  char _szFileName[MAX_FNAME];
101  Common::Stream *_stream = nullptr;
102  uint32 _lFlags = CBF_DEFAULT;
103 
104 public:
108  CBofFile();
109 
115  CBofFile(const char *pszFileName, uint32 lFlags = CBF_DEFAULT);
116 
120  virtual ~CBofFile();
121 
127  ErrorCode open(const char *pszFileName, uint32 lFlags = CBF_DEFAULT);
128 
134  ErrorCode create(const char *pszFileName, uint32 lFlags = CBF_DEFAULT | CBF_CREATE);
135 
139  virtual ErrorCode close();
140 
147  virtual ErrorCode read(void *pDestBuf, int32 lBytes);
148 
155  virtual ErrorCode write(const void *pSrcBuf, int32 lBytes);
156 
160  void commit();
161 
166  ErrorCode seek(uint32 lPos) {
167  return (setPosition(lPos));
168  }
169 
174  ErrorCode seekToBeginning() {
175  return (setPosition(0));
176  }
177 
182  ErrorCode seekToEnd();
183 
188  ErrorCode setPosition(uint32 lPos);
189 
193  uint32 getPosition();
194 
198  uint32 getLength();
199 
200  operator Common::SeekableReadStream *() const {
201  return dynamic_cast<Common::SeekableReadStream *>(_stream);
202  }
203 };
204 
205 } // namespace Bagel
206 
207 #endif
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: file.h:60
int64 size() const override
Definition: file.h:93
Definition: file.h:73
Definition: stream.h:77
Definition: file.h:51
Definition: object.h:28
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
int64 size() const override
Definition: memstream.h:250
int64 pos() const override
Definition: memstream.h:249
Definition: memstream.h:194
Definition: stream.h:745
Out copy(In first, In last, Out dst)
Definition: algorithm.h:52
bool seek(int64 offs, int whence=SEEK_SET) override
Definition: memstream.h:254
Definition: error.h:50
int64 pos() const override
Definition: file.h:87
Definition: file.h:98
bool eos() const override
Definition: file.h:57
Definition: bagel.h:31
ErrorCode seekToBeginning()
Definition: file.h:174
Definition: stream.h:48
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: file.h:90
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0
ErrorCode seek(uint32 lPos)
Definition: file.h:166