diff xrogue/state.c @ 279:d3968e9cb98d

Use C stdio functions for score files and save files. Switching from Unix file descriptor operations to C standard FILE* functions will reduce portability problems.
author John "Elwin" Edwards
date Fri, 15 Sep 2017 19:57:54 -0400
parents 3d4252fa2ed3
children 6376b514a30b
line wrap: on
line diff
--- a/xrogue/state.c	Sun Sep 10 17:30:13 2017 -0400
+++ b/xrogue/state.c	Fri Sep 15 19:57:54 2017 -0400
@@ -96,13 +96,13 @@
 int save_debug = FALSE;
 #define DBG(x)   {if (save_debug) rsPrintf x;}
 
-bool rs_read_new_string(int inf, char **s);
+bool rs_read_new_string(FILE *inf, char **s);
 int find_list_ptr(struct linked_list *l, void *ptr);
 bool rs_write_coord_list(FILE *savef, struct linked_list *l);
-bool rs_read_coord_list(int inf, struct linked_list **list);
+bool rs_read_coord_list(FILE *inf, struct linked_list **list);
 int list_size(struct linked_list *l);
 bool rs_write_object_list(FILE *savef, struct linked_list *l);
-bool rs_read_object_list(int inf, struct linked_list **list);
+bool rs_read_object_list(FILE *inf, struct linked_list **list);
 
 int
 rsPrintf(char *fmt, ...)
@@ -152,7 +152,7 @@
 int end_of_file = FALSE;
 
 bool
-rs_read(int inf, void *ptr, size_t size)
+rs_read(FILE *inf, void *ptr, size_t size)
 {
     int actual;
     end_of_file =FALSE;
@@ -284,7 +284,7 @@
 }
 
 bool
-rs_read_int(int inf, int *i)
+rs_read_int(FILE *inf, int *i)
 {
     char bytes[4];
     int  input;
@@ -307,7 +307,7 @@
 }
 
 bool
-rs_read_uint(int inf, unsigned int *i)
+rs_read_uint(FILE *inf, unsigned int *i)
 {
     char bytes[4];
     int  input;
@@ -330,7 +330,7 @@
 }
 
 bool
-rs_read_ulong(int inf, unsigned long *i)
+rs_read_ulong(FILE *inf, unsigned long *i)
 {
     char bytes[4];
     unsigned long input;
@@ -355,7 +355,7 @@
 }
 
 bool
-rs_read_long(int inf, long *i)
+rs_read_long(FILE *inf, long *i)
 {
     char bytes[4];
     long input;
@@ -380,7 +380,7 @@
 }
 
 bool
-rs_read_boolean(int inf, bool *i)
+rs_read_boolean(FILE *inf, bool *i)
 {
     char buf;
     
@@ -423,7 +423,7 @@
 }
 
 bool
-rs_read_short(int inf, short *s)
+rs_read_short(FILE *inf, short *s)
 {
     char  bytes[2];
     short input;
@@ -496,7 +496,7 @@
 }
 
 bool
-rs_read_ints(int inf, int *i, int count)
+rs_read_ints(FILE *inf, int *i, int count)
 {
     int n=0,value=0;
     
@@ -515,7 +515,7 @@
 }
 
 bool
-rs_read_shorts(int inf, short *i, int count)
+rs_read_shorts(FILE *inf, short *i, int count)
 {
     int n=0,value=0;
     
@@ -534,7 +534,7 @@
 }
 
 bool
-rs_read_longs(int inf, long *i, int count)
+rs_read_longs(FILE *inf, long *i, int count)
 {
     int n=0,value=0;
     
@@ -553,7 +553,7 @@
 }
 
 bool
-rs_read_ulongs(int inf, unsigned long *i, int count)
+rs_read_ulongs(FILE *inf, unsigned long *i, int count)
 {
     int n=0,value=0;
     
@@ -572,7 +572,7 @@
 }
 
 bool
-rs_read_booleans(int inf, bool *i, int count)
+rs_read_booleans(FILE *inf, bool *i, int count)
 {
     int n=0,value=0;
     
@@ -611,7 +611,7 @@
 }
 
 bool
-rs_read_levtype(int inf, LEVTYPE *l)
+rs_read_levtype(FILE *inf, LEVTYPE *l)
 {
     int lt;
     
@@ -640,7 +640,7 @@
 }
 
 bool
-rs_read_char(int inf, char *c)
+rs_read_char(FILE *inf, char *c)
 {
     rs_read(inf, c, 1);
     
@@ -657,7 +657,7 @@
 }
 
 bool
-rs_read_uchar(int inf, unsigned char *c)
+rs_read_uchar(FILE *inf, unsigned char *c)
 {
     rs_read(inf, c, 1);
     
@@ -678,7 +678,7 @@
 }
 
 bool
-rs_read_string_index(int inf, struct words master[], int maxindex, char **str)
+rs_read_string_index(FILE *inf, struct words master[], int maxindex, char **str)
 {
     int i;
 
@@ -718,7 +718,7 @@
 }
  
 bool
-rs_read_scrolls(int inf)
+rs_read_scrolls(FILE *inf)
 {
     int i;
 
@@ -747,7 +747,7 @@
 }
 
 bool
-rs_read_potions(int inf)
+rs_read_potions(FILE *inf)
 {
     int i;
 
@@ -777,7 +777,7 @@
 }
 
 bool
-rs_read_rings(int inf)
+rs_read_rings(FILE *inf)
 {
     int i;
 
@@ -807,7 +807,7 @@
 }
 
 bool
-rs_read_misc(int inf)
+rs_read_misc(FILE *inf)
 {
     int i;
 
@@ -859,7 +859,7 @@
 }
         
 bool
-rs_read_sticks(int inf)
+rs_read_sticks(FILE *inf)
 {
     int i = 0, list = 0;
 
@@ -884,7 +884,7 @@
 }
     
 bool
-rs_read_string(int inf, char *s, int max)
+rs_read_string(FILE *inf, char *s, int max)
 {
     int len = 0;
 
@@ -904,7 +904,7 @@
 }
 
 bool
-rs_read_new_string(int inf, char **s)
+rs_read_new_string(FILE *inf, char **s)
 {
     int len=0;
     char *buf=0;
@@ -966,7 +966,7 @@
 }
 
 bool
-rs_read_words(int inf, struct words *w, int count)
+rs_read_words(FILE *inf, struct words *w, int count)
 {
     int n = 0;
     int value = 0;
@@ -989,7 +989,7 @@
 }
 
 bool
-rs_read_new_strings(int inf, char **s, int count)
+rs_read_new_strings(FILE *inf, char **s, int count)
 {
     int len   = 0;
     int n     = 0;
@@ -1034,7 +1034,7 @@
 }
 
 bool
-rs_read_coord(int inf, coord *c)
+rs_read_coord(FILE *inf, coord *c)
 {
     rs_read_int(inf,&c->x);
     rs_read_int(inf,&c->y);
@@ -1233,7 +1233,7 @@
 }       
 
 bool
-rs_read_daemons(int inf, struct delayed_action *d_list, int count)
+rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count)
 {
     int i = 0;
     int func = 0;
@@ -1438,7 +1438,7 @@
 }
 
 bool
-rs_read_rooms(int inf, struct room *r, int count)
+rs_read_rooms(FILE *inf, struct room *r, int count)
 {
     int value = 0, n = 0, i = 0, index = 0, id = 0;
     struct linked_list *fires=NULL, *item = NULL;
@@ -1551,7 +1551,7 @@
 }
 
 bool
-rs_read_object(int inf, struct object *o)
+rs_read_object(FILE *inf, struct object *o)
 {
     int id;
 
@@ -1612,7 +1612,7 @@
 }
 
 bool
-rs_read_stats(int inf, struct stats *s)
+rs_read_stats(FILE *inf, struct stats *s)
 {
     int id;
 
@@ -1666,7 +1666,7 @@
 }
 
 bool
-rs_read_mstats(int inf, struct mstats *s)
+rs_read_mstats(FILE *inf, struct mstats *s)
 {
     int id;
 
@@ -1726,7 +1726,7 @@
 }
 
 bool
-rs_read_init_weps(int inf, struct init_weps *w,int count)
+rs_read_init_weps(FILE *inf, struct init_weps *w,int count)
 {
     int id,value,i;
 
@@ -1773,7 +1773,7 @@
 }
 
 bool
-rs_read_init_armor(int inf, struct init_armor *a,int count)
+rs_read_init_armor(FILE *inf, struct init_armor *a,int count)
 {
     int id,value,i;
 
@@ -1810,7 +1810,7 @@
 }
 
 bool
-rs_read_spells(int inf, struct spells *s,int count)
+rs_read_spells(FILE *inf, struct spells *s,int count)
 {
     int id,value,i;
 
@@ -1838,7 +1838,7 @@
 }
 
 bool
-rs_read_item_list(int inf, struct item_list *i)
+rs_read_item_list(FILE *inf, struct item_list *i)
 {
     int id;
 
@@ -1860,7 +1860,7 @@
 }
 
 bool
-rs_read_h_list(int inf, struct h_list *h)
+rs_read_h_list(FILE *inf, struct h_list *h)
 {
     int id;
 
@@ -1889,7 +1889,7 @@
 }
 
 bool
-rs_read_death_types(int inf, struct death_type *d, int count)
+rs_read_death_types(FILE *inf, struct death_type *d, int count)
 {
     int id,value,i;
 
@@ -1935,7 +1935,7 @@
 }
 
 bool
-rs_read_character_types(int inf, struct character_types *c,int count)
+rs_read_character_types(FILE *inf, struct character_types *c,int count)
 {
     int id,value,i;
 
@@ -1984,7 +1984,7 @@
 }
 
 bool
-rs_read_traps(int inf, struct trap *trap, int count)
+rs_read_traps(FILE *inf, struct trap *trap, int count)
 {
     int id = 0, value = 0, n = 0;
 
@@ -2054,7 +2054,7 @@
 }
 
 bool
-rs_read_monsters(int inf, struct monster *m, int count)
+rs_read_monsters(FILE *inf, struct monster *m, int count)
 {
     int id = 0, value = 0, n = 0;
     char buffer[1024];
@@ -2130,7 +2130,7 @@
 }
 
 bool
-rs_read_coord_list(int inf, struct linked_list **list)
+rs_read_coord_list(FILE *inf, struct linked_list **list)
 {
     int id;
     int i, cnt;
@@ -2190,7 +2190,7 @@
 }
 
 bool
-rs_read_object_list(int inf, struct linked_list **list)
+rs_read_object_list(FILE *inf, struct linked_list **list)
 {
     int id;
     int i, cnt;
@@ -2398,7 +2398,7 @@
 }
 
 bool
-rs_read_thing(int inf, struct thing *t)
+rs_read_thing(FILE *inf, struct thing *t)
 {
     int id;
     int listid = 0, index = -1;
@@ -2542,7 +2542,7 @@
 }
 
 bool
-rs_read_monster_list(int inf, struct linked_list **list)
+rs_read_monster_list(FILE *inf, struct linked_list **list)
 {
     int id;
     int i, cnt;
@@ -2605,7 +2605,7 @@
 }
 
 bool
-rs_read_magic_items(int inf, struct magic_item *mi, int count)
+rs_read_magic_items(FILE *inf, struct magic_item *mi, int count)
 {
     int id;
     int n;
@@ -2664,7 +2664,7 @@
 }
 
 bool
-rs_read_window(int inf, WINDOW *win)
+rs_read_window(FILE *inf, WINDOW *win)
 {
     int row,col,maxlines,maxcols,value,width,height;
     
@@ -2849,7 +2849,7 @@
 }
 
 bool
-rs_restore_file(int inf)
+rs_restore_file(FILE *inf)
 {
     int weapon, armor, ring, misc, room = -1,i,checkpoint;
     int endian = 0x01020304;
@@ -3034,9 +3034,8 @@
 rs_read_scorefile(FILE *savef, struct sc_ent *entries, int count)
 {
     int i,available = 0;
-    int sfd = md_fileno(savef);
-
-    rs_read_int(sfd, &available);
+
+    rs_read_int(savef, &available);
 
     if (end_of_file)
        return(-1);
@@ -3046,15 +3045,15 @@
 
     for(i = 0; i < count; i++)
     {
-        rs_read_ulong(sfd, &entries[i].sc_score);
-        rs_read(sfd, entries[i].sc_name, sizeof(entries[i].sc_name));
-        rs_read(sfd, entries[i].sc_system, sizeof(entries[i].sc_system));
-        rs_read(sfd, entries[i].sc_login, sizeof(entries[i].sc_login));
-        rs_read_short(sfd, &entries[i].sc_flags);
-        rs_read_short(sfd, &entries[i].sc_level);
-        rs_read_short(sfd, &entries[i].sc_ctype);
-        rs_read_short(sfd, &entries[i].sc_monster);
-        rs_read_short(sfd, &entries[i].sc_quest);
+        rs_read_ulong(savef, &entries[i].sc_score);
+        rs_read(savef, entries[i].sc_name, sizeof(entries[i].sc_name));
+        rs_read(savef, entries[i].sc_system, sizeof(entries[i].sc_system));
+        rs_read(savef, entries[i].sc_login, sizeof(entries[i].sc_login));
+        rs_read_short(savef, &entries[i].sc_flags);
+        rs_read_short(savef, &entries[i].sc_level);
+        rs_read_short(savef, &entries[i].sc_ctype);
+        rs_read_short(savef, &entries[i].sc_monster);
+        rs_read_short(savef, &entries[i].sc_quest);
     }
 
     return(0);