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 namespace SpaceBar {
35 
36 #define CBF_TEXT 0x00000001
37 #define CBF_BINARY 0x00000002
38 #define CBF_READONLY 0x00000004
39 #define CBF_OVERWRITE 0x00000008
40 #define CBF_SHARED 0x00000010
41 #define CBF_CREATE 0x00000020
42 #define CBF_SAVEFILE 0x100
43 
44 #define CBF_DEFAULT (CBF_BINARY | CBF_READONLY)
45 
46 #define CBOFFILE_TEXT CBF_TEXT
47 #define CBOFFILE_READONLY CBF_READONLY
48 #define CBOFFILE_OVERWRITE CBF_OVERWRITE
49 
50 #define CBOFFILE_DEFAULT CBF_DEFAULT
51 
53 private:
55 public:
56  SaveReadStream(Common::MemoryWriteStreamDynamic *owner) : _owner(owner) {
57  }
58 
59  bool eos() const override {
60  return _owner->pos() >= _owner->size();
61  }
62  uint32 read(void *dataPtr, uint32 dataSize) override {
63  int bytesToCopy = MIN<int>(dataSize, _owner->size() - _owner->pos());
64  const byte *src = _owner->getData() + _owner->pos();
65  Common::copy(src, src + bytesToCopy, (byte *)dataPtr);
66  seek(bytesToCopy, SEEK_CUR);
67  return bytesToCopy;
68  }
69 };
70 
76 private:
77  Common::WriteStream *_save;
78 
79 public:
81  Common::MemoryWriteStreamDynamic(DisposeAfterUse::YES),
82  SaveReadStream(this), _save(save) {
83  }
85  _save->write(getData(), Common::MemoryWriteStreamDynamic::size());
86  delete _save;
87  }
88 
89  int64 pos() const override {
91  }
92  bool seek(int64 offset, int whence = SEEK_SET) override {
93  return Common::MemoryWriteStreamDynamic::seek(offset, whence);
94  }
95  int64 size() const override {
97  }
98 };
99 
100 class CBofFile : public CBofObject, public CBofError {
101 protected:
102  char _szFileName[MAX_FNAME];
103  Common::Stream *_stream = nullptr;
104  uint32 _lFlags = CBF_DEFAULT;
105 
106 public:
110  CBofFile();
111 
117  CBofFile(const char *pszFileName, uint32 lFlags = CBF_DEFAULT);
118 
122  virtual ~CBofFile();
123 
129  ErrorCode open(const char *pszFileName, uint32 lFlags = CBF_DEFAULT);
130 
136  ErrorCode create(const char *pszFileName, uint32 lFlags = CBF_DEFAULT | CBF_CREATE);
137 
141  virtual ErrorCode close();
142 
149  virtual ErrorCode read(void *pDestBuf, int32 lBytes);
150 
157  virtual ErrorCode write(const void *pSrcBuf, int32 lBytes);
158 
162  void commit();
163 
168  ErrorCode seek(uint32 lPos) {
169  return (setPosition(lPos));
170  }
171 
176  ErrorCode seekToBeginning() {
177  return (setPosition(0));
178  }
179 
184  ErrorCode seekToEnd();
185 
190  ErrorCode setPosition(uint32 lPos);
191 
195  uint32 getPosition();
196 
200  uint32 getLength();
201 
202  operator Common::SeekableReadStream *() const {
203  return dynamic_cast<Common::SeekableReadStream *>(_stream);
204  }
205 };
206 
207 } // namespace SpaceBar
208 } // namespace Bagel
209 
210 #endif
Definition: stream.h:77
Definition: object.h:28
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
bool eos() const override
Definition: file.h:59
int64 size() const override
Definition: memstream.h:250
int64 size() const override
Definition: file.h:95
Definition: file.h:52
bool seek(int64 offset, int whence=SEEK_SET) override
Definition: file.h:92
int64 pos() const override
Definition: memstream.h:249
Definition: memstream.h:194
int64 pos() const override
Definition: file.h:89
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:52
ErrorCode seekToBeginning()
Definition: file.h:176
Definition: file.h:100
ErrorCode seek(uint32 lPos)
Definition: file.h:168
uint32 read(void *dataPtr, uint32 dataSize) override
Definition: file.h:62
Definition: afxwin.h:27
Definition: stream.h:48
virtual uint32 write(const void *dataPtr, uint32 dataSize)=0