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);