ScummVM API documentation
file.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_LIB_ALLEGRO_FILE_H
23 #define AGS_LIB_ALLEGRO_FILE_H
24 
25 #include "ags/lib/allegro/alconfig.h"
26 #include "ags/shared/core/types.h"
27 #include "common/file.h"
28 
29 namespace AGS3 {
30 
31 #define F_READ "r"
32 #define F_WRITE "w"
33 
34 #define F_BUF_SIZE 4096 /* 4K buffer for caching data */
35 
37  int hndl; /* DOS file handle */
38  int flags; /* PACKFILE_FLAG_* constants */
39  unsigned char *buf_pos; /* position in buffer */
40  int buf_size; /* number of bytes in the buffer */
41  long todo; /* number of bytes still on the disk */
42  struct PACKFILE *parent; /* nested, parent file */
43  char *filename; /* name of the file */
44  unsigned char buf[F_BUF_SIZE]; /* the actual data buffer */
45 };
46 
48  AL_METHOD(int, pf_fclose, (void *userdata));
49  AL_METHOD(int, pf_getc, (void *userdata));
50  AL_METHOD(int, pf_ungetc, (int c, void *userdata));
51  AL_METHOD(long, pf_fread, (void *p, long n, void *userdata));
52  AL_METHOD(int, pf_putc, (int c, void *userdata));
53  AL_METHOD(long, pf_fwrite, (AL_CONST void *p, long n, void *userdata));
54  AL_METHOD(int, pf_fseek, (void *userdata, int offset));
55  AL_METHOD(int, pf_feof, (void *userdata));
56  AL_METHOD(int, pf_ferror, (void *userdata));
57 };
58 
62 struct PACKFILE {
63  virtual ~PACKFILE() {
64  close();
65  }
66 
67  virtual void close() {}
68  virtual int pack_fseek(int offset) = 0;
69  virtual int pack_getc() = 0;
70  virtual int pack_putc(int c) = 0;
71  virtual int pack_ungetc(int c) = 0;
72  virtual long pack_fread(void *p, long n) = 0;
73  virtual long pack_fwrite(AL_CONST void *p, long n) = 0;
74  virtual int pack_feof() = 0;
75  virtual int pack_ferror() = 0;
76  virtual void *pack_get_userdata() const {
77  return nullptr;
78  }
79 
80  PACKFILE *pack_fopen_chunk(int pack);
81  PACKFILE *pack_fclose_chunk();
82  int pack_igetw();
83  int32_t pack_igetl();
84  int pack_iputw(int w);
85  int32_t pack_iputl(int32_t l);
86  int pack_mgetw();
87  int32_t pack_mgetl();
88  int pack_mputw(int w);
89  int32_t pack_mputl(int32_t l);
90  char *pack_fgets(char *p, int max);
91  int pack_fputs(AL_CONST char *p);
92  };
93 
94 struct ScummVMPackFile : public PACKFILE {
95 public:
97 
99  }
100 
101  virtual ~ScummVMPackFile() {}
102 
103  void close() override {
104  delete _stream;
105  _stream = nullptr;
106  }
107 
108  int pack_fseek(int offset) override {
109  return _stream->seek(offset, SEEK_CUR);
110  }
111 
112  int pack_getc() override {
113  return _stream->readByte();
114  }
115 
116  int pack_putc(int c) override {
117  error("pack_putc is not yet supported");
118  }
119 
120  int pack_ungetc(int c) override {
121  _stream->seek(-1, SEEK_CUR);
122  return 0;
123  }
124 
125  long pack_fread(void *p, long n) override {
126  return _stream->read(p, n);
127  }
128 
129  long pack_fwrite(AL_CONST void *p, long n) override {
130  error("pack_fwrite is not yet supported");
131  }
132 
133  int pack_feof() override {
134  return _stream->eos();
135  }
136 
137  int pack_ferror() override {
138  return _stream->err();
139  }
140  };
141 
142  struct VTablePackFile : public PACKFILE {
143  AL_CONST PACKFILE_VTABLE *_vTable;
144  void *_userData;
145 
146  VTablePackFile(AL_CONST PACKFILE_VTABLE *vTable, void *userData) :
147  _vTable(vTable), _userData(userData) {
148  }
149 
150  void close() override {
151  _vTable->pf_fclose(_userData);
152  }
153 
154  int pack_fseek(int offset) override {
155  return _vTable->pf_fseek(_userData, offset);
156  }
157 
158  int pack_getc() override {
159  return _vTable->pf_getc(_userData);
160  }
161 
162  int pack_putc(int c) override {
163  return _vTable->pf_putc(c, _userData);
164  }
165 
166  int pack_ungetc(int c) override {
167  return _vTable->pf_ungetc(c, _userData);
168  }
169 
170  long pack_fread(void *p, long n) override {
171  return _vTable->pf_fread(p, n, _userData);
172  }
173 
174  long pack_fwrite(AL_CONST void *p, long n) override {
175  return _vTable->pf_fwrite(p, n, _userData);
176  }
177 
178  int pack_feof() override {
179  return _vTable->pf_feof(_userData);
180  }
181 
182  int pack_ferror() override {
183  return _vTable->pf_ferror(_userData);
184  }
185 
186  void *pack_get_userdata() const override {
187  return _userData;
188  }
189 };
190 
191 extern void set_filename_encoding(int);
192 extern char *fix_filename_case(char *path);
193 extern char *fix_filename_slashes(char *path);
194 extern char *append_filename(char *dest, const char *path, const char *filename, int size);
195 extern char *canonicalize_filename(char *dest, const char *filename, int size);
196 extern char *make_relative_filename(char *dest, const char *path, const char *filename, int size);
197 extern int is_relative_filename(const char *filename);
198 
199 AL_FUNC(void, packfile_password, (AL_CONST char *password));
200 AL_FUNC(PACKFILE *, pack_fopen, (AL_CONST char *filename, AL_CONST char *mode));
201 AL_FUNC(PACKFILE *, pack_fopen_vtable, (AL_CONST PACKFILE_VTABLE *vtable, void *userdata));
202 AL_FUNC(int, pack_fclose, (PACKFILE *f));
203 AL_FUNC(int, pack_fseek, (PACKFILE *f, int offset));
204 AL_FUNC(PACKFILE *, pack_fopen_chunk, (PACKFILE *f, int pack));
205 AL_FUNC(PACKFILE *, pack_fclose_chunk, (PACKFILE *f));
206 AL_FUNC(int, pack_getc, (PACKFILE *f));
207 AL_FUNC(int, pack_putc, (int c, PACKFILE *f));
208 AL_FUNC(int, pack_feof, (PACKFILE *f));
209 AL_FUNC(int, pack_ferror, (PACKFILE *f));
210 AL_FUNC(int, pack_igetw, (PACKFILE *f));
211 AL_FUNC(int32_t, pack_igetl, (PACKFILE *f));
212 AL_FUNC(int, pack_iputw, (int w, PACKFILE *f));
213 AL_FUNC(int32_t, pack_iputl, (int32_t l, PACKFILE *f));
214 AL_FUNC(int, pack_mgetw, (PACKFILE *f));
215 AL_FUNC(int32_t, pack_mgetl, (PACKFILE *f));
216 AL_FUNC(int, pack_mputw, (int w, PACKFILE *f));
217 AL_FUNC(int32_t, pack_mputl, (int32_t l, PACKFILE *f));
218 AL_FUNC(long, pack_fread, (void *p, long n, PACKFILE *f));
219 AL_FUNC(long, pack_fwrite, (AL_CONST void *p, long n, PACKFILE *f));
220 AL_FUNC(int, pack_ungetc, (int c, PACKFILE *f));
221 AL_FUNC(char *, pack_fgets, (char *p, int max, PACKFILE *f));
222 AL_FUNC(int, pack_fputs, (AL_CONST char *p, PACKFILE *f));
223 AL_FUNC(void *, pack_get_userdata, (PACKFILE *f));
224 
225 } // namespace AGS3
226 
227 #endif
virtual bool err() const
Definition: stream.h:61
Definition: file.h:62
virtual bool seek(int64 offset, int whence=SEEK_SET)=0
virtual bool eos() const =0
Definition: stream.h:745
Definition: file.h:94
byte readByte()
Definition: stream.h:434
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: file.h:142
Definition: file.h:47
Definition: ags.h:40
virtual uint32 read(void *dataPtr, uint32 dataSize)=0