Mercurial > hg > early-roguelike
comparison rogue4/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 |
---|---|
69 | 69 |
70 #define READSTAT ((format_error == 0) && (read_error == 0)) | 70 #define READSTAT ((format_error == 0) && (read_error == 0)) |
71 #define WRITESTAT (write_error == 0) | 71 #define WRITESTAT (write_error == 0) |
72 | 72 |
73 int rs_write_int(FILE *savef, int c); | 73 int rs_write_int(FILE *savef, int c); |
74 int rs_read_int(int inf, int *i); | 74 int rs_read_int(FILE *inf, int *i); |
75 | 75 |
76 int read_error = FALSE; | 76 int read_error = FALSE; |
77 int write_error = FALSE; | 77 int write_error = FALSE; |
78 int format_error = FALSE; | 78 int format_error = FALSE; |
79 int end_of_file = FALSE; | 79 int end_of_file = FALSE; |
419 | 419 |
420 return(WRITESTAT); | 420 return(WRITESTAT); |
421 } | 421 } |
422 | 422 |
423 int | 423 int |
424 rs_read(int inf, void *ptr, int size) | 424 rs_read(FILE *inf, void *ptr, int size) |
425 { | 425 { |
426 int actual; | 426 int actual; |
427 | 427 |
428 end_of_file = FALSE; | 428 end_of_file = FALSE; |
429 | 429 |
449 | 449 |
450 return(READSTAT); | 450 return(READSTAT); |
451 } | 451 } |
452 | 452 |
453 int | 453 int |
454 rs_read_char(int inf, char *c) | 454 rs_read_char(FILE *inf, char *c) |
455 { | 455 { |
456 rs_read(inf, c, 1); | 456 rs_read(inf, c, 1); |
457 | 457 |
458 return(READSTAT); | 458 return(READSTAT); |
459 } | 459 } |
460 int | 460 int |
461 rs_read_uchar(int inf, unsigned char *c) | 461 rs_read_uchar(FILE *inf, unsigned char *c) |
462 { | 462 { |
463 rs_read(inf, c, 1); | 463 rs_read(inf, c, 1); |
464 | 464 |
465 return(READSTAT); | 465 return(READSTAT); |
466 } | 466 } |
467 | 467 |
468 int | 468 int |
469 rs_read_boolean(int inf, bool *i) | 469 rs_read_boolean(FILE *inf, bool *i) |
470 { | 470 { |
471 unsigned char buf; | 471 unsigned char buf; |
472 | 472 |
473 rs_read(inf, &buf, 1); | 473 rs_read(inf, &buf, 1); |
474 | 474 |
476 | 476 |
477 return(READSTAT); | 477 return(READSTAT); |
478 } | 478 } |
479 | 479 |
480 int | 480 int |
481 rs_read_booleans(int inf, bool *i, int count) | 481 rs_read_booleans(FILE *inf, bool *i, int count) |
482 { | 482 { |
483 int n = 0, value = 0; | 483 int n = 0, value = 0; |
484 | 484 |
485 if (rs_read_int(inf,&value) != 0) | 485 if (rs_read_int(inf,&value) != 0) |
486 { | 486 { |
498 | 498 |
499 return(READSTAT); | 499 return(READSTAT); |
500 } | 500 } |
501 | 501 |
502 int | 502 int |
503 rs_read_shint(int inf, shint *i) | 503 rs_read_shint(FILE *inf, shint *i) |
504 { | 504 { |
505 unsigned char buf; | 505 unsigned char buf; |
506 | 506 |
507 rs_read(inf, &buf, 1); | 507 rs_read(inf, &buf, 1); |
508 | 508 |
510 | 510 |
511 return(READSTAT); | 511 return(READSTAT); |
512 } | 512 } |
513 | 513 |
514 int | 514 int |
515 rs_read_short(int inf, short *i) | 515 rs_read_short(FILE *inf, short *i) |
516 { | 516 { |
517 unsigned char bytes[2]; | 517 unsigned char bytes[2]; |
518 short input; | 518 short input; |
519 unsigned char *buf = (unsigned char *)&input; | 519 unsigned char *buf = (unsigned char *)&input; |
520 | 520 |
531 | 531 |
532 return(READSTAT); | 532 return(READSTAT); |
533 } | 533 } |
534 | 534 |
535 int | 535 int |
536 rs_read_shorts(int inf, short *i, int count) | 536 rs_read_shorts(FILE *inf, short *i, int count) |
537 { | 537 { |
538 int n = 0, value = 0; | 538 int n = 0, value = 0; |
539 | 539 |
540 if (rs_read_int(inf,&value) != 0) | 540 if (rs_read_int(inf,&value) != 0) |
541 { | 541 { |
550 | 550 |
551 return(READSTAT); | 551 return(READSTAT); |
552 } | 552 } |
553 | 553 |
554 int | 554 int |
555 rs_read_ushort(int inf, unsigned short *i) | 555 rs_read_ushort(FILE *inf, unsigned short *i) |
556 { | 556 { |
557 unsigned char bytes[2]; | 557 unsigned char bytes[2]; |
558 unsigned short input; | 558 unsigned short input; |
559 unsigned char *buf = (unsigned char *)&input; | 559 unsigned char *buf = (unsigned char *)&input; |
560 | 560 |
571 | 571 |
572 return(READSTAT); | 572 return(READSTAT); |
573 } | 573 } |
574 | 574 |
575 int | 575 int |
576 rs_read_int(int inf, int *i) | 576 rs_read_int(FILE *inf, int *i) |
577 { | 577 { |
578 unsigned char bytes[4]; | 578 unsigned char bytes[4]; |
579 int input; | 579 int input; |
580 unsigned char *buf = (unsigned char *)&input; | 580 unsigned char *buf = (unsigned char *)&input; |
581 | 581 |
594 | 594 |
595 return(READSTAT); | 595 return(READSTAT); |
596 } | 596 } |
597 | 597 |
598 int | 598 int |
599 rs_read_ints(int inf, int *i, int count) | 599 rs_read_ints(FILE *inf, int *i, int count) |
600 { | 600 { |
601 int n = 0, value = 0; | 601 int n = 0, value = 0; |
602 | 602 |
603 if (rs_read_int(inf,&value) != 0) | 603 if (rs_read_int(inf,&value) != 0) |
604 { | 604 { |
613 | 613 |
614 return(READSTAT); | 614 return(READSTAT); |
615 } | 615 } |
616 | 616 |
617 int | 617 int |
618 rs_read_uint(int inf, unsigned int *i) | 618 rs_read_uint(FILE *inf, unsigned int *i) |
619 { | 619 { |
620 unsigned char bytes[4]; | 620 unsigned char bytes[4]; |
621 int input; | 621 int input; |
622 unsigned char *buf = (unsigned char *)&input; | 622 unsigned char *buf = (unsigned char *)&input; |
623 | 623 |
636 | 636 |
637 return(READSTAT); | 637 return(READSTAT); |
638 } | 638 } |
639 | 639 |
640 int | 640 int |
641 rs_read_long(int inf, long *i) | 641 rs_read_long(FILE *inf, long *i) |
642 { | 642 { |
643 unsigned char bytes[4]; | 643 unsigned char bytes[4]; |
644 long input; | 644 long input; |
645 unsigned char *buf = (unsigned char *) &input; | 645 unsigned char *buf = (unsigned char *) &input; |
646 | 646 |
659 | 659 |
660 return(READSTAT); | 660 return(READSTAT); |
661 } | 661 } |
662 | 662 |
663 int | 663 int |
664 rs_read_longs(int inf, long *i, int count) | 664 rs_read_longs(FILE *inf, long *i, int count) |
665 { | 665 { |
666 int n = 0, value = 0; | 666 int n = 0, value = 0; |
667 | 667 |
668 if (rs_read_int(inf,&value) != 0) | 668 if (rs_read_int(inf,&value) != 0) |
669 { | 669 { |
678 | 678 |
679 return(READSTAT); | 679 return(READSTAT); |
680 } | 680 } |
681 | 681 |
682 int | 682 int |
683 rs_read_ulong(int inf, unsigned long *i) | 683 rs_read_ulong(FILE *inf, unsigned long *i) |
684 { | 684 { |
685 unsigned char bytes[4]; | 685 unsigned char bytes[4]; |
686 unsigned long input; | 686 unsigned long input; |
687 unsigned char *buf = (unsigned char *) &input; | 687 unsigned char *buf = (unsigned char *) &input; |
688 | 688 |
701 | 701 |
702 return(READSTAT); | 702 return(READSTAT); |
703 } | 703 } |
704 | 704 |
705 int | 705 int |
706 rs_read_ulongs(int inf, unsigned long *i, int count) | 706 rs_read_ulongs(FILE *inf, unsigned long *i, int count) |
707 { | 707 { |
708 int n = 0, value = 0; | 708 int n = 0, value = 0; |
709 | 709 |
710 if (rs_read_int(inf,&value) != 0) | 710 if (rs_read_int(inf,&value) != 0) |
711 { | 711 { |
720 | 720 |
721 return(READSTAT); | 721 return(READSTAT); |
722 } | 722 } |
723 | 723 |
724 int | 724 int |
725 rs_read_string(int inf, char *s, int max) | 725 rs_read_string(FILE *inf, char *s, int max) |
726 { | 726 { |
727 int len = 0; | 727 int len = 0; |
728 | 728 |
729 if (rs_read_int(inf, &len) != FALSE) | 729 if (rs_read_int(inf, &len) != FALSE) |
730 { | 730 { |
740 | 740 |
741 return(READSTAT); | 741 return(READSTAT); |
742 } | 742 } |
743 | 743 |
744 int | 744 int |
745 rs_read_new_string(int inf, char **s) | 745 rs_read_new_string(FILE *inf, char **s) |
746 { | 746 { |
747 int len=0; | 747 int len=0; |
748 char *buf=0; | 748 char *buf=0; |
749 | 749 |
750 if (rs_read_int(inf, &len) != 0) | 750 if (rs_read_int(inf, &len) != 0) |
767 | 767 |
768 return(READSTAT); | 768 return(READSTAT); |
769 } | 769 } |
770 | 770 |
771 int | 771 int |
772 rs_read_string_index(int inf, const char *master[], int maxindex, | 772 rs_read_string_index(FILE *inf, const char *master[], int maxindex, |
773 const char **str) | 773 const char **str) |
774 { | 774 { |
775 int i; | 775 int i; |
776 | 776 |
777 if (rs_read_int(inf,&i) != 0) | 777 if (rs_read_int(inf,&i) != 0) |
790 | 790 |
791 return(READSTAT); | 791 return(READSTAT); |
792 } | 792 } |
793 | 793 |
794 int | 794 int |
795 rs_read_strings(int inf, char **s, int count, int max) | 795 rs_read_strings(FILE *inf, char **s, int count, int max) |
796 { | 796 { |
797 int n = 0; | 797 int n = 0; |
798 int value = 0; | 798 int value = 0; |
799 | 799 |
800 if (rs_read_int(inf,&value) != 0) | 800 if (rs_read_int(inf,&value) != 0) |
817 | 817 |
818 return(READSTAT); | 818 return(READSTAT); |
819 } | 819 } |
820 | 820 |
821 int | 821 int |
822 rs_read_new_strings(int inf, char **s, int count) | 822 rs_read_new_strings(FILE *inf, char **s, int count) |
823 { | 823 { |
824 int len = 0; | 824 int len = 0; |
825 int n = 0; | 825 int n = 0; |
826 int value = 0; | 826 int value = 0; |
827 | 827 |
861 | 861 |
862 return(WRITESTAT); | 862 return(WRITESTAT); |
863 } | 863 } |
864 | 864 |
865 int | 865 int |
866 rs_read_str_t(int inf, str_t *st) | 866 rs_read_str_t(FILE *inf, str_t *st) |
867 { | 867 { |
868 rs_read_uint(inf,st); | 868 rs_read_uint(inf,st); |
869 | 869 |
870 return(READSTAT); | 870 return(READSTAT); |
871 } | 871 } |
878 | 878 |
879 return(WRITESTAT); | 879 return(WRITESTAT); |
880 } | 880 } |
881 | 881 |
882 int | 882 int |
883 rs_read_coord(int inf, coord *c) | 883 rs_read_coord(FILE *inf, coord *c) |
884 { | 884 { |
885 rs_read_shint(inf,&c->x); | 885 rs_read_shint(inf,&c->x); |
886 rs_read_shint(inf,&c->y); | 886 rs_read_shint(inf,&c->y); |
887 | 887 |
888 return(READSTAT); | 888 return(READSTAT); |
905 | 905 |
906 return(WRITESTAT); | 906 return(WRITESTAT); |
907 } | 907 } |
908 | 908 |
909 int | 909 int |
910 rs_read_window(int inf, WINDOW *win) | 910 rs_read_window(FILE *inf, WINDOW *win) |
911 { | 911 { |
912 int id,row,col,maxlines,maxcols,value,width,height; | 912 int id,row,col,maxlines,maxcols,value,width,height; |
913 | 913 |
914 width = getmaxx(win); | 914 width = getmaxx(win); |
915 height = getmaxy(win); | 915 height = getmaxy(win); |
985 | 985 |
986 return(WRITESTAT); | 986 return(WRITESTAT); |
987 } | 987 } |
988 | 988 |
989 int | 989 int |
990 rs_read_daemons(int inf, struct delayed_action *d_list, int count) | 990 rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count) |
991 { | 991 { |
992 int i = 0; | 992 int i = 0; |
993 int func = 0; | 993 int func = 0; |
994 int value = 0; | 994 int value = 0; |
995 int id = 0; | 995 int id = 0; |
1081 | 1081 |
1082 return(WRITESTAT); | 1082 return(WRITESTAT); |
1083 } | 1083 } |
1084 | 1084 |
1085 int | 1085 int |
1086 rs_read_magic_items(int inf, struct magic_item *mi, int count) | 1086 rs_read_magic_items(FILE *inf, struct magic_item *mi, int count) |
1087 { | 1087 { |
1088 int id; | 1088 int id; |
1089 int n; | 1089 int n; |
1090 int value; | 1090 int value; |
1091 | 1091 |
1159 | 1159 |
1160 return(WRITESTAT); | 1160 return(WRITESTAT); |
1161 } | 1161 } |
1162 | 1162 |
1163 int | 1163 int |
1164 rs_read_room(int inf, struct room *r) | 1164 rs_read_room(FILE *inf, struct room *r) |
1165 { | 1165 { |
1166 rs_read_coord(inf,&r->r_pos); | 1166 rs_read_coord(inf,&r->r_pos); |
1167 rs_read_coord(inf,&r->r_max); | 1167 rs_read_coord(inf,&r->r_max); |
1168 rs_read_coord(inf,&r->r_gold); | 1168 rs_read_coord(inf,&r->r_gold); |
1169 rs_read_int(inf,&r->r_goldval); | 1169 rs_read_int(inf,&r->r_goldval); |
1184 | 1184 |
1185 return(READSTAT); | 1185 return(READSTAT); |
1186 } | 1186 } |
1187 | 1187 |
1188 int | 1188 int |
1189 rs_read_rooms(int inf, struct room *r, int count) | 1189 rs_read_rooms(FILE *inf, struct room *r, int count) |
1190 { | 1190 { |
1191 int value = 0, n = 0; | 1191 int value = 0, n = 0; |
1192 | 1192 |
1193 if (rs_read_int(inf,&value) != 0) | 1193 if (rs_read_int(inf,&value) != 0) |
1194 { | 1194 { |
1220 | 1220 |
1221 return(WRITESTAT); | 1221 return(WRITESTAT); |
1222 } | 1222 } |
1223 | 1223 |
1224 int | 1224 int |
1225 rs_read_room_reference(int inf, struct room **rp) | 1225 rs_read_room_reference(FILE *inf, struct room **rp) |
1226 { | 1226 { |
1227 int i; | 1227 int i; |
1228 | 1228 |
1229 rs_read_int(inf, &i); | 1229 rs_read_int(inf, &i); |
1230 | 1230 |
1250 | 1250 |
1251 return(WRITESTAT); | 1251 return(WRITESTAT); |
1252 } | 1252 } |
1253 | 1253 |
1254 int | 1254 int |
1255 rs_read_stats(int inf, struct stats *s) | 1255 rs_read_stats(FILE *inf, struct stats *s) |
1256 { | 1256 { |
1257 int id; | 1257 int id; |
1258 | 1258 |
1259 rs_read_int(inf, &id); | 1259 rs_read_int(inf, &id); |
1260 | 1260 |
1288 | 1288 |
1289 return(WRITESTAT); | 1289 return(WRITESTAT); |
1290 } | 1290 } |
1291 | 1291 |
1292 int | 1292 int |
1293 rs_read_object(int inf, THING *o) | 1293 rs_read_object(FILE *inf, THING *o) |
1294 { | 1294 { |
1295 int id; | 1295 int id; |
1296 | 1296 |
1297 if (rs_read_int(inf, &id) != 0) | 1297 if (rs_read_int(inf, &id) != 0) |
1298 { | 1298 { |
1337 | 1337 |
1338 return(WRITESTAT); | 1338 return(WRITESTAT); |
1339 } | 1339 } |
1340 | 1340 |
1341 int | 1341 int |
1342 rs_read_object_list(int inf, THING **list) | 1342 rs_read_object_list(FILE *inf, THING **list) |
1343 { | 1343 { |
1344 int id; | 1344 int id; |
1345 int i, cnt; | 1345 int i, cnt; |
1346 THING *l = NULL, *previous = NULL, *head = NULL; | 1346 THING *l = NULL, *previous = NULL, *head = NULL; |
1347 | 1347 |
1388 | 1388 |
1389 return(WRITESTAT); | 1389 return(WRITESTAT); |
1390 } | 1390 } |
1391 | 1391 |
1392 int | 1392 int |
1393 rs_read_object_reference(int inf, THING *list, THING **item) | 1393 rs_read_object_reference(FILE *inf, THING *list, THING **item) |
1394 { | 1394 { |
1395 int i; | 1395 int i; |
1396 | 1396 |
1397 rs_read_int(inf, &i); | 1397 rs_read_int(inf, &i); |
1398 | 1398 |
1564 for(item = list; item != NULL; item = item->l_next) | 1564 for(item = list; item != NULL; item = item->l_next) |
1565 rs_fix_thing(item); | 1565 rs_fix_thing(item); |
1566 } | 1566 } |
1567 | 1567 |
1568 int | 1568 int |
1569 rs_read_thing(int inf, THING *t) | 1569 rs_read_thing(FILE *inf, THING *t) |
1570 { | 1570 { |
1571 int id; | 1571 int id; |
1572 int listid = 0, index = -1; | 1572 int listid = 0, index = -1; |
1573 THING *item; | 1573 THING *item; |
1574 | 1574 |
1664 | 1664 |
1665 return(WRITESTAT); | 1665 return(WRITESTAT); |
1666 } | 1666 } |
1667 | 1667 |
1668 int | 1668 int |
1669 rs_read_thing_list(int inf, THING **list) | 1669 rs_read_thing_list(FILE *inf, THING **list) |
1670 { | 1670 { |
1671 int id; | 1671 int id; |
1672 int i, cnt; | 1672 int i, cnt; |
1673 THING *l = NULL, *previous = NULL, *head = NULL; | 1673 THING *l = NULL, *previous = NULL, *head = NULL; |
1674 | 1674 |
1728 | 1728 |
1729 return(WRITESTAT); | 1729 return(WRITESTAT); |
1730 } | 1730 } |
1731 | 1731 |
1732 int | 1732 int |
1733 rs_read_monsters(int inf, struct monster *m, int count) | 1733 rs_read_monsters(FILE *inf, struct monster *m, int count) |
1734 { | 1734 { |
1735 int id = 0, value = 0, n = 0; | 1735 int id = 0, value = 0, n = 0; |
1736 | 1736 |
1737 if (rs_read_int(inf, &id) != 0) | 1737 if (rs_read_int(inf, &id) != 0) |
1738 { | 1738 { |
1772 rs_write_string(savef,s_names[i]); | 1772 rs_write_string(savef,s_names[i]); |
1773 rs_write_boolean(savef,s_know[i]); | 1773 rs_write_boolean(savef,s_know[i]); |
1774 rs_write_string(savef,s_guess[i]); | 1774 rs_write_string(savef,s_guess[i]); |
1775 } | 1775 } |
1776 | 1776 |
1777 return(READSTAT); | 1777 return(WRITESTAT); |
1778 } | 1778 } |
1779 | 1779 |
1780 int | 1780 int |
1781 rs_read_scrolls(int inf) | 1781 rs_read_scrolls(FILE *inf) |
1782 { | 1782 { |
1783 int i; | 1783 int i; |
1784 | 1784 |
1785 for(i = 0; i < MAXSCROLLS; i++) | 1785 for(i = 0; i < MAXSCROLLS; i++) |
1786 { | 1786 { |
1806 | 1806 |
1807 return(WRITESTAT); | 1807 return(WRITESTAT); |
1808 } | 1808 } |
1809 | 1809 |
1810 int | 1810 int |
1811 rs_read_potions(int inf) | 1811 rs_read_potions(FILE *inf) |
1812 { | 1812 { |
1813 int i; | 1813 int i; |
1814 | 1814 |
1815 for(i = 0; i < MAXPOTIONS; i++) | 1815 for(i = 0; i < MAXPOTIONS; i++) |
1816 { | 1816 { |
1841 | 1841 |
1842 return(WRITESTAT); | 1842 return(WRITESTAT); |
1843 } | 1843 } |
1844 | 1844 |
1845 int | 1845 int |
1846 rs_read_rings(int inf) | 1846 rs_read_rings(FILE *inf) |
1847 { | 1847 { |
1848 int i; | 1848 int i; |
1849 const char *stones_list[NSTONES]; | 1849 const char *stones_list[NSTONES]; |
1850 | 1850 |
1851 for(i = 0; i < NSTONES; i++) | 1851 for(i = 0; i < NSTONES; i++) |
1884 | 1884 |
1885 return(WRITESTAT); | 1885 return(WRITESTAT); |
1886 } | 1886 } |
1887 | 1887 |
1888 int | 1888 int |
1889 rs_read_sticks(int inf) | 1889 rs_read_sticks(FILE *inf) |
1890 { | 1890 { |
1891 int i = 0, list = 0; | 1891 int i = 0, list = 0; |
1892 | 1892 |
1893 for(i = 0; i < MAXSTICKS; i++) | 1893 for(i = 0; i < MAXSTICKS; i++) |
1894 { | 1894 { |
1929 | 1929 |
1930 return(WRITESTAT); | 1930 return(WRITESTAT); |
1931 } | 1931 } |
1932 | 1932 |
1933 int | 1933 int |
1934 rs_read_thing_reference(int inf, THING *list, THING **item) | 1934 rs_read_thing_reference(FILE *inf, THING *list, THING **item) |
1935 { | 1935 { |
1936 int i; | 1936 int i; |
1937 | 1937 |
1938 rs_read_int(inf, &i); | 1938 rs_read_int(inf, &i); |
1939 | 1939 |
1959 | 1959 |
1960 return(WRITESTAT); | 1960 return(WRITESTAT); |
1961 } | 1961 } |
1962 | 1962 |
1963 int | 1963 int |
1964 rs_read_thing_references(int inf, THING *list, THING *items[], int count) | 1964 rs_read_thing_references(FILE *inf, THING *list, THING *items[], int count) |
1965 { | 1965 { |
1966 int i; | 1966 int i; |
1967 | 1967 |
1968 for(i = 0; i < count; i++) | 1968 for(i = 0; i < count; i++) |
1969 rs_read_thing_reference(inf,list,&items[i]); | 1969 rs_read_thing_reference(inf,list,&items[i]); |
2074 | 2074 |
2075 return(WRITESTAT); | 2075 return(WRITESTAT); |
2076 } | 2076 } |
2077 | 2077 |
2078 int | 2078 int |
2079 rs_restore_file(int inf) | 2079 rs_restore_file(FILE *inf) |
2080 { | 2080 { |
2081 bool junk; | 2081 bool junk; |
2082 THING *mitem; | 2082 THING *mitem; |
2083 int endian = 0x01020304; | 2083 int endian = 0x01020304; |
2084 big_endian = ( *((char *)&endian) == 0x01 ); | 2084 big_endian = ( *((char *)&endian) == 0x01 ); |