Mercurial > hg > early-roguelike
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); |
