summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 10bdd39)
raw | patch | inline | side by side (parent: 10bdd39)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Wed, 7 Apr 2021 07:09:53 +0000 (10:09 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Wed, 7 Apr 2021 07:09:53 +0000 (10:09 +0300) |
src/common/files.c | patch | blob | history | |
src/common/files.h | patch | blob | history | |
src/common/streams.c | patch | blob | history | |
src/common/streams.h | patch | blob | history | |
src/common/wadres.c | patch | blob | history | |
src/common/wadres.h | patch | blob | history | |
src/files.c | patch | blob | history | |
src/map.c | patch | blob | history | |
src/map.h | patch | blob | history | |
src/save.c | patch | blob | history | |
src/save.h | patch | blob | history |
diff --git a/src/common/files.c b/src/common/files.c
index 91789f638b829061d41efe480276582e8dea2286..792df2d1afc70188946e986328309447002732ff 100644 (file)
--- a/src/common/files.c
+++ b/src/common/files.c
#include <stdint.h>
#include <assert.h>
-static long FILE_Reader_GetPos (Reader *r) {
- FILE_Reader *rd = (FILE_Reader*)r;
+static long FILE_Stream_GetPos (Stream *r) {
+ FILE_Stream *rd = (FILE_Stream*)r;
assert(rd != NULL);
assert(rd->fp != NULL);
long pos = ftell(rd->fp);
return pos;
}
-static void FILE_Reader_SetPos (Reader *r, long pos) {
- FILE_Reader *rd = (FILE_Reader*)r;
+static void FILE_Stream_SetPos (Stream *r, long pos) {
+ FILE_Stream *rd = (FILE_Stream*)r;
assert(rd != NULL);
assert(rd->fp != NULL);
assert(pos >= 0);
assert(res == 0); // fail
}
-static void FILE_Reader_Read (Reader *r, void *data, size_t size, size_t n) {
- FILE_Reader *rd = (FILE_Reader*)r;
+static void FILE_Stream_Read (Stream *r, void *data, size_t size, size_t n) {
+ FILE_Stream *rd = (FILE_Stream*)r;
assert(rd != NULL);
assert(rd->fp != NULL);
size_t res = fread(data, size, n, rd->fp);
assert(res == n); // fail
}
-void FILE_AssignReader (FILE_Reader *r, FILE *fp) {
+static void FILE_Stream_Write (Stream *w, const void *data, size_t size, size_t n) {
+ FILE_Stream *wr = (FILE_Stream*)w;
+ assert(wr != NULL);
+ assert(wr->fp != NULL);
+ size_t res = fwrite(data, size, n, wr->fp);
+ assert(res == n); // fail
+}
+
+void FILE_Assign (FILE_Stream *r, FILE *fp) {
assert(r != NULL);
assert(fp != NULL);
- r->base.getpos = FILE_Reader_GetPos;
- r->base.setpos = FILE_Reader_SetPos;
- r->base.read = FILE_Reader_Read;
+ r->base.getpos = FILE_Stream_GetPos;
+ r->base.setpos = FILE_Stream_SetPos;
+ r->base.read = FILE_Stream_Read;
+ r->base.write = FILE_Stream_Write;
r->fp = fp;
}
-int FILE_OpenReader (FILE_Reader *r, const char *name) {
+int FILE_Open (FILE_Stream *r, const char *name, const char *mode) {
assert(r != NULL);
assert(name != NULL);
- FILE *fp = fopen(name, "rb");
+ FILE *fp = fopen(name, mode);
if (fp) {
- FILE_AssignReader(r, fp);
+ FILE_Assign(r, fp);
}
return fp != NULL;
}
-void FILE_CloseReader (FILE_Reader *r) {
+void FILE_Close (FILE_Stream *r) {
assert(r != NULL);
if (r->fp != NULL) {
fclose(r->fp);
}
r->base.getpos = NULL;
r->base.setpos = NULL;
- r->base.read = NULL;
+ r->base.read = NULL;
+ r->base.write = NULL;
r->fp = NULL;
}
-
-static long FILE_Writer_GetPos (Writer *w) {
- FILE_Writer *wr = (FILE_Writer*)w;
- assert(wr != NULL);
- assert(wr->fp != NULL);
- long pos = ftell(wr->fp);
- assert(pos != -1); // fail
- return pos;
-}
-
-static void FILE_Writer_SetPos (Writer *w, long pos) {
- FILE_Writer *wr = (FILE_Writer*)w;
- assert(wr != NULL);
- assert(wr->fp != NULL);
- assert(pos >= 0);
- int res = fseek(wr->fp, pos, SEEK_SET);
- assert(res == 0); // fail
-}
-
-static void FILE_Writer_Write (Writer *w, const void *data, size_t size, size_t n) {
- FILE_Writer *wr = (FILE_Writer*)w;
- assert(wr != NULL);
- assert(wr->fp != NULL);
- size_t res = fwrite(data, size, n, wr->fp);
- assert(res == n); // fail
-}
-
-void FILE_AssignWriter (FILE_Writer *w, FILE *fp) {
- assert(w != NULL);
- assert(fp != NULL);
- w->base.getpos = FILE_Writer_GetPos;
- w->base.setpos = FILE_Writer_SetPos;
- w->base.write = FILE_Writer_Write;
- w->fp = fp;
-}
-
-int FILE_OpenWriter (FILE_Writer *w, const char *name) {
- assert(w != NULL);
- assert(name != NULL);
- FILE *fp = fopen(name, "wb");
- if (fp) {
- FILE_AssignWriter(w, fp);
- }
- return fp != NULL;
-}
-
-void FILE_CloseWriter (FILE_Writer *w) {
- assert(w != NULL);
- if (w->fp != NULL) {
- fclose(w->fp);
- }
- w->base.getpos = NULL;
- w->base.setpos = NULL;
- w->base.write = NULL;
- w->fp = NULL;
-}
diff --git a/src/common/files.h b/src/common/files.h
index 9145902e758c3af3050972ee2ee81b6bcd768f2b..a575ec1ce162f90871f06f2cb8b086acc7b26680 100644 (file)
--- a/src/common/files.h
+++ b/src/common/files.h
#include <stdio.h>
#include "streams.h"
-typedef struct FILE_Reader {
- Reader base;
+typedef struct FILE_Stream {
+ Stream base;
FILE *fp;
-} FILE_Reader;
+} FILE_Stream;
-typedef struct FILE_Writer {
- Writer base;
- FILE *fp;
-} FILE_Writer;
-
-void FILE_AssignReader (FILE_Reader *r, FILE *fp);
-int FILE_OpenReader (FILE_Reader *r, const char *name);
-void FILE_CloseReader (FILE_Reader *r);
-
-void FILE_AssignWriter (FILE_Writer *w, FILE *fp);
-int FILE_OpenWriter (FILE_Writer *w, const char *name);
-void FILE_CloseWriter (FILE_Writer *w);
+void FILE_Assign (FILE_Stream *r, FILE *fp);
+int FILE_Open (FILE_Stream *r, const char *name, const char *mode);
+void FILE_Close (FILE_Stream *r);
#endif /* COMMON_FILES_H_INCLUDED */
\ No newline at end of file
diff --git a/src/common/streams.c b/src/common/streams.c
index 46e756b18b66acf0d36f93022f696ef8327639f0..76edaaba1e32fd35606c0afc8e9e75d15b219241 100644 (file)
--- a/src/common/streams.c
+++ b/src/common/streams.c
#include <stddef.h>
#include <stdint.h>
-void stream_read (void *data, size_t size, size_t n, Reader *r) {
+long stream_getpos (Stream *s) {
+ return s->getpos(s);
+}
+
+void stream_setpos (Stream *s, long pos) {
+ s->setpos(s, pos);
+}
+
+void stream_read (void *data, size_t size, size_t n, Stream *r) {
r->read(r, data, size, n);
}
-int8_t stream_read8 (Reader *r) {
+int8_t stream_read8 (Stream *r) {
int8_t x;
r->read(r, &x, 1, 1);
return x;
}
-int16_t stream_read16 (Reader *r) {
+int16_t stream_read16 (Stream *r) {
int16_t x;
r->read(r, &x, 2, 1);
return short2host(x);
}
-int32_t stream_read32 (Reader *r) {
+int32_t stream_read32 (Stream *r) {
int32_t x;
r->read(r, &x, 4, 1);
return int2host(x);
}
-void stream_write (const void *data, size_t size, size_t n, Writer *w) {
+void stream_write (const void *data, size_t size, size_t n, Stream *w) {
w->write(w, data, size, n);
}
-void stream_write8 (int8_t x, Writer *w) {
+void stream_write8 (int8_t x, Stream *w) {
w->write(w, &x, 1, 1);
}
-void stream_write16 (int16_t x, Writer *w) {
+void stream_write16 (int16_t x, Stream *w) {
int16_t y = short2host(x);
w->write(w, &y, 2, 1);
}
-void stream_write32 (int32_t x, Writer *w) {
+void stream_write32 (int32_t x, Stream *w) {
int32_t y = int2host(x);
w->write(w, &y, 4, 1);
}
diff --git a/src/common/streams.h b/src/common/streams.h
index d82b51af477658e54029da426619cb8019b9445d..e40427f432716e8c49173205a3277655ae6d8d40 100644 (file)
--- a/src/common/streams.h
+++ b/src/common/streams.h
#include <stddef.h>
#include <stdint.h>
-typedef struct Reader Reader;
-typedef struct Writer Writer;
-typedef struct Handle Handle;
+typedef struct Stream Stream;
-struct Reader {
- long (*getpos)(Reader *r);
- void (*setpos)(Reader *r, long pos);
- void (*read)(Reader *r, void *data, size_t size, size_t n);
+struct Stream {
+ long (*getpos)(Stream *r);
+ void (*setpos)(Stream *r, long pos);
+ void (*read)(Stream *r, void *data, size_t size, size_t n);
+ void (*write)(Stream *w, const void *data, size_t size, size_t n);
};
-struct Writer {
- long (*getpos)(Writer *w);
- void (*setpos)(Writer *w, long pos);
- void (*write)(Writer *w, const void *data, size_t size, size_t n);
-};
+long stream_getpos (Stream *s);
+void stream_setpos (Stream *s, long pos);
-void stream_read (void *data, size_t size, size_t n, Reader *f);
-int8_t stream_read8 (Reader *f);
-int16_t stream_read16 (Reader *f);
-int32_t stream_read32 (Reader *f);
+void stream_read (void *data, size_t size, size_t n, Stream *r);
+int8_t stream_read8 (Stream *r);
+int16_t stream_read16 (Stream *r);
+int32_t stream_read32 (Stream *r);
-void stream_write (const void *data, size_t size, size_t n, Writer *w);
-void stream_write8 (int8_t x, Writer *w);
-void stream_write16 (int16_t x, Writer *w);
-void stream_write32 (int32_t x, Writer *w);
+void stream_write (const void *data, size_t size, size_t n, Stream *w);
+void stream_write8 (int8_t x, Stream *w);
+void stream_write16 (int16_t x, Stream *w);
+void stream_write32 (int32_t x, Stream *w);
#endif /* COMMON_STREAMS_H_INCLUDED */
\ No newline at end of file
diff --git a/src/common/wadres.c b/src/common/wadres.c
index 99524cc1ed81ff2725e05221799f6151f6cf1a46..88583b78ece81075a003012b3c704177ced50cba 100644 (file)
--- a/src/common/wadres.c
+++ b/src/common/wadres.c
static int n_wads;
static int n_resources;
-static Reader *wads[MAX_WADS];
+static Stream *wads[MAX_WADS];
static Entry resources[MAX_RESOURCES];
-static int check_header (Reader *r) {
+static int check_header (Stream *r) {
assert(r != NULL);
char ident[4];
- r->setpos(r, 0);
+ stream_setpos(r, 0); // !!!
stream_read(ident, 4, 1, r);
return (memcmp(ident, "IWAD", 4) == 0) || (memcmp(ident, "PWAD", 4) == 0);
}
-int WADRES_addwad (Reader *r) {
+int WADRES_addwad (Stream *r) {
assert(r != NULL);
if (n_wads < MAX_WADS && check_header(r)) {
wads[n_wads] = r;
return -1;
}
-static int WADRES_read (Reader *r) {
- r->setpos(r, 4); // skip magic
+static int WADRES_read (Stream *r) {
+ stream_setpos(r, 4); // skip magic
int32_t n = stream_read32(r);
int32_t dir = stream_read32(r);
- r->setpos(r, dir);
+ stream_setpos(r, dir);
int ok = 1;
for (int32_t i = 0; ok && i < n; ++i) {
Entry e;
return n_resources;
}
-Reader *WADRES_getbasereader (int id) {
+Stream *WADRES_getbasereader (int id) {
assert(id >= 0 && id < n_resources);
return wads[resources[id].f];
}
void WADRES_getdata (int id, void *data) {
assert(id >= 0 && id < n_resources);
- Reader *r = wads[resources[id].f];
- long pos = r->getpos(r);
- r->setpos(r, resources[id].offset);
+ Stream *r = wads[resources[id].f];
+ long pos = stream_getpos(r);
+ stream_setpos(r, resources[id].offset);
stream_read(data, resources[id].size, 1, r);
- r->setpos(r, pos);
+ stream_setpos(r, pos);
}
diff --git a/src/common/wadres.h b/src/common/wadres.h
index 30ee85b8d21a21a2aaca53553b537c81ed3962ad..b970dfba5fc44825bf3c62a0a8cf0845a89d9edb 100644 (file)
--- a/src/common/wadres.h
+++ b/src/common/wadres.h
#define MAX_WADS 20
#define MAX_RESOURCES 2000
-int WADRES_addwad (Reader *r);
+int WADRES_addwad (Stream *r);
int WADRES_rehash (void);
int WADRES_find (const char name[8]);
int WADRES_maxids (void);
-Reader *WADRES_getbasereader (int id);
+Stream *WADRES_getbasereader (int id);
long WADRES_getoffset (int id);
long WADRES_getsize (int id);
void WADRES_getname (int id, char *name);
diff --git a/src/files.c b/src/files.c
index 1141c3b308e41a06dc9c9e7e4665d33c811b41db..deac9e3e2898da9726eeb48a4bc84d8d36b59ec8 100644 (file)
--- a/src/files.c
+++ b/src/files.c
void F_addwad (const char *fn) {
static int i = 0;
- static FILE_Reader wadh[MAX_WADS];
+ static FILE_Stream wadh[MAX_WADS];
if (i < MAX_WADS) {
- if (FILE_OpenReader(&wadh[i], fn)) {
+ if (FILE_Open(&wadh[i], fn, "rb")) {
if (WADRES_addwad(&wadh[i].base)) {
i += 1;
} else {
void F_loadmap (char n[8]) {
int id = F_getresid(n);
if (id != -1) {
- Reader *r = WADRES_getbasereader(id);
+ Stream *r = WADRES_getbasereader(id);
long offset = WADRES_getoffset(id);
- r->setpos(r, offset);
+ stream_setpos(r, offset);
if (!MAP_load(r)) {
ERR_fatal("Failed to load map");
}
diff --git a/src/map.c b/src/map.c
index dc65f0c2491e0952de1a089e0ee7118f02d7eec6..a5b5bd8a261d2ca75ad00e2cf377a06bdc8cea55 100644 (file)
--- a/src/map.c
+++ b/src/map.c
static map_block_t blk;
-static int G_load (Reader *h) {
+static int G_load (Stream *h) {
switch (blk.t) {
case MB_MUSIC:
stream_read(g_music, 8, 1, h);
return 0;
}
-static int IT_load (Reader *h) {
+static int IT_load (Stream *h) {
int m, i, j;
old_thing_t t;
switch (blk.t) {
return 0;
}
-static int SW_load (Reader *h) {
+static int SW_load (Stream *h) {
int i;
switch(blk.t) {
case MB_SWITCH2:
}
}
-static int read_array (void *p, Reader *h) {
+static int read_array (void *p, Stream *h) {
void *buf;
switch (blk.st) {
case 0:
return 1;
}
-static int W_load (Reader *h) {
+static int W_load (Stream *h) {
int i;
char s[8];
switch (blk.t) {
return 0;
}
-int MAP_load (Reader *r) {
+int MAP_load (Stream *r) {
assert(r != NULL);
int ok = 0;
map_header_t hdr;
blk.t = stream_read16(r);
blk.st = stream_read16(r);
blk.sz = stream_read32(r);
- long off = r->getpos(r) + blk.sz;
+ long off = stream_getpos(r) + blk.sz;
switch (blk.t) {
case MB_MUSIC:
ok = G_load(r);
logo("Unknown block %d(%d)\n", blk.t, blk.st);
return 0; // error
}
- r->setpos(r, off);
+ stream_setpos(r, off);
}
} else {
logo("Invalid map header\n");
diff --git a/src/map.h b/src/map.h
index 4e1470652445a3e5c886266d994ab88bb3c57c6c..3b0ff10e337ee69b7bb701d2074c77a57550583e 100644 (file)
--- a/src/map.h
+++ b/src/map.h
#include "common/streams.h"
-int MAP_load (Reader *r);
+int MAP_load (Stream *r);
#endif /* MAP_H_INCLUDED */
diff --git a/src/save.c b/src/save.c
index c5dcb76d66404c84dff790595cfed8fccc012c73..a40160df359f3ab815a7e10754888ce2d25be655 100644 (file)
--- a/src/save.c
+++ b/src/save.c
char savname[SAVE_MAX][SAVE_MAXLEN];
char savok[SAVE_MAX];
-static void DOT_savegame (Writer *h) {
+static void DOT_savegame (Stream *h) {
int i, n;
for (i = n = 0; i < MAXDOT; ++i) {
if (dot[i].t) {
}
}
-static void DOT_loadgame (Reader *h) {
+static void DOT_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; i++) {
}
}
-static void FX_savegame (Writer *h) {
+static void FX_savegame (Stream *h) {
int i, n;
for (i = n = 0; i < MAXFX; ++i) {
if (fx[i].t) {
}
}
-static void FX_loadgame (Reader *h) {
+static void FX_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; i++) {
}
}
-static void G_savegame (Writer *h) {
+static void G_savegame (Stream *h) {
stream_write8(_2pl, h);
stream_write8(g_dm, h);
stream_write8(g_exit, h);
stream_write(g_music, 8, 1, h);
}
-static void G_loadgame (Reader *h) {
+static void G_loadgame (Stream *h) {
_2pl = stream_read8(h);
g_dm = stream_read8(h);
g_exit = stream_read8(h);
MUS_load(g_music);
}
-static void IT_savegame (Writer *h) {
+static void IT_savegame (Stream *h) {
int i, n;
for (n = MAXITEM - 1; n >= 0 && it[n].t == 0; n--) {
// empty
stream_write32(itm_rtime, h);
}
-static void IT_loadgame (Reader *h) {
+static void IT_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; i++) {
itm_rtime = stream_read32(h);
}
-static void MN_savegame (Writer *h) {
+static void MN_savegame (Stream *h) {
int i, n;
for (n = MAXMN - 1; n >= 0 && mn[n].t == 0; n--) {
// empty
stream_write32(gsndt, h);
}
-static void MN_loadgame (Reader *h) {
+static void MN_loadgame (Stream *h) {
int i, n, c;
n = stream_read32(h);
for (i = 0; i < n; i++) {
}
}
-static void PL_save_player (player_t *p, Writer *h) {
+static void PL_save_player (player_t *p, Stream *h) {
stream_write32(p->o.x, h);
stream_write32(p->o.y, h);
stream_write32(p->o.xv, h);
// k* not saved
}
-static void PL_savegame (Writer *h) {
+static void PL_savegame (Stream *h) {
PL_save_player(&pl1, h);
if (_2pl) {
PL_save_player(&pl2, h);
stream_write8(p_immortal, h);
}
-static void PL_load_player (player_t *p, Reader *h) {
+static void PL_load_player (player_t *p, Stream *h) {
p->o.x = stream_read32(h);
p->o.y = stream_read32(h);
p->o.xv = stream_read32(h);
// k* not saved
}
-static void PL_loadgame (Reader *h) {
+static void PL_loadgame (Stream *h) {
PL_load_player(&pl1, h);
if (_2pl) {
PL_load_player(&pl2, h);
p_immortal = stream_read8(h);
}
-static void SMK_savegame (Writer *h) {
+static void SMK_savegame (Stream *h) {
int i, n;
for (i = n = 0; i < MAXSMOK; ++i) {
if (sm[i].t) {
}
}
-static void SMK_loadgame (Reader *h) {
+static void SMK_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; ++i) {
}
}
-static void SW_savegame (Writer *h) {
+static void SW_savegame (Stream *h) {
int i, n;
for (n = MAXSW - 1; n >= 0 && sw[n].t == 0; n--) {
// empty
stream_write32(sw_secrets, h);
}
-static void SW_loadgame (Reader *h) {
+static void SW_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; i++) {
sw_secrets = stream_read32(h);
}
-static void W_savegame (Writer* h) {
+static void W_savegame (Stream* h) {
char s[8];
int i;
stream_write32(sky_type, h);
stream_write(fldf, FLDW*FLDH, 1, h);
}
-static void W_loadgame (Reader *h) {
+static void W_loadgame (Stream *h) {
int i;
char s[8];
sky_type = stream_read32(h);
stream_read(fldf, FLDW*FLDH, 1, h);
}
-static void WP_savegame (Writer *h) {
+static void WP_savegame (Stream *h) {
int i, n;
for (n = MAXWPN - 1; n >= 0 && wp[n].t == 0; n--) {
// empty
}
}
-static void WP_loadgame (Reader *h) {
+static void WP_loadgame (Stream *h) {
int i, n;
n = stream_read32(h);
for (i = 0; i < n; i++) {
return p;
}
-void SAVE_save (Writer *w, const char name[24]) {
+void SAVE_save (Stream *w, const char name[24]) {
assert(w != NULL);
stream_write(name, 24, 1, w); // slot name
stream_write16(3, w); // version
}
void F_savegame (int n, char *s) {
- FILE_Writer wr;
+ FILE_Stream wr;
char *p = getsavfpname(n, 0);
- if (FILE_OpenWriter(&wr, p)) {
+ if (FILE_Open(&wr, p, "wb")) {
SAVE_save(&wr.base, s);
- FILE_CloseWriter(&wr);
+ FILE_Close(&wr);
}
}
-void SAVE_load (Reader *h) {
+void SAVE_load (Stream *h) {
int16_t version;
- h->setpos(h, 24); // skip name
+ stream_setpos(h, 24); // skip name
version = stream_read16(h);
if (version == 3) {
G_loadgame(h);
void F_getsavnames (void) {
int i;
char *p;
- FILE_Reader rd;
+ FILE_Stream rd;
int16_t version;
for (i = 0; i < 7; ++i) {
p = getsavfpname(i, 1);
memset(savname[i], 0, 24);
savok[i] = 0;
- if (FILE_OpenReader(&rd, p)) {
+ if (FILE_Open(&rd, p, "rb")) {
version = -1;
stream_read(savname[i], 24, 1, &rd.base);
version = stream_read16(&rd.base);
savname[i][23] = 0;
savok[i] = version == 3;
- FILE_CloseReader(&rd);
+ FILE_Close(&rd);
}
}
}
void F_loadgame (int n) {
- FILE_Reader rd;
+ FILE_Stream rd;
char *p = getsavfpname(n, 1);
- if (FILE_OpenReader(&rd, p)) {
+ if (FILE_Open(&rd, p, "rb")) {
SAVE_load(&rd.base);
- FILE_CloseReader(&rd);
+ FILE_Close(&rd);
}
}
diff --git a/src/save.h b/src/save.h
index b3848ab293289e98ffc65de2f6b2ec75eadfb114..5f98bfee54ea34ca0c04c2ef4f7b6ea08487b736 100644 (file)
--- a/src/save.h
+++ b/src/save.h
void F_savegame (int n, char *s);
void F_loadgame (int n);
-void SAVE_save (Writer *w, const char name[24]);
-void SAVE_load (Reader *r);
+void SAVE_save (Stream *w, const char name[24]);
+void SAVE_load (Stream *r);
#endif /* SAVE_H_INCLUDED */