changeset 145:aac28331e71d

Advanced Rogue family: fix the "score" option. Changing the score file (when permitted) now works again, closing the old score file and opening the new one.
author John "Elwin" Edwards
date Wed, 20 May 2015 08:42:17 -0400
parents 708bb2dea17c
children 5a77931393f4
files arogue5/main.c arogue5/mdport.c arogue5/options.c arogue5/rogue.c arogue5/rogue.h arogue7/main.c arogue7/mdport.c arogue7/options.c arogue7/rogue.c arogue7/rogue.h xrogue/main.c xrogue/options.c xrogue/rogue.c xrogue/rogue.h
diffstat 14 files changed, 105 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/arogue5/main.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue5/main.c	Wed May 20 08:42:17 2015 -0400
@@ -16,7 +16,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
-#include <sys/types.h>
 #include <sys/stat.h>
 #include <limits.h>
 #include <signal.h>
@@ -43,8 +42,6 @@
 };
 #define NFRUIT (sizeof(funfruit) / sizeof (char *))
 
-int scorefd = -1;
-FILE *logfile = NULL;
 void open_records(void);
 
 main(argc, argv, envp)
@@ -255,7 +252,7 @@
 open_records(void)
 {
     if (scorefd == -1)
-        scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
+        md_reopen_score();
 #ifdef LOGFILE
     if (logfile == NULL)
         logfile = fopen(LOGFILE, "a");
--- a/arogue5/mdport.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue5/mdport.c	Wed May 20 08:42:17 2015 -0400
@@ -695,6 +695,17 @@
 #endif
 }
 
+extern int scorefd;
+extern char score_file[];
+
+void
+md_reopen_score(void)
+{
+    if (scorefd > 0)
+        close(scorefd);
+    scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
+}
+
 /*
     Cursor/Keypad Support
 
--- a/arogue5/options.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue5/options.c	Wed May 20 08:42:17 2015 -0400
@@ -40,6 +40,7 @@
 	put_str(),
 	get_str(),
 	get_restr(),
+	get_score(),
 	put_abil(),
 	get_abil(),
 	get_quest(),
@@ -65,7 +66,7 @@
     {"file",	 "Save file: ",
 		(int *) file_name,	put_str,	get_restr	},
     {"score",	 "Score file: ",
-		(int *) score_file,	put_str,	get_restr	},
+		(int *) score_file,	put_str,	get_score	},
     {"class",	"Character class: ",
 		(int *)&char_type,	put_abil,	get_abil	},
     {"quest",	"Quest item: ",
@@ -87,6 +88,26 @@
         return get_str(optstr, win);
 }
 
+/* For the score file, which must be opened. */
+int get_score(char *optstr, WINDOW *win)
+{
+    char old_score_file[LINELEN];
+    int status;
+
+    if (use_savedir)
+        return get_restr(optstr, win);
+
+    strcpy(old_score_file, optstr);
+    status = get_str(optstr, win);
+    if (status != NORM)
+        return status;
+    if (strcmp(old_score_file, optstr))
+    {
+        md_reopen_score();
+    }
+    return status;
+}
+
 /*
  * The ability field is read-only
  */
--- a/arogue5/rogue.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue5/rogue.c	Wed May 20 08:42:17 2015 -0400
@@ -64,6 +64,7 @@
 int spell_power = 0;
 int turns = 0;				/* Number of turns player has taken */
 int quest_item = 0;			/* Item player is looking for */
+int scorefd = -1;			/* File descriptor for the scorefile */
 char nfloors = -1;			/* Number of floors in this dungeon */
 char curpurch[LINELEN*2];		/* name of item ready to buy */
 char PLAYER = VPLAYER;			/* what the player looks like */
@@ -123,6 +124,7 @@
 char *morestr  =	"-- More --";
 char *retstr   =	"[Press return to continue]";
 
+FILE *logfile = NULL;
 
 /*
  * NOTE: the ordering of the points in this array is critical. They MUST
--- a/arogue5/rogue.h	Mon May 18 10:53:22 2015 -0400
+++ b/arogue5/rogue.h	Wed May 20 08:42:17 2015 -0400
@@ -1050,6 +1050,7 @@
 extern int spell_power;			/* Spell power left at this level */
 extern int turns;			/* Number of turns player has taken */
 extern int quest_item;			/* Item hero is looking for */
+extern int scorefd;			/* File descriptor for the scorefile */
 extern int cur_relic[];			/* Current relics */
 extern char take;			/* Thing the rogue is taking */
 extern char prbuf[];			/* Buffer for sprintfs */
@@ -1121,3 +1122,4 @@
 extern char *metal[NMETAL];
 extern char *wood[NWOOD];
 extern coord ch_ret;
+extern FILE *logfile;
--- a/arogue7/main.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue7/main.c	Wed May 20 08:42:17 2015 -0400
@@ -16,8 +16,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <signal.h>
-#include <sys/types.h>
-#include <fcntl.h>
 #ifdef BSD
 #include <sys/time.h>
 #else
@@ -33,8 +31,6 @@
 extern char oldtext[WTXTNUM][WTXTLEN];
 #endif
 
-int scorefd = -1;
-FILE *logfile = NULL;
 void open_records(void);
 
 main(argc, argv, envp)
@@ -553,7 +549,7 @@
 open_records(void)
 {
     if (scorefd == -1)
-        scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
+        md_reopen_score();
 #ifdef LOGFILE
     if (logfile == NULL)
         logfile = fopen(LOGFILE, "a");
--- a/arogue7/mdport.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue7/mdport.c	Wed May 20 08:42:17 2015 -0400
@@ -677,6 +677,17 @@
 #endif
 }
 
+extern int scorefd;
+extern char score_file[];
+
+void
+md_reopen_score(void)
+{
+    if (scorefd > 0)
+        close(scorefd);
+    scorefd = open(score_file, O_RDWR | O_CREAT, 0666);
+}
+
 /*
     Cursor/Keypad Support
 
--- a/arogue7/options.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue7/options.c	Wed May 20 08:42:17 2015 -0400
@@ -50,6 +50,7 @@
 	put_quest();
 
 int get_str_prot(char *opt, WINDOW *win);
+int get_score(char *opt, WINDOW *win);
 bool allowchange(OPTION *op);
 
 OPTION	optlist[] = {
@@ -72,7 +73,7 @@
     {"file",	 "Save file: ",
 		(int *) file_name,	put_str,	get_str_prot	},
     {"score",	 "Score file: ",
-		(int *) score_file,	put_str,	get_str_prot	},
+		(int *) score_file,	put_str,	get_score	},
     {"class",	"Character class: ",
 		(int *)&char_type,	put_abil,	get_abil	},
     {"quest",	"Quest item: ",
@@ -486,6 +487,25 @@
     }
 }
 
+/* When getting the scorefile, the new file must be opened. */
+int
+get_score(char *optstr, WINDOW *win)
+{
+    char old_score_file[LINELEN];
+    int status;
+
+    if (use_savedir)
+        return get_str_prot(optstr, win);
+
+    strcpy(old_score_file, optstr);
+    status = get_str(optstr, win);
+    if (status == NORM && strcmp(old_score_file, optstr))
+    {
+        md_reopen_score();
+    }
+    return status;
+}
+
 bool
 allowchange(OPTION *op)
 {
--- a/arogue7/rogue.c	Mon May 18 10:53:22 2015 -0400
+++ b/arogue7/rogue.c	Wed May 20 08:42:17 2015 -0400
@@ -80,6 +80,7 @@
 int spell_power = 0;
 int turns = 0;				/* Number of turns player has taken */
 int quest_item = 0;			/* Item player is looking for */
+int scorefd = -1;			/* File descriptor for score file */
 int cols = 0;				/* number of columns in terminal */
 int lines = 0;				/* number of lines on the terminal */
 char nfloors = -1;			/* Number of floors in this dungeon */
@@ -133,6 +134,7 @@
 bool in_shell = FALSE; 
 bool daytime = TRUE;
 bool use_savedir = FALSE;
+FILE *logfile = NULL;
 LEVTYPE levtype;			/* type of level i'm on */
 
 char *nothing  =	"Nothing seems to happen.";
--- a/arogue7/rogue.h	Mon May 18 10:53:22 2015 -0400
+++ b/arogue7/rogue.h	Wed May 20 08:42:17 2015 -0400
@@ -1227,6 +1227,7 @@
 extern int spell_power;			/* Spell power left at this level */
 extern int turns;			/* Number of turns player has taken */
 extern int quest_item;			/* Item hero is looking for */
+extern int scorefd;			/* File descriptor for score file */
 extern int cur_relic[];			/* Current relics */
 extern char take;			/* Thing the rogue is taking */
 extern char prbuf[];			/* Buffer for sprintfs */
@@ -1286,6 +1287,7 @@
 extern char *spacemsg;
 extern char *morestr;
 extern char *retstr;
+extern FILE *logfile;
 extern LEVTYPE levtype;
 extern void (*add_abil[NUMABILITIES])(); /* Functions to change abilities */
 extern void (*res_abil[NUMABILITIES])(); /* Functions to change abilities */
--- a/xrogue/main.c	Mon May 18 10:53:22 2015 -0400
+++ b/xrogue/main.c	Wed May 20 08:42:17 2015 -0400
@@ -26,8 +26,6 @@
 #include "network.h"
 #include "rogue.h"
 
-FILE *scorefi = NULL;
-FILE *logfile = NULL;
 void open_records(void);
 
 main(argc, argv, envp)
--- a/xrogue/options.c	Mon May 18 10:53:22 2015 -0400
+++ b/xrogue/options.c	Wed May 20 08:42:17 2015 -0400
@@ -52,6 +52,7 @@
     get_default();
 
 int get_str_prot(char *opt, WINDOW *win);
+int get_score(char *opt, WINDOW *win);
 bool allowchange(OPTION *op);
 
 OPTION  optlist[] = {
@@ -74,7 +75,7 @@
     {"file",    "Save file: ",
                 (int *) file_name,      put_str,        get_str_prot    },
     {"score",   "Score file: ",
-                (int *) score_file,     put_str,        get_str_prot    },
+                (int *) score_file,     put_str,        get_score       },
     {"class",   "Character type: ",
                 (int *) &char_type,     put_abil,       get_abil        },
     {"quest",   "Quest item: ",
@@ -521,6 +522,29 @@
     }
 }
 
+/* When getting the scorefile, the new file must be opened. */
+int
+get_score(char *optstr, WINDOW *win)
+{
+    char old_score_file[LINELEN];
+    int status;
+
+    if (use_savedir)
+        return get_str_prot(optstr, win);
+
+    strcpy(old_score_file, optstr);
+    status = get_str(optstr, win);
+    if (status == NORM && strcmp(old_score_file, optstr))
+    {
+        if (scorefi != NULL)
+            fclose(scorefi);
+        scorefi = fopen(score_file, "rb+");
+        if (scorefi == NULL)
+            scorefi = fopen(score_file, "wb+");
+    }
+    return status;
+}
+
 bool
 allowchange(OPTION *op)
 {
--- a/xrogue/rogue.c	Mon May 18 10:53:22 2015 -0400
+++ b/xrogue/rogue.c	Wed May 20 08:42:17 2015 -0400
@@ -126,6 +126,9 @@
 bool daytime = TRUE;
 bool funfont = FALSE;
 
+FILE *scorefi = NULL;
+FILE *logfile = NULL;
+
 LEVTYPE levtype;           /* what type of level am i'm on? */
 
 char *nothing  =  "Nothing seems to happen. ";
--- a/xrogue/rogue.h	Mon May 18 10:53:22 2015 -0400
+++ b/xrogue/rogue.h	Wed May 20 08:42:17 2015 -0400
@@ -1398,6 +1398,8 @@
 extern char *spacemsg;
 extern char *morestr;
 extern char *retstr;
+extern FILE *scorefi;
+extern FILE *logfile;
 extern LEVTYPE levtype;
 extern int (*add_abil[NUMABILITIES])(); /* Functions to change abilities */
 extern int (*res_abil[NUMABILITIES])(); /* Functions to change abilities */