From: DeaDDooMER Date: Wed, 7 Apr 2021 10:00:29 +0000 (+0300) Subject: save: move file access to files.c X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=3d9acf8660ae1f0148c8578b8e9ca168795deec4;p=flatwaifu.git save: move file access to files.c --- diff --git a/src/files.c b/src/files.c index 8189375..2eab3a8 100644 --- a/src/files.c +++ b/src/files.c @@ -24,6 +24,11 @@ #include "error.h" #include "map.h" // MAP_load +#include "save.h" // SAVE_getname + +#ifdef UNIX +# include +#endif #include "common/streams.h" #include "common/files.h" @@ -31,6 +36,10 @@ #include "common/cp866.h" int d_start, d_end; + +char savname[SAVE_MAX][SAVE_MAXLEN]; +char savok[SAVE_MAX]; + static int m_start, m_end; static int s_start, s_end; @@ -159,3 +168,57 @@ void F_loadmap (char n[8]) { ERR_fatal("Failed to load map: resource %.8s not found", n); } } + +static char *getsavfpname (int n, int ro) { + static char fn[] = "savgame0.dat"; + static char p[100]; + fn[7] = n + '0'; +#ifdef UNIX + char *e = getenv("HOME"); + strncpy(p, e, 60); + strcat(p, "/.flatwaifu"); + if (!ro) { + mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + } + strcat(p, "/"); + strcat(p, fn); +#else + strcpy(p, fn); +#endif + return p; +} + +void F_getsavnames (void) { + FILE_Stream rd; + for (int i = 0; i < SAVE_MAX; ++i) { + savok[i] = 0; + char *p = getsavfpname(i, 1); + if (FILE_Open(&rd, p, "rb")) { + savok[i] = SAVE_getname(&rd.base, savname[i]); + FILE_Close(&rd); + } + if (!savok[i]) { + memset(savname[i], 0, 24); + } else { + savname[i][23] = 0; + } + } +} + +void F_loadgame (int n) { + FILE_Stream rd; + char *p = getsavfpname(n, 1); + if (FILE_Open(&rd, p, "rb")) { + SAVE_load(&rd.base); + FILE_Close(&rd); + } +} + +void F_savegame (int n, char *s) { + FILE_Stream wr; + char *p = getsavfpname(n, 0); + if (FILE_Open(&wr, p, "wb")) { + SAVE_save(&wr.base, s); + FILE_Close(&wr); + } +} diff --git a/src/files.h b/src/files.h index e1b30b5..1fa7db8 100644 --- a/src/files.h +++ b/src/files.h @@ -20,6 +20,11 @@ #include "glob.h" +#define SAVE_MAX 7 +#define SAVE_MAXLEN 24 + +extern char savname[SAVE_MAX][SAVE_MAXLEN]; +extern char savok[SAVE_MAX]; extern int d_start, d_end; void F_startup (void); @@ -32,9 +37,14 @@ int F_getresid (const char n[8]); void F_getresname (char n[8], int r); int F_getsprid (const char n[4], int s, int d, char *dir); int F_getreslen (int r); + void F_nextmus (char *s); void F_randmus (char *s); void F_loadmap (char n[8]); +void F_getsavnames (void); +void F_savegame (int n, char *s); +void F_loadgame (int n); + #endif /* FILES_H_INCLUDED */ diff --git a/src/save.c b/src/save.c index a40160d..a219fdf 100644 --- a/src/save.c +++ b/src/save.c @@ -30,20 +30,12 @@ #include "render.h" #include "music.h" - #include -#ifdef UNIX -# include -#endif #include -#include "files.h" #include "common/streams.h" #include "common/files.h" -char savname[SAVE_MAX][SAVE_MAXLEN]; -char savok[SAVE_MAX]; - static void DOT_savegame (Stream *h) { int i, n; for (i = n = 0; i < MAXDOT; ++i) { @@ -540,25 +532,6 @@ static void WP_loadgame (Stream *h) { } } -static char *getsavfpname (int n, int ro) { - static char fn[] = "savgame0.dat"; - static char p[100]; - fn[7] = n + '0'; -#ifdef UNIX - char *e = getenv("HOME"); - strncpy(p, e, 60); - strcat(p, "/.flatwaifu"); - if (!ro) { - mkdir(p, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - } - strcat(p, "/"); - strcat(p, fn); -#else - strcpy(p, fn); -#endif - return p; -} - void SAVE_save (Stream *w, const char name[24]) { assert(w != NULL); stream_write(name, 24, 1, w); // slot name @@ -575,15 +548,6 @@ void SAVE_save (Stream *w, const char name[24]) { WP_savegame(w); } -void F_savegame (int n, char *s) { - FILE_Stream wr; - char *p = getsavfpname(n, 0); - if (FILE_Open(&wr, p, "wb")) { - SAVE_save(&wr.base, s); - FILE_Close(&wr); - } -} - void SAVE_load (Stream *h) { int16_t version; stream_setpos(h, 24); // skip name @@ -602,31 +566,8 @@ void SAVE_load (Stream *h) { } } -void F_getsavnames (void) { - int i; - char *p; - 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_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_Close(&rd); - } - } -} - -void F_loadgame (int n) { - FILE_Stream rd; - char *p = getsavfpname(n, 1); - if (FILE_Open(&rd, p, "rb")) { - SAVE_load(&rd.base); - FILE_Close(&rd); - } +int SAVE_getname (Stream *r, char name[24]) { + stream_read(name, 24, 1, r); + int16_t version = stream_read16(r); + return version == 3; } diff --git a/src/save.h b/src/save.h index 5f98bfe..272107f 100644 --- a/src/save.h +++ b/src/save.h @@ -20,16 +20,7 @@ #include "common/streams.h" -#define SAVE_MAX 7 -#define SAVE_MAXLEN 24 - -extern char savname[SAVE_MAX][SAVE_MAXLEN]; -extern char savok[SAVE_MAX]; - -void F_getsavnames (void); -void F_savegame (int n, char *s); -void F_loadgame (int n); - +int SAVE_getname (Stream *r, char name[24]); void SAVE_save (Stream *w, const char name[24]); void SAVE_load (Stream *r);