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