comparison arogue5/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 b49d8b963df3
children e52a8a7ad4c5
comparison
equal deleted inserted replaced
278:c222f9d56776 279:d3968e9cb98d
62 #include <stdio.h> 62 #include <stdio.h>
63 #include <stdlib.h> 63 #include <stdlib.h>
64 #include <string.h> 64 #include <string.h>
65 #include "rogue.h" 65 #include "rogue.h"
66 66
67 int rs_read_int(int inf, int *i); 67 int rs_read_int(FILE *inf, int *i);
68 int rs_write_int(FILE *savef, int c); 68 int rs_write_int(FILE *savef, int c);
69 int list_size(struct linked_list *l); 69 int list_size(struct linked_list *l);
70 int rs_write_object_list(FILE *savef, struct linked_list *l); 70 int rs_write_object_list(FILE *savef, struct linked_list *l);
71 int rs_read_object_list(int inf, struct linked_list **list); 71 int rs_read_object_list(FILE *inf, struct linked_list **list);
72 72
73 #define READSTAT (format_error || read_error ) 73 #define READSTAT (format_error || read_error )
74 #define WRITESTAT (write_error) 74 #define WRITESTAT (write_error)
75 75
76 static int read_error = FALSE; 76 static int read_error = FALSE;
90 90
91 return(WRITESTAT); 91 return(WRITESTAT);
92 } 92 }
93 93
94 int 94 int
95 rs_read(int inf, void *ptr, size_t size) 95 rs_read(FILE *inf, void *ptr, size_t size)
96 { 96 {
97 if (read_error || format_error) 97 if (read_error || format_error)
98 return(READSTAT); 98 return(READSTAT);
99 99
100 if (encread(ptr, size, inf) != size) 100 if (encread(ptr, size, inf) != size)
113 113
114 return(WRITESTAT); 114 return(WRITESTAT);
115 } 115 }
116 116
117 int 117 int
118 rs_read_uchar(int inf, unsigned char *c) 118 rs_read_uchar(FILE *inf, unsigned char *c)
119 { 119 {
120 if (read_error || format_error) 120 if (read_error || format_error)
121 return(READSTAT); 121 return(READSTAT);
122 122
123 rs_read(inf, c, 1); 123 rs_read(inf, c, 1);
135 135
136 return(WRITESTAT); 136 return(WRITESTAT);
137 } 137 }
138 138
139 int 139 int
140 rs_read_char(int inf, char *c) 140 rs_read_char(FILE *inf, char *c)
141 { 141 {
142 if (read_error || format_error) 142 if (read_error || format_error)
143 return(READSTAT); 143 return(READSTAT);
144 144
145 rs_read(inf, c, 1); 145 rs_read(inf, c, 1);
158 158
159 return(WRITESTAT); 159 return(WRITESTAT);
160 } 160 }
161 161
162 int 162 int
163 rs_read_chars(int inf, char *i, int count) 163 rs_read_chars(FILE *inf, char *i, int count)
164 { 164 {
165 int value = 0; 165 int value = 0;
166 166
167 if (read_error || format_error) 167 if (read_error || format_error)
168 return(READSTAT); 168 return(READSTAT);
199 199
200 return(WRITESTAT); 200 return(WRITESTAT);
201 } 201 }
202 202
203 int 203 int
204 rs_read_int(int inf, int *i) 204 rs_read_int(FILE *inf, int *i)
205 { 205 {
206 unsigned char bytes[4]; 206 unsigned char bytes[4];
207 int input = 0; 207 int input = 0;
208 unsigned char *buf = (unsigned char *)&input; 208 unsigned char *buf = (unsigned char *)&input;
209 209
242 242
243 return(WRITESTAT); 243 return(WRITESTAT);
244 } 244 }
245 245
246 int 246 int
247 rs_read_ints(int inf, int *i, int count) 247 rs_read_ints(FILE *inf, int *i, int count)
248 { 248 {
249 int n, value; 249 int n, value;
250 250
251 if (read_error || format_error) 251 if (read_error || format_error)
252 return(READSTAT); 252 return(READSTAT);
275 275
276 return(WRITESTAT); 276 return(WRITESTAT);
277 } 277 }
278 278
279 int 279 int
280 rs_read_boolean(int inf, bool *i) 280 rs_read_boolean(FILE *inf, bool *i)
281 { 281 {
282 unsigned char buf = 0; 282 unsigned char buf = 0;
283 283
284 if (read_error || format_error) 284 if (read_error || format_error)
285 return(READSTAT); 285 return(READSTAT);
307 307
308 return(WRITESTAT); 308 return(WRITESTAT);
309 } 309 }
310 310
311 int 311 int
312 rs_read_booleans(int inf, bool *i, int count) 312 rs_read_booleans(FILE *inf, bool *i, int count)
313 { 313 {
314 int n = 0, value = 0; 314 int n = 0, value = 0;
315 315
316 if (read_error || format_error) 316 if (read_error || format_error)
317 return(READSTAT); 317 return(READSTAT);
348 348
349 return(WRITESTAT); 349 return(WRITESTAT);
350 } 350 }
351 351
352 int 352 int
353 rs_read_short(int inf, short *i) 353 rs_read_short(FILE *inf, short *i)
354 { 354 {
355 unsigned char bytes[2]; 355 unsigned char bytes[2];
356 short input; 356 short input;
357 unsigned char *buf = (unsigned char *)&input; 357 unsigned char *buf = (unsigned char *)&input;
358 358
389 389
390 return(WRITESTAT); 390 return(WRITESTAT);
391 } 391 }
392 392
393 int 393 int
394 rs_read_shorts(int inf, short *i, int count) 394 rs_read_shorts(FILE *inf, short *i, int count)
395 { 395 {
396 int n = 0, value = 0; 396 int n = 0, value = 0;
397 397
398 if (read_error || format_error) 398 if (read_error || format_error)
399 return(READSTAT); 399 return(READSTAT);
430 430
431 return(WRITESTAT); 431 return(WRITESTAT);
432 } 432 }
433 433
434 int 434 int
435 rs_read_ushort(int inf, unsigned short *i) 435 rs_read_ushort(FILE *inf, unsigned short *i)
436 { 436 {
437 unsigned char bytes[2]; 437 unsigned char bytes[2];
438 unsigned short input; 438 unsigned short input;
439 unsigned char *buf = (unsigned char *)&input; 439 unsigned char *buf = (unsigned char *)&input;
440 440
477 477
478 return(WRITESTAT); 478 return(WRITESTAT);
479 } 479 }
480 480
481 int 481 int
482 rs_read_uint(int inf, unsigned int *i) 482 rs_read_uint(FILE *inf, unsigned int *i)
483 { 483 {
484 unsigned char bytes[4]; 484 unsigned char bytes[4];
485 int input; 485 int input;
486 unsigned char *buf = (unsigned char *)&input; 486 unsigned char *buf = (unsigned char *)&input;
487 487
533 533
534 return(WRITESTAT); 534 return(WRITESTAT);
535 } 535 }
536 536
537 int 537 int
538 rs_read_long(int inf, long *i) 538 rs_read_long(FILE *inf, long *i)
539 { 539 {
540 unsigned char bytes[4]; 540 unsigned char bytes[4];
541 long input; 541 long input;
542 unsigned char *buf = (unsigned char *) &input; 542 unsigned char *buf = (unsigned char *) &input;
543 543
575 575
576 return(WRITESTAT); 576 return(WRITESTAT);
577 } 577 }
578 578
579 int 579 int
580 rs_read_longs(int inf, long *i, int count) 580 rs_read_longs(FILE *inf, long *i, int count)
581 { 581 {
582 int n = 0, value = 0; 582 int n = 0, value = 0;
583 583
584 if (read_error || format_error) 584 if (read_error || format_error)
585 return(READSTAT); 585 return(READSTAT);
625 625
626 return(WRITESTAT); 626 return(WRITESTAT);
627 } 627 }
628 628
629 int 629 int
630 rs_read_ulong(int inf, unsigned long *i) 630 rs_read_ulong(FILE *inf, unsigned long *i)
631 { 631 {
632 unsigned char bytes[4]; 632 unsigned char bytes[4];
633 unsigned long input; 633 unsigned long input;
634 unsigned char *buf = (unsigned char *) &input; 634 unsigned char *buf = (unsigned char *) &input;
635 635
671 671
672 return(WRITESTAT); 672 return(WRITESTAT);
673 } 673 }
674 674
675 int 675 int
676 rs_read_ulongs(int inf, unsigned long *i, int count) 676 rs_read_ulongs(FILE *inf, unsigned long *i, int count)
677 { 677 {
678 int n = 0, value = 0; 678 int n = 0, value = 0;
679 679
680 if (read_error || format_error) 680 if (read_error || format_error)
681 return(READSTAT); 681 return(READSTAT);
702 702
703 return(WRITESTAT); 703 return(WRITESTAT);
704 } 704 }
705 705
706 int 706 int
707 rs_read_marker(int inf, int id) 707 rs_read_marker(FILE *inf, int id)
708 { 708 {
709 int nid; 709 int nid;
710 710
711 if (read_error || format_error) 711 if (read_error || format_error)
712 return(READSTAT); 712 return(READSTAT);
737 737
738 return(WRITESTAT); 738 return(WRITESTAT);
739 } 739 }
740 740
741 int 741 int
742 rs_read_string(int inf, char *s, int max) 742 rs_read_string(FILE *inf, char *s, int max)
743 { 743 {
744 int len = 0; 744 int len = 0;
745 745
746 if (read_error || format_error) 746 if (read_error || format_error)
747 return(READSTAT); 747 return(READSTAT);
755 755
756 return(READSTAT); 756 return(READSTAT);
757 } 757 }
758 758
759 int 759 int
760 rs_read_new_string(int inf, char **s) 760 rs_read_new_string(FILE *inf, char **s)
761 { 761 {
762 int len=0; 762 int len=0;
763 char *buf=0; 763 char *buf=0;
764 764
765 if (read_error || format_error) 765 if (read_error || format_error)
800 800
801 return(WRITESTAT); 801 return(WRITESTAT);
802 } 802 }
803 803
804 int 804 int
805 rs_read_strings(int inf, char **s, int count, int max) 805 rs_read_strings(FILE *inf, char **s, int count, int max)
806 { 806 {
807 int n = 0; 807 int n = 0;
808 int value = 0; 808 int value = 0;
809 809
810 if (read_error || format_error) 810 if (read_error || format_error)
821 821
822 return(READSTAT); 822 return(READSTAT);
823 } 823 }
824 824
825 int 825 int
826 rs_read_new_strings(int inf, char **s, int count) 826 rs_read_new_strings(FILE *inf, char **s, int count)
827 { 827 {
828 int n = 0; 828 int n = 0;
829 int value = 0; 829 int value = 0;
830 830
831 if (read_error || format_error) 831 if (read_error || format_error)
857 857
858 return( rs_write_int(savef,-1) ); 858 return( rs_write_int(savef,-1) );
859 } 859 }
860 860
861 int 861 int
862 rs_read_string_index(int inf, char *master[], int maxindex, char **str) 862 rs_read_string_index(FILE *inf, char *master[], int maxindex, char **str)
863 { 863 {
864 int i; 864 int i;
865 865
866 if (read_error || format_error) 866 if (read_error || format_error)
867 return(READSTAT); 867 return(READSTAT);
889 889
890 return(WRITESTAT); 890 return(WRITESTAT);
891 } 891 }
892 892
893 int 893 int
894 rs_read_coord(int inf, coord *c) 894 rs_read_coord(FILE *inf, coord *c)
895 { 895 {
896 coord in; 896 coord in;
897 897
898 if (read_error || format_error) 898 if (read_error || format_error)
899 return(READSTAT); 899 return(READSTAT);
924 924
925 return(WRITESTAT); 925 return(WRITESTAT);
926 } 926 }
927 927
928 int 928 int
929 rs_read_coord_list(int inf, struct linked_list **list) 929 rs_read_coord_list(FILE *inf, struct linked_list **list)
930 { 930 {
931 int i, cnt; 931 int i, cnt;
932 struct linked_list *l = NULL, *previous = NULL, *head = NULL; 932 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
933 933
934 rs_read_marker(inf, RSID_COORDLIST); 934 rs_read_marker(inf, RSID_COORDLIST);
982 982
983 return(WRITESTAT); 983 return(WRITESTAT);
984 } 984 }
985 985
986 int 986 int
987 rs_read_window(int inf, WINDOW *win) 987 rs_read_window(FILE *inf, WINDOW *win)
988 { 988 {
989 int row,col,maxlines,maxcols,value,width,height; 989 int row,col,maxlines,maxcols,value,width,height;
990 990
991 if (read_error || format_error) 991 if (read_error || format_error)
992 return(READSTAT); 992 return(READSTAT);
1069 1069
1070 return(WRITESTAT); 1070 return(WRITESTAT);
1071 } 1071 }
1072 1072
1073 int 1073 int
1074 rs_read_levtype(int inf, LEVTYPE *l) 1074 rs_read_levtype(FILE *inf, LEVTYPE *l)
1075 { 1075 {
1076 int lt; 1076 int lt;
1077 1077
1078 rs_read_int(inf, &lt); 1078 rs_read_int(inf, &lt);
1079 1079
1112 1112
1113 return(WRITESTAT); 1113 return(WRITESTAT);
1114 } 1114 }
1115 1115
1116 int 1116 int
1117 rs_read_stats(int inf, struct stats *s) 1117 rs_read_stats(FILE *inf, struct stats *s)
1118 { 1118 {
1119 if (read_error || format_error) 1119 if (read_error || format_error)
1120 return(READSTAT); 1120 return(READSTAT);
1121 1121
1122 rs_read_marker(inf, RSID_STATS); 1122 rs_read_marker(inf, RSID_STATS);
1153 1153
1154 return(WRITESTAT); 1154 return(WRITESTAT);
1155 } 1155 }
1156 1156
1157 int 1157 int
1158 rs_read_magic_items(int inf, struct magic_item *mi, int count) 1158 rs_read_magic_items(FILE *inf, struct magic_item *mi, int count)
1159 { 1159 {
1160 int n; 1160 int n;
1161 int value; 1161 int value;
1162 1162
1163 rs_read_marker(inf, RSID_MAGICITEMS); 1163 rs_read_marker(inf, RSID_MAGICITEMS);
1194 1194
1195 return(WRITESTAT); 1195 return(WRITESTAT);
1196 } 1196 }
1197 1197
1198 int 1198 int
1199 rs_read_scrolls(int inf) 1199 rs_read_scrolls(FILE *inf)
1200 { 1200 {
1201 int i; 1201 int i;
1202 1202
1203 if (read_error || format_error) 1203 if (read_error || format_error)
1204 return(READSTAT); 1204 return(READSTAT);
1230 1230
1231 return(WRITESTAT); 1231 return(WRITESTAT);
1232 } 1232 }
1233 1233
1234 int 1234 int
1235 rs_read_potions(int inf) 1235 rs_read_potions(FILE *inf)
1236 { 1236 {
1237 int i; 1237 int i;
1238 1238
1239 if (read_error || format_error) 1239 if (read_error || format_error)
1240 return(READSTAT); 1240 return(READSTAT);
1266 1266
1267 return(WRITESTAT); 1267 return(WRITESTAT);
1268 } 1268 }
1269 1269
1270 int 1270 int
1271 rs_read_rings(int inf) 1271 rs_read_rings(FILE *inf)
1272 { 1272 {
1273 int i; 1273 int i;
1274 1274
1275 if (read_error || format_error) 1275 if (read_error || format_error)
1276 return(READSTAT); 1276 return(READSTAT);
1314 1314
1315 return(WRITESTAT); 1315 return(WRITESTAT);
1316 } 1316 }
1317 1317
1318 int 1318 int
1319 rs_read_sticks(int inf) 1319 rs_read_sticks(FILE *inf)
1320 { 1320 {
1321 int i = 0, j = 0, list = 0; 1321 int i = 0, j = 0, list = 0;
1322 1322
1323 if (read_error || format_error) 1323 if (read_error || format_error)
1324 return(READSTAT); 1324 return(READSTAT);
1499 1499
1500 return(WRITESTAT); 1500 return(WRITESTAT);
1501 } 1501 }
1502 1502
1503 int 1503 int
1504 rs_read_daemons(int inf, struct delayed_action *d_list, int count) 1504 rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count)
1505 { 1505 {
1506 int i = 0; 1506 int i = 0;
1507 int func = 0; 1507 int func = 0;
1508 int value = 0; 1508 int value = 0;
1509 int dummy = 0; 1509 int dummy = 0;
1641 1641
1642 return(WRITESTAT); 1642 return(WRITESTAT);
1643 } 1643 }
1644 1644
1645 int 1645 int
1646 rs_read_room(int inf, struct room *r) 1646 rs_read_room(FILE *inf, struct room *r)
1647 { 1647 {
1648 int value = 0, n = 0, i = 0, index = 0, id = 0; 1648 int value = 0, n = 0, i = 0, index = 0, id = 0;
1649 struct linked_list *fires=NULL, *item = NULL; 1649 struct linked_list *fires=NULL, *item = NULL;
1650 1650
1651 if (read_error || format_error) 1651 if (read_error || format_error)
1698 1698
1699 return(WRITESTAT); 1699 return(WRITESTAT);
1700 } 1700 }
1701 1701
1702 int 1702 int
1703 rs_read_rooms(int inf, struct room *r, int count) 1703 rs_read_rooms(FILE *inf, struct room *r, int count)
1704 { 1704 {
1705 int value = 0, n = 0; 1705 int value = 0, n = 0;
1706 1706
1707 if (read_error || format_error) 1707 if (read_error || format_error)
1708 return(READSTAT); 1708 return(READSTAT);
1734 1734
1735 return(WRITESTAT); 1735 return(WRITESTAT);
1736 } 1736 }
1737 1737
1738 int 1738 int
1739 rs_read_room_reference(int inf, struct room **rp) 1739 rs_read_room_reference(FILE *inf, struct room **rp)
1740 { 1740 {
1741 int i; 1741 int i;
1742 1742
1743 if (read_error || format_error) 1743 if (read_error || format_error)
1744 return(READSTAT); 1744 return(READSTAT);
1781 1781
1782 return(WRITESTAT); 1782 return(WRITESTAT);
1783 } 1783 }
1784 1784
1785 int 1785 int
1786 rs_read_door_reference(int inf, coord **exit) 1786 rs_read_door_reference(FILE *inf, coord **exit)
1787 { 1787 {
1788 int i, idx; 1788 int i, idx;
1789 1789
1790 rs_read_int(inf, &i); 1790 rs_read_int(inf, &i);
1791 rs_read_int(inf, &idx); 1791 rs_read_int(inf, &idx);
1816 1816
1817 return (WRITESTAT); 1817 return (WRITESTAT);
1818 } 1818 }
1819 1819
1820 int 1820 int
1821 rs_read_traps(int inf, struct trap *trap, int count) 1821 rs_read_traps(FILE *inf, struct trap *trap, int count)
1822 { 1822 {
1823 int id = 0, value = 0, n = 0; 1823 int id = 0, value = 0, n = 0;
1824 1824
1825 if (rs_read_int(inf,&id) != 0) 1825 if (rs_read_int(inf,&id) != 0)
1826 format_error = TRUE; 1826 format_error = TRUE;
1863 1863
1864 return(WRITESTAT); 1864 return(WRITESTAT);
1865 } 1865 }
1866 1866
1867 int 1867 int
1868 rs_read_monsters(int inf, struct monster *m, int count) 1868 rs_read_monsters(FILE *inf, struct monster *m, int count)
1869 { 1869 {
1870 int value = 0, n = 0; 1870 int value = 0, n = 0;
1871 1871
1872 if (read_error || format_error) 1872 if (read_error || format_error)
1873 return(READSTAT); 1873 return(READSTAT);
1915 1915
1916 return(WRITESTAT); 1916 return(WRITESTAT);
1917 } 1917 }
1918 1918
1919 int 1919 int
1920 rs_read_object(int inf, struct object *o) 1920 rs_read_object(FILE *inf, struct object *o)
1921 { 1921 {
1922 if (read_error || format_error) 1922 if (read_error || format_error)
1923 return(READSTAT); 1923 return(READSTAT);
1924 1924
1925 rs_read_marker(inf, RSID_OBJECT); 1925 rs_read_marker(inf, RSID_OBJECT);
1956 1956
1957 return(WRITESTAT); 1957 return(WRITESTAT);
1958 } 1958 }
1959 1959
1960 int 1960 int
1961 rs_read_object_list(int inf, struct linked_list **list) 1961 rs_read_object_list(FILE *inf, struct linked_list **list)
1962 { 1962 {
1963 int i, cnt; 1963 int i, cnt;
1964 struct linked_list *l = NULL, *previous = NULL, *head = NULL; 1964 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
1965 1965
1966 if (read_error || format_error) 1966 if (read_error || format_error)
2008 2008
2009 return(WRITESTAT); 2009 return(WRITESTAT);
2010 } 2010 }
2011 2011
2012 int 2012 int
2013 rs_read_object_reference(int inf, struct linked_list *list, struct object **item) 2013 rs_read_object_reference(FILE *inf, struct linked_list *list, struct object **item)
2014 { 2014 {
2015 int i; 2015 int i;
2016 2016
2017 if (read_error || format_error) 2017 if (read_error || format_error)
2018 return(READSTAT); 2018 return(READSTAT);
2154 2154
2155 return(WRITESTAT); 2155 return(WRITESTAT);
2156 } 2156 }
2157 2157
2158 int 2158 int
2159 rs_read_thing(int inf, struct thing *t) 2159 rs_read_thing(FILE *inf, struct thing *t)
2160 { 2160 {
2161 int listid = 0, index = -1; 2161 int listid = 0, index = -1;
2162 2162
2163 if (read_error || format_error) 2163 if (read_error || format_error)
2164 return(READSTAT); 2164 return(READSTAT);
2273 2273
2274 return(WRITESTAT); 2274 return(WRITESTAT);
2275 } 2275 }
2276 2276
2277 int 2277 int
2278 rs_read_thing_list(int inf, struct linked_list **list) 2278 rs_read_thing_list(FILE *inf, struct linked_list **list)
2279 { 2279 {
2280 int i, cnt; 2280 int i, cnt;
2281 struct linked_list *l = NULL, *previous = NULL, *head = NULL; 2281 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
2282 2282
2283 if (read_error || format_error) 2283 if (read_error || format_error)
2442 2442
2443 return(WRITESTAT); 2443 return(WRITESTAT);
2444 } 2444 }
2445 2445
2446 int 2446 int
2447 rs_restore_file(int inf) 2447 rs_restore_file(FILE *inf)
2448 { 2448 {
2449 int i; 2449 int i;
2450 2450
2451 if (read_error || format_error) 2451 if (read_error || format_error)
2452 return(READSTAT); 2452 return(READSTAT);