comparison srogue/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
68 #include "rogue.ext" 68 #include "rogue.ext"
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_read_int(int inf, int *i); 73 int rs_read_int(FILE *inf, int *i);
74 int rs_write_int(FILE *savef, int c); 74 int rs_write_int(FILE *savef, int c);
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;
103 103
104 /* 104 /*
105 * perform an encrypted read 105 * perform an encrypted read
106 */ 106 */
107 int 107 int
108 encread(void *starta, unsigned int size, int inf) 108 encread(void *starta, unsigned int size, FILE *inf)
109 { 109 {
110 register char *ep; 110 register char *ep;
111 register int read_size; 111 register int read_size;
112 register char *start = starta; 112 register char *start = starta;
113 113
114 if ((read_size = read(inf, start, size)) == -1 || read_size == 0) 114 if ((read_size = fread(start, 1, size, inf)) == 0)
115 return read_size; 115 return read_size;
116 116
117 ep = encstr; 117 ep = encstr;
118 118
119 while (size--) 119 while (size--)
464 464
465 return(WRITESTAT); 465 return(WRITESTAT);
466 } 466 }
467 467
468 int 468 int
469 rs_read(int inf, void *ptr, int size) 469 rs_read(FILE *inf, void *ptr, int size)
470 { 470 {
471 int actual; 471 int actual;
472 472
473 end_of_file = FALSE; 473 end_of_file = FALSE;
474 474
495 495
496 return(READSTAT); 496 return(READSTAT);
497 } 497 }
498 498
499 int 499 int
500 rs_read_char(int inf, char *c) 500 rs_read_char(FILE *inf, char *c)
501 { 501 {
502 rs_read(inf, c, 1); 502 rs_read(inf, c, 1);
503 503
504 return(READSTAT); 504 return(READSTAT);
505 } 505 }
506 506
507 int 507 int
508 rs_read_boolean(int inf, bool *i) 508 rs_read_boolean(FILE *inf, bool *i)
509 { 509 {
510 unsigned char buf; 510 unsigned char buf;
511 511
512 rs_read(inf, &buf, 1); 512 rs_read(inf, &buf, 1);
513 513
515 515
516 return(READSTAT); 516 return(READSTAT);
517 } 517 }
518 518
519 int 519 int
520 rs_read_booleans(int inf, bool *i, int count) 520 rs_read_booleans(FILE *inf, bool *i, int count)
521 { 521 {
522 int n = 0, value = 0; 522 int n = 0, value = 0;
523 523
524 if (rs_read_int(inf,&value) != 0) 524 if (rs_read_int(inf,&value) != 0)
525 { 525 {
537 537
538 return(READSTAT); 538 return(READSTAT);
539 } 539 }
540 540
541 int 541 int
542 rs_read_shint(int inf, unsigned char *i) 542 rs_read_shint(FILE *inf, unsigned char *i)
543 { 543 {
544 unsigned char buf; 544 unsigned char buf;
545 545
546 rs_read(inf, &buf, 1); 546 rs_read(inf, &buf, 1);
547 547
549 549
550 return(READSTAT); 550 return(READSTAT);
551 } 551 }
552 552
553 int 553 int
554 rs_read_short(int inf, short *i) 554 rs_read_short(FILE *inf, short *i)
555 { 555 {
556 unsigned char bytes[2]; 556 unsigned char bytes[2];
557 short input; 557 short input;
558 unsigned char *buf = (unsigned char *)&input; 558 unsigned char *buf = (unsigned char *)&input;
559 559
570 570
571 return(READSTAT); 571 return(READSTAT);
572 } 572 }
573 573
574 int 574 int
575 rs_read_shorts(int inf, short *i, int count) 575 rs_read_shorts(FILE *inf, short *i, int count)
576 { 576 {
577 int n = 0, value = 0; 577 int n = 0, value = 0;
578 578
579 if (rs_read_int(inf,&value) != 0) 579 if (rs_read_int(inf,&value) != 0)
580 { 580 {
589 589
590 return(READSTAT); 590 return(READSTAT);
591 } 591 }
592 592
593 int 593 int
594 rs_read_ushort(int inf, unsigned short *i) 594 rs_read_ushort(FILE *inf, unsigned short *i)
595 { 595 {
596 unsigned char bytes[2]; 596 unsigned char bytes[2];
597 unsigned short input; 597 unsigned short input;
598 unsigned char *buf = (unsigned char *)&input; 598 unsigned char *buf = (unsigned char *)&input;
599 599
610 610
611 return(READSTAT); 611 return(READSTAT);
612 } 612 }
613 613
614 int 614 int
615 rs_read_int(int inf, int *i) 615 rs_read_int(FILE *inf, int *i)
616 { 616 {
617 unsigned char bytes[4]; 617 unsigned char bytes[4];
618 int input; 618 int input;
619 unsigned char *buf = (unsigned char *)&input; 619 unsigned char *buf = (unsigned char *)&input;
620 620
633 633
634 return(READSTAT); 634 return(READSTAT);
635 } 635 }
636 636
637 int 637 int
638 rs_read_ints(int inf, int *i, int count) 638 rs_read_ints(FILE *inf, int *i, int count)
639 { 639 {
640 int n = 0, value = 0; 640 int n = 0, value = 0;
641 641
642 if (rs_read_int(inf,&value) != 0) 642 if (rs_read_int(inf,&value) != 0)
643 { 643 {
652 652
653 return(READSTAT); 653 return(READSTAT);
654 } 654 }
655 655
656 int 656 int
657 rs_read_uint(int inf, unsigned int *i) 657 rs_read_uint(FILE *inf, unsigned int *i)
658 { 658 {
659 unsigned char bytes[4]; 659 unsigned char bytes[4];
660 int input; 660 int input;
661 unsigned char *buf = (unsigned char *)&input; 661 unsigned char *buf = (unsigned char *)&input;
662 662
675 675
676 return(READSTAT); 676 return(READSTAT);
677 } 677 }
678 678
679 int 679 int
680 rs_read_long(int inf, long *i) 680 rs_read_long(FILE *inf, long *i)
681 { 681 {
682 unsigned char bytes[4]; 682 unsigned char bytes[4];
683 long input; 683 long input;
684 unsigned char *buf = (unsigned char *) &input; 684 unsigned char *buf = (unsigned char *) &input;
685 685
698 698
699 return(READSTAT); 699 return(READSTAT);
700 } 700 }
701 701
702 int 702 int
703 rs_read_longs(int inf, long *i, int count) 703 rs_read_longs(FILE *inf, long *i, int count)
704 { 704 {
705 int n = 0, value = 0; 705 int n = 0, value = 0;
706 706
707 if (rs_read_int(inf,&value) != 0) 707 if (rs_read_int(inf,&value) != 0)
708 { 708 {
717 717
718 return(READSTAT); 718 return(READSTAT);
719 } 719 }
720 720
721 int 721 int
722 rs_read_ulong(int inf, unsigned long *i) 722 rs_read_ulong(FILE *inf, unsigned long *i)
723 { 723 {
724 unsigned char bytes[4]; 724 unsigned char bytes[4];
725 unsigned long input; 725 unsigned long input;
726 unsigned char *buf = (unsigned char *) &input; 726 unsigned char *buf = (unsigned char *) &input;
727 727
740 740
741 return(READSTAT); 741 return(READSTAT);
742 } 742 }
743 743
744 int 744 int
745 rs_read_ulongs(int inf, unsigned long *i, int count) 745 rs_read_ulongs(FILE *inf, unsigned long *i, int count)
746 { 746 {
747 int n = 0, value = 0; 747 int n = 0, value = 0;
748 748
749 if (rs_read_int(inf,&value) != 0) 749 if (rs_read_int(inf,&value) != 0)
750 { 750 {
759 759
760 return(READSTAT); 760 return(READSTAT);
761 } 761 }
762 762
763 int 763 int
764 rs_read_string(int inf, char *s, int max) 764 rs_read_string(FILE *inf, char *s, int max)
765 { 765 {
766 int len = 0; 766 int len = 0;
767 767
768 if (rs_read_int(inf, &len) != FALSE) 768 if (rs_read_int(inf, &len) != FALSE)
769 { 769 {
779 779
780 return(READSTAT); 780 return(READSTAT);
781 } 781 }
782 782
783 int 783 int
784 rs_read_new_string(int inf, char **s) 784 rs_read_new_string(FILE *inf, char **s)
785 { 785 {
786 int len=0; 786 int len=0;
787 char *buf=0; 787 char *buf=0;
788 788
789 if (rs_read_int(inf, &len) != 0) 789 if (rs_read_int(inf, &len) != 0)
806 806
807 return(READSTAT); 807 return(READSTAT);
808 } 808 }
809 809
810 int 810 int
811 rs_read_string_index(int inf, char *master[], int maxindex, char **str) 811 rs_read_string_index(FILE *inf, char *master[], int maxindex, char **str)
812 { 812 {
813 int i; 813 int i;
814 814
815 if (rs_read_int(inf,&i) != 0) 815 if (rs_read_int(inf,&i) != 0)
816 { 816 {
828 828
829 return(READSTAT); 829 return(READSTAT);
830 } 830 }
831 831
832 int 832 int
833 rs_read_strings(int inf, char **s, int count, int max) 833 rs_read_strings(FILE *inf, char **s, int count, int max)
834 { 834 {
835 int len = 0; 835 int len = 0;
836 int n = 0; 836 int n = 0;
837 int value = 0; 837 int value = 0;
838 838
856 856
857 return(READSTAT); 857 return(READSTAT);
858 } 858 }
859 859
860 int 860 int
861 rs_read_new_strings(int inf, char **s, int count) 861 rs_read_new_strings(FILE *inf, char **s, int count)
862 { 862 {
863 int len = 0; 863 int len = 0;
864 int n = 0; 864 int n = 0;
865 int value = 0; 865 int value = 0;
866 866
901 901
902 return(WRITESTAT); 902 return(WRITESTAT);
903 } 903 }
904 904
905 int 905 int
906 rs_read_coord(int inf, struct coord *c) 906 rs_read_coord(FILE *inf, struct coord *c)
907 { 907 {
908 rs_read_int(inf,&c->x); 908 rs_read_int(inf,&c->x);
909 rs_read_int(inf,&c->y); 909 rs_read_int(inf,&c->y);
910 910
911 return(READSTAT); 911 return(READSTAT);
928 928
929 return(WRITESTAT); 929 return(WRITESTAT);
930 } 930 }
931 931
932 int 932 int
933 rs_read_window(int inf, WINDOW *win) 933 rs_read_window(FILE *inf, WINDOW *win)
934 { 934 {
935 int id,row,col,maxlines,maxcols,value,width,height; 935 int id,row,col,maxlines,maxcols,value,width,height;
936 936
937 width = getmaxx(win); 937 width = getmaxx(win);
938 height = getmaxy(win); 938 height = getmaxy(win);
1022 1022
1023 return(WRITESTAT); 1023 return(WRITESTAT);
1024 } 1024 }
1025 1025
1026 int 1026 int
1027 rs_read_daemons(int inf, struct delayed_action *d_list, int count) 1027 rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count)
1028 { 1028 {
1029 int i = 0; 1029 int i = 0;
1030 int func = 0; 1030 int func = 0;
1031 int value = 0; 1031 int value = 0;
1032 int id = 0; 1032 int id = 0;
1128 1128
1129 return(WRITESTAT); 1129 return(WRITESTAT);
1130 } 1130 }
1131 1131
1132 int 1132 int
1133 rs_read_room_reference(int inf, struct room **rp) 1133 rs_read_room_reference(FILE *inf, struct room **rp)
1134 { 1134 {
1135 int i; 1135 int i;
1136 1136
1137 rs_read_int(inf, &i); 1137 rs_read_int(inf, &i);
1138 1138
1171 1171
1172 return(WRITESTAT); 1172 return(WRITESTAT);
1173 } 1173 }
1174 1174
1175 int 1175 int
1176 rs_read_rooms(int inf, struct room *r, int count) 1176 rs_read_rooms(FILE *inf, struct room *r, int count)
1177 { 1177 {
1178 int value = 0, n = 0; 1178 int value = 0, n = 0;
1179 1179
1180 if (rs_read_int(inf,&value) != 0) 1180 if (rs_read_int(inf,&value) != 0)
1181 { 1181 {
1218 1218
1219 return(WRITESTAT); 1219 return(WRITESTAT);
1220 } 1220 }
1221 1221
1222 int 1222 int
1223 rs_read_monlev(int inf, struct monlev *m) 1223 rs_read_monlev(FILE *inf, struct monlev *m)
1224 { 1224 {
1225 rs_read_int(inf, &m->l_lev); 1225 rs_read_int(inf, &m->l_lev);
1226 rs_read_int(inf, &m->h_lev); 1226 rs_read_int(inf, &m->h_lev);
1227 rs_read_boolean(inf, &m->d_wand); 1227 rs_read_boolean(inf, &m->d_wand);
1228 1228
1245 1245
1246 return(WRITESTAT); 1246 return(WRITESTAT);
1247 } 1247 }
1248 1248
1249 int 1249 int
1250 rs_read_magic_items(int inf, struct magic_item *mi, int count) 1250 rs_read_magic_items(FILE *inf, struct magic_item *mi, int count)
1251 { 1251 {
1252 int id; 1252 int id;
1253 int n; 1253 int n;
1254 int value; 1254 int value;
1255 1255
1295 1295
1296 return(WRITESTAT); 1296 return(WRITESTAT);
1297 } 1297 }
1298 1298
1299 int 1299 int
1300 rs_read_real(int inf, struct real *r) 1300 rs_read_real(FILE *inf, struct real *r)
1301 { 1301 {
1302 rs_read_int(inf,&r->a_str); 1302 rs_read_int(inf,&r->a_str);
1303 rs_read_int(inf,&r->a_dex); 1303 rs_read_int(inf,&r->a_dex);
1304 rs_read_int(inf,&r->a_wis); 1304 rs_read_int(inf,&r->a_wis);
1305 rs_read_int(inf,&r->a_con); 1305 rs_read_int(inf,&r->a_con);
1324 1324
1325 return(WRITESTAT); 1325 return(WRITESTAT);
1326 } 1326 }
1327 1327
1328 int 1328 int
1329 rs_read_stats(int inf, struct stats *s) 1329 rs_read_stats(FILE *inf, struct stats *s)
1330 { 1330 {
1331 int id; 1331 int id;
1332 1332
1333 rs_read_int(inf, &id); 1333 rs_read_int(inf, &id);
1334 rs_read_real(inf, &s->s_re); 1334 rs_read_real(inf, &s->s_re);
1360 1360
1361 return(WRITESTAT); 1361 return(WRITESTAT);
1362 } 1362 }
1363 1363
1364 int 1364 int
1365 rs_read_monster_reference(int inf, struct monster **mp) 1365 rs_read_monster_reference(FILE *inf, struct monster **mp)
1366 { 1366 {
1367 int i; 1367 int i;
1368 1368
1369 rs_read_int(inf, &i); 1369 rs_read_int(inf, &i);
1370 1370
1386 1386
1387 return(WRITESTAT); 1387 return(WRITESTAT);
1388 } 1388 }
1389 1389
1390 int 1390 int
1391 rs_read_monster_references(int inf, struct monster *marray[], int count) 1391 rs_read_monster_references(FILE *inf, struct monster *marray[], int count)
1392 { 1392 {
1393 int i; 1393 int i;
1394 1394
1395 for(i = 0; i < count; i++) 1395 for(i = 0; i < count; i++)
1396 rs_read_monster_reference(inf, &marray[i]); 1396 rs_read_monster_reference(inf, &marray[i]);
1440 1440
1441 return(WRITESTAT); 1441 return(WRITESTAT);
1442 } 1442 }
1443 1443
1444 int 1444 int
1445 rs_read_object(int inf, struct object *o) 1445 rs_read_object(FILE *inf, struct object *o)
1446 { 1446 {
1447 int id; 1447 int id;
1448 1448
1449 if (rs_read_int(inf, &id) != 0) 1449 if (rs_read_int(inf, &id) != 0)
1450 { 1450 {
1496 1496
1497 return(READSTAT); 1497 return(READSTAT);
1498 } 1498 }
1499 1499
1500 int 1500 int
1501 rs_read_object_list(int inf, struct linked_list **list) 1501 rs_read_object_list(FILE *inf, struct linked_list **list)
1502 { 1502 {
1503 int id; 1503 int id;
1504 int i, cnt; 1504 int i, cnt;
1505 struct linked_list *l = NULL, *previous = NULL, *head = NULL; 1505 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
1506 1506
1568 } 1568 }
1569 return (WRITESTAT); 1569 return (WRITESTAT);
1570 } 1570 }
1571 1571
1572 int 1572 int
1573 rs_read_traps(int inf, struct trap *trap, int count) 1573 rs_read_traps(FILE *inf, struct trap *trap, int count)
1574 { 1574 {
1575 int id = 0, value = 0, n = 0; 1575 int id = 0, value = 0, n = 0;
1576 1576
1577 if (rs_read_int(inf,&id) != 0) 1577 if (rs_read_int(inf,&id) != 0)
1578 { 1578 {
1626 1626
1627 return(WRITESTAT); 1627 return(WRITESTAT);
1628 } 1628 }
1629 1629
1630 int 1630 int
1631 rs_read_monsters(int inf, struct monster *m, int count) 1631 rs_read_monsters(FILE *inf, struct monster *m, int count)
1632 { 1632 {
1633 int id = 0, value = 0, n = 0; 1633 int id = 0, value = 0, n = 0;
1634 1634
1635 if (rs_read_int(inf, &id) != 0) 1635 if (rs_read_int(inf, &id) != 0)
1636 { 1636 {
1811 1811
1812 return(WRITESTAT); 1812 return(WRITESTAT);
1813 } 1813 }
1814 1814
1815 int 1815 int
1816 rs_read_thing(int inf, struct thing *t) 1816 rs_read_thing(FILE *inf, struct thing *t)
1817 { 1817 {
1818 int id; 1818 int id;
1819 int listid = 0, index = -1; 1819 int listid = 0, index = -1;
1820 struct linked_list *item; 1820 struct linked_list *item;
1821 1821
1929 1929
1930 return(WRITESTAT); 1930 return(WRITESTAT);
1931 } 1931 }
1932 1932
1933 int 1933 int
1934 rs_read_monster_list(int inf, struct linked_list **list) 1934 rs_read_monster_list(FILE *inf, struct linked_list **list)
1935 { 1935 {
1936 int id; 1936 int id;
1937 int i, cnt; 1937 int i, cnt;
1938 struct linked_list *l = NULL, *previous = NULL, *head = NULL; 1938 struct linked_list *l = NULL, *previous = NULL, *head = NULL;
1939 1939
1983 1983
1984 return(WRITESTAT); 1984 return(WRITESTAT);
1985 } 1985 }
1986 1986
1987 int 1987 int
1988 rs_read_object_reference(int inf, struct linked_list *list, 1988 rs_read_object_reference(FILE *inf, struct linked_list *list,
1989 struct object **item) 1989 struct object **item)
1990 { 1990 {
1991 int i; 1991 int i;
1992 1992
1993 rs_read_int(inf, &i); 1993 rs_read_int(inf, &i);
1997 } 1997 }
1998 1998
1999 1999
2000 2000
2001 int 2001 int
2002 rs_read_scrolls(int inf) 2002 rs_read_scrolls(FILE *inf)
2003 { 2003 {
2004 int i; 2004 int i;
2005 2005
2006 for(i = 0; i < MAXSCROLLS; i++) 2006 for(i = 0; i < MAXSCROLLS; i++)
2007 { 2007 {
2026 } 2026 }
2027 return(READSTAT); 2027 return(READSTAT);
2028 } 2028 }
2029 2029
2030 int 2030 int
2031 rs_read_potions(int inf) 2031 rs_read_potions(FILE *inf)
2032 { 2032 {
2033 int i; 2033 int i;
2034 2034
2035 for(i = 0; i < MAXPOTIONS; i++) 2035 for(i = 0; i < MAXPOTIONS; i++)
2036 { 2036 {
2056 2056
2057 return(WRITESTAT); 2057 return(WRITESTAT);
2058 } 2058 }
2059 2059
2060 int 2060 int
2061 rs_read_rings(int inf) 2061 rs_read_rings(FILE *inf)
2062 { 2062 {
2063 int i; 2063 int i;
2064 2064
2065 for(i = 0; i < MAXRINGS; i++) 2065 for(i = 0; i < MAXRINGS; i++)
2066 { 2066 {
2112 2112
2113 return(WRITESTAT); 2113 return(WRITESTAT);
2114 } 2114 }
2115 2115
2116 int 2116 int
2117 rs_read_sticks(int inf) 2117 rs_read_sticks(FILE *inf)
2118 { 2118 {
2119 int i = 0, list = 0; 2119 int i = 0, list = 0;
2120 2120
2121 for(i = 0; i < MAXSTICKS; i++) 2121 for(i = 0; i < MAXSTICKS; i++)
2122 { 2122 {
2239 2239
2240 return(WRITESTAT); 2240 return(WRITESTAT);
2241 } 2241 }
2242 2242
2243 int 2243 int
2244 rs_restore_file(int inf) 2244 rs_restore_file(FILE *inf)
2245 { 2245 {
2246 bool junk; 2246 bool junk;
2247 int endian = 0x01020304; 2247 int endian = 0x01020304;
2248 big_endian = ( *((char *)&endian) == 0x01 ); 2248 big_endian = ( *((char *)&endian) == 0x01 );
2249 2249