Mercurial > hg > early-roguelike
comparison arogue7/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 | 
|---|---|
| 73 static int endian = 0x01020304; | 73 static int endian = 0x01020304; | 
| 74 #define big_endian ( *((char *)&endian) == 0x01 ) | 74 #define big_endian ( *((char *)&endian) == 0x01 ) | 
| 75 | 75 | 
| 76 int list_size(struct linked_list *l); | 76 int list_size(struct linked_list *l); | 
| 77 int rs_write_int(FILE *savef, int c); | 77 int rs_write_int(FILE *savef, int c); | 
| 78 int rs_read_int(int inf, int *i); | 78 int rs_read_int(FILE *inf, int *i); | 
| 79 int rs_write_object_list(FILE *savef, struct linked_list *l); | 79 int rs_write_object_list(FILE *savef, struct linked_list *l); | 
| 80 int rs_read_object_list(int inf, struct linked_list **list); | 80 int rs_read_object_list(FILE *inf, struct linked_list **list); | 
| 81 | 81 | 
| 82 int | 82 int | 
| 83 rs_write(FILE *savef, void *ptr, size_t size) | 83 rs_write(FILE *savef, void *ptr, size_t size) | 
| 84 { | 84 { | 
| 85 if (write_error) | 85 if (write_error) | 
| 86 return(WRITESTAT); | 86 return(WRITESTAT); | 
| 87 | 87 | 
| 88 if (encwrite(ptr, size, md_fileno(savef)) != size) | 88 if (encwrite(ptr, size, savef) != size) | 
| 89 write_error = 1; | 89 write_error = 1; | 
| 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 | 
| 578 | 578 | 
| 579 return(WRITESTAT); | 579 return(WRITESTAT); | 
| 580 } | 580 } | 
| 581 | 581 | 
| 582 int | 582 int | 
| 583 rs_read_longs(int inf, long *i, int count) | 583 rs_read_longs(FILE *inf, long *i, int count) | 
| 584 { | 584 { | 
| 585 int n = 0, value = 0; | 585 int n = 0, value = 0; | 
| 586 | 586 | 
| 587 if (read_error || format_error) | 587 if (read_error || format_error) | 
| 588 return(READSTAT); | 588 return(READSTAT); | 
| 628 | 628 | 
| 629 return(WRITESTAT); | 629 return(WRITESTAT); | 
| 630 } | 630 } | 
| 631 | 631 | 
| 632 int | 632 int | 
| 633 rs_read_ulong(int inf, unsigned long *i) | 633 rs_read_ulong(FILE *inf, unsigned long *i) | 
| 634 { | 634 { | 
| 635 unsigned char bytes[4]; | 635 unsigned char bytes[4]; | 
| 636 unsigned long input; | 636 unsigned long input; | 
| 637 unsigned char *buf = (unsigned char *) &input; | 637 unsigned char *buf = (unsigned char *) &input; | 
| 638 | 638 | 
| 674 | 674 | 
| 675 return(WRITESTAT); | 675 return(WRITESTAT); | 
| 676 } | 676 } | 
| 677 | 677 | 
| 678 int | 678 int | 
| 679 rs_read_ulongs(int inf, unsigned long *i, int count) | 679 rs_read_ulongs(FILE *inf, unsigned long *i, int count) | 
| 680 { | 680 { | 
| 681 int n = 0, value = 0; | 681 int n = 0, value = 0; | 
| 682 | 682 | 
| 683 if (read_error || format_error) | 683 if (read_error || format_error) | 
| 684 return(READSTAT); | 684 return(READSTAT); | 
| 705 | 705 | 
| 706 return(WRITESTAT); | 706 return(WRITESTAT); | 
| 707 } | 707 } | 
| 708 | 708 | 
| 709 int | 709 int | 
| 710 rs_read_marker(int inf, int id) | 710 rs_read_marker(FILE *inf, int id) | 
| 711 { | 711 { | 
| 712 int nid; | 712 int nid; | 
| 713 | 713 | 
| 714 if (read_error || format_error) | 714 if (read_error || format_error) | 
| 715 return(READSTAT); | 715 return(READSTAT); | 
| 740 | 740 | 
| 741 return(WRITESTAT); | 741 return(WRITESTAT); | 
| 742 } | 742 } | 
| 743 | 743 | 
| 744 int | 744 int | 
| 745 rs_read_string(int inf, char *s, int max) | 745 rs_read_string(FILE *inf, char *s, int max) | 
| 746 { | 746 { | 
| 747 int len = 0; | 747 int len = 0; | 
| 748 | 748 | 
| 749 if (read_error || format_error) | 749 if (read_error || format_error) | 
| 750 return(READSTAT); | 750 return(READSTAT); | 
| 758 | 758 | 
| 759 return(READSTAT); | 759 return(READSTAT); | 
| 760 } | 760 } | 
| 761 | 761 | 
| 762 int | 762 int | 
| 763 rs_read_new_string(int inf, char **s) | 763 rs_read_new_string(FILE *inf, char **s) | 
| 764 { | 764 { | 
| 765 int len=0; | 765 int len=0; | 
| 766 char *buf=0; | 766 char *buf=0; | 
| 767 | 767 | 
| 768 if (read_error || format_error) | 768 if (read_error || format_error) | 
| 803 | 803 | 
| 804 return(WRITESTAT); | 804 return(WRITESTAT); | 
| 805 } | 805 } | 
| 806 | 806 | 
| 807 int | 807 int | 
| 808 rs_read_strings(int inf, char **s, int count, int max) | 808 rs_read_strings(FILE *inf, char **s, int count, int max) | 
| 809 { | 809 { | 
| 810 int n = 0; | 810 int n = 0; | 
| 811 int value = 0; | 811 int value = 0; | 
| 812 | 812 | 
| 813 if (read_error || format_error) | 813 if (read_error || format_error) | 
| 824 | 824 | 
| 825 return(READSTAT); | 825 return(READSTAT); | 
| 826 } | 826 } | 
| 827 | 827 | 
| 828 int | 828 int | 
| 829 rs_read_new_strings(int inf, char **s, int count) | 829 rs_read_new_strings(FILE *inf, char **s, int count) | 
| 830 { | 830 { | 
| 831 int n = 0; | 831 int n = 0; | 
| 832 int value = 0; | 832 int value = 0; | 
| 833 | 833 | 
| 834 if (read_error || format_error) | 834 if (read_error || format_error) | 
| 860 | 860 | 
| 861 return( rs_write_int(savef,-1) ); | 861 return( rs_write_int(savef,-1) ); | 
| 862 } | 862 } | 
| 863 | 863 | 
| 864 int | 864 int | 
| 865 rs_read_string_index(int inf, char *master[], int maxindex, char **str) | 865 rs_read_string_index(FILE *inf, char *master[], int maxindex, char **str) | 
| 866 { | 866 { | 
| 867 int i; | 867 int i; | 
| 868 | 868 | 
| 869 if (read_error || format_error) | 869 if (read_error || format_error) | 
| 870 return(READSTAT); | 870 return(READSTAT); | 
| 892 | 892 | 
| 893 return(WRITESTAT); | 893 return(WRITESTAT); | 
| 894 } | 894 } | 
| 895 | 895 | 
| 896 int | 896 int | 
| 897 rs_read_coord(int inf, coord *c) | 897 rs_read_coord(FILE *inf, coord *c) | 
| 898 { | 898 { | 
| 899 coord in; | 899 coord in; | 
| 900 | 900 | 
| 901 if (read_error || format_error) | 901 if (read_error || format_error) | 
| 902 return(READSTAT); | 902 return(READSTAT); | 
| 927 | 927 | 
| 928 return(WRITESTAT); | 928 return(WRITESTAT); | 
| 929 } | 929 } | 
| 930 | 930 | 
| 931 int | 931 int | 
| 932 rs_read_coord_list(int inf, struct linked_list **list) | 932 rs_read_coord_list(FILE *inf, struct linked_list **list) | 
| 933 { | 933 { | 
| 934 int i, cnt; | 934 int i, cnt; | 
| 935 struct linked_list *l = NULL, *previous = NULL, *head = NULL; | 935 struct linked_list *l = NULL, *previous = NULL, *head = NULL; | 
| 936 | 936 | 
| 937 rs_read_marker(inf, RSID_COORDLIST); | 937 rs_read_marker(inf, RSID_COORDLIST); | 
| 985 | 985 | 
| 986 return(WRITESTAT); | 986 return(WRITESTAT); | 
| 987 } | 987 } | 
| 988 | 988 | 
| 989 int | 989 int | 
| 990 rs_read_window(int inf, WINDOW *win) | 990 rs_read_window(FILE *inf, WINDOW *win) | 
| 991 { | 991 { | 
| 992 int row,col,maxlines,maxcols,value,width,height; | 992 int row,col,maxlines,maxcols,value,width,height; | 
| 993 | 993 | 
| 994 if (read_error || format_error) | 994 if (read_error || format_error) | 
| 995 return(READSTAT); | 995 return(READSTAT); | 
| 1073 | 1073 | 
| 1074 return(WRITESTAT); | 1074 return(WRITESTAT); | 
| 1075 } | 1075 } | 
| 1076 | 1076 | 
| 1077 int | 1077 int | 
| 1078 rs_read_levtype(int inf, LEVTYPE *l) | 1078 rs_read_levtype(FILE *inf, LEVTYPE *l) | 
| 1079 { | 1079 { | 
| 1080 int lt; | 1080 int lt; | 
| 1081 | 1081 | 
| 1082 rs_read_int(inf, <); | 1082 rs_read_int(inf, <); | 
| 1083 | 1083 | 
| 1118 | 1118 | 
| 1119 return(WRITESTAT); | 1119 return(WRITESTAT); | 
| 1120 } | 1120 } | 
| 1121 | 1121 | 
| 1122 int | 1122 int | 
| 1123 rs_read_stats(int inf, struct stats *s) | 1123 rs_read_stats(FILE *inf, struct stats *s) | 
| 1124 { | 1124 { | 
| 1125 if (read_error || format_error) | 1125 if (read_error || format_error) | 
| 1126 return(READSTAT); | 1126 return(READSTAT); | 
| 1127 | 1127 | 
| 1128 rs_read_marker(inf, RSID_STATS); | 1128 rs_read_marker(inf, RSID_STATS); | 
| 1163 | 1163 | 
| 1164 return(WRITESTAT); | 1164 return(WRITESTAT); | 
| 1165 } | 1165 } | 
| 1166 | 1166 | 
| 1167 int | 1167 int | 
| 1168 rs_read_magic_items(int inf, struct magic_item *mi, int count) | 1168 rs_read_magic_items(FILE *inf, struct magic_item *mi, int count) | 
| 1169 { | 1169 { | 
| 1170 int n; | 1170 int n; | 
| 1171 int value; | 1171 int value; | 
| 1172 | 1172 | 
| 1173 rs_read_marker(inf, RSID_MAGICITEMS); | 1173 rs_read_marker(inf, RSID_MAGICITEMS); | 
| 1208 | 1208 | 
| 1209 return(WRITESTAT); | 1209 return(WRITESTAT); | 
| 1210 } | 1210 } | 
| 1211 | 1211 | 
| 1212 int | 1212 int | 
| 1213 rs_read_scrolls(int inf) | 1213 rs_read_scrolls(FILE *inf) | 
| 1214 { | 1214 { | 
| 1215 int i; | 1215 int i; | 
| 1216 | 1216 | 
| 1217 if (read_error || format_error) | 
