Store plname in save files (and restore it).

Move get_saved_games() functionality to files.c
Use moved get_saved_games() functionality in Qt windowport.
[also some non-enabled perminv code in Qt windowport]
This commit is contained in:
warwick
2002-07-22 06:25:52 +00:00
parent 6bc604ecf3
commit 16b7d4a099
6 changed files with 95 additions and 48 deletions

View File

@@ -642,6 +642,8 @@ E void NDECL(read_wizkit);
#endif
E void FDECL(paniclog, (const char *, const char *));
E int FDECL(validate_prefix_locations, (char *));
E char** NDECL(get_saved_games);
E void FDECL(free_saved_games, (char**));
/* ### fountain.c ### */

View File

@@ -13,7 +13,7 @@
* Incrementing EDITLEVEL can be used to force invalidation of old bones
* and save files.
*/
#define EDITLEVEL 0
#define EDITLEVEL 1
#define COPYRIGHT_BANNER_A \
"NetHack, Copyright 1985-2002"

View File

@@ -19,6 +19,10 @@
#include <errno.h>
#endif
#if defined(UNIX)
#include <dirent.h>
#endif
#if defined(UNIX) || defined(VMS)
#include <errno.h>
# ifndef SKIP_ERRNO
@@ -804,6 +808,77 @@ restore_saved_game()
return fd;
}
static char*
plname_from_file(filename)
const char* filename;
{
int fd;
char* result = 0;
Strcpy(SAVEF,filename);
#ifdef COMPRESS_EXTENSION
SAVEF[strlen(SAVEF)-strlen(COMPRESS_EXTENSION)] = '\0';
#endif
uncompress(SAVEF);
if ((fd = open_savefile()) >= 0) {
if (uptodate(fd, filename)) {
char tplname[PL_NSIZ];
mread(fd, (genericptr_t) tplname, PL_NSIZ);
result = strdup(tplname);
}
(void) close(fd);
}
compress(SAVEF);
return result;
}
char**
get_saved_games()
{
#ifdef UNIX
int myuid=getuid();
struct dirent **namelist;
int n = scandir("save", &namelist, 0, alphasort);;
if ( n > 0 ) {
int i,j=0;
char** result = (char**)malloc((n+1)*sizeof(char*)); /* at most */
for (i=0; i<n; i++) {
int uid;
char name[NAME_MAX];
if ( sscanf( namelist[i]->d_name, "%d%s", &uid, name ) == 2 ) {
if ( uid == myuid ) {
char filename[BUFSZ];
char* r;
Sprintf(filename,"save/%d%s",uid,name);
r = plname_from_file(filename);
if ( r ) {
result[j++] = r;
}
}
}
}
result[j++] = 0;
return result;
} else
#endif
{
return 0;
}
}
void
free_saved_games(saved)
char** saved;
{
if ( saved ) {
int i=0;
while (saved[i]) free(saved[i++]);
free(saved);
}
}
/* ---------- END SAVE FILE HANDLING ----------- */

View File

@@ -542,6 +542,8 @@ register int fd;
int rtmp;
struct obj *otmp;
mread(fd, (genericptr_t) plname, PL_NSIZ);
restoring = TRUE;
getlev(fd, 0, (xchar)0, FALSE);
if (!restgamestate(fd, &stuckid, &steedid)) {
@@ -621,6 +623,7 @@ register int fd;
(void) lseek(fd, (off_t)0, 0);
#endif
(void) uptodate(fd, (char *)0); /* skip version info */
mread(fd, (genericptr_t) plname, PL_NSIZ);
getlev(fd, 0, (xchar)0, FALSE);
(void) close(fd);

View File

@@ -204,6 +204,7 @@ dosave0()
#endif /* MFLOPPY */
store_version(fd);
bwrite(fd, (genericptr_t) plname, PL_NSIZ);
ustuck_id = (u.ustuck ? u.ustuck->m_id : 0);
#ifdef STEED
usteed_id = (u.usteed ? u.usteed->m_id : 0);
@@ -372,6 +373,7 @@ savestateinlock()
(void) write(fd, (genericptr_t) &currlev, sizeof(currlev));
save_savefile_name(fd);
store_version(fd);
bwrite(fd, (genericptr_t) plname, PL_NSIZ);
ustuck_id = (u.ustuck ? u.ustuck->m_id : 0);
#ifdef STEED
usteed_id = (u.usteed ? u.usteed->m_id : 0);

View File

@@ -2844,7 +2844,10 @@ int NetHackQtMenuWindow::SelectMenu(int h, MENU_ITEM_P **menu_list)
if (dialog->result()<0)
qApp->enter_loop();
}
dialog->hide();
//if ( (nhid != WIN_INVEN || !flags.perm_invent) ) // doesn't work yet
{
dialog->hide();
}
int result=dialog->result();
// Consume ^M (which QDialog steals for default button)
@@ -4463,57 +4466,13 @@ int NetHackQtSavedGameSelector::choose()
return exec()-2;
}
static char** get_saved_names()
{
int myuid=getuid();
struct dirent **namelist;
int n = scandir("save", &namelist, 0, alphasort);;
if ( n > 0 ) {
int i,j=0;
char** result = (char**)malloc((n+1)*sizeof(char*)); /* at most */
for (i=0; i<n; i++) {
int uid;
char name[NAME_MAX];
if ( sscanf( namelist[i]->d_name, "%d%s", &uid, name ) == 2 ) {
if ( uid == myuid ) {
/* Name should be stored in save file, but currently we
have to extract it from the filename, which loses
information (eg. "/", "_", and "." characters are lost. */
int k;
char* end = strstr(name,".gz");
if ( !end ) end = strstr(name,".Z");
if ( end ) *end = 0;
/* "_" most likely means " ", which certainly looks nicer */
for (k=0; name[k]; k++)
if ( name[k]=='_' )
name[k]=' ';
result[j++] = strdup(name);
}
}
}
result[j++] = 0;
return result;
} else {
return 0;
}
}
static void free_saved_names(char** saved)
{
if ( saved ) {
int i=0;
while (saved[i]) free(saved[i++]);
free(saved);
}
}
void NetHackQtBind::qt_askname()
{
have_asked = TRUE;
// We do it all here, and nothing in askname
char** saved = get_saved_names();
char** saved = get_saved_games();
int ch = -1;
if ( saved && *saved ) {
if ( splash ) splash->hide();
@@ -4522,7 +4481,7 @@ void NetHackQtBind::qt_askname()
if ( ch >= 0 )
strcpy(plname,saved[ch]);
}
free_saved_names(saved);
free_saved_games(saved);
switch (ch) {
case -1:
@@ -4606,6 +4565,8 @@ winid NetHackQtBind::qt_create_nhwindow(int type)
window=new NetHackQtTextWindow(keybuffer);
}
window->nhid = id;
// Note: use of isHidden does not work with Qt 2.1
if ( splash
#if QT_VERSION >= 300
@@ -4736,6 +4697,10 @@ void NetHackQtBind::qt_update_inventory()
{
if (main)
main->updateInventory();
/* doesn't work yet
if (program_state.something_worth_saving && flags.perm_invent)
display_inventory(NULL, FALSE);
*/
}
void NetHackQtBind::qt_mark_synch()