Mercurial > hg > early-roguelike
comparison xrogue/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 | 3d4252fa2ed3 |
| children | 6376b514a30b |
comparison
equal
deleted
inserted
replaced
| 278:c222f9d56776 | 279:d3968e9cb98d |
|---|---|
| 94 int format_error = FALSE; | 94 int format_error = FALSE; |
| 95 | 95 |
| 96 int save_debug = FALSE; | 96 int save_debug = FALSE; |
| 97 #define DBG(x) {if (save_debug) rsPrintf x;} | 97 #define DBG(x) {if (save_debug) rsPrintf x;} |
| 98 | 98 |
| 99 bool rs_read_new_string(int inf, char **s); | 99 bool rs_read_new_string(FILE *inf, char **s); |
| 100 int find_list_ptr(struct linked_list *l, void *ptr); | 100 int find_list_ptr(struct linked_list *l, void *ptr); |
| 101 bool rs_write_coord_list(FILE *savef, struct linked_list *l); | 101 bool rs_write_coord_list(FILE *savef, struct linked_list *l); |
| 102 bool rs_read_coord_list(int inf, struct linked_list **list); | 102 bool rs_read_coord_list(FILE *inf, struct linked_list **list); |
| 103 int list_size(struct linked_list *l); | 103 int list_size(struct linked_list *l); |
| 104 bool rs_write_object_list(FILE *savef, struct linked_list *l); | 104 bool rs_write_object_list(FILE *savef, struct linked_list *l); |
| 105 bool rs_read_object_list(int inf, struct linked_list **list); | 105 bool rs_read_object_list(FILE *inf, struct linked_list **list); |
| 106 | 106 |
| 107 int | 107 int |
| 108 rsPrintf(char *fmt, ...) | 108 rsPrintf(char *fmt, ...) |
| 109 { | 109 { |
| 110 va_list ap; | 110 va_list ap; |
| 150 } | 150 } |
| 151 | 151 |
| 152 int end_of_file = FALSE; | 152 int end_of_file = FALSE; |
| 153 | 153 |
| 154 bool | 154 bool |
| 155 rs_read(int inf, void *ptr, size_t size) | 155 rs_read(FILE *inf, void *ptr, size_t size) |
| 156 { | 156 { |
| 157 int actual; | 157 int actual; |
| 158 end_of_file =FALSE; | 158 end_of_file =FALSE; |
| 159 if (!read_error && !format_error) | 159 if (!read_error && !format_error) |
| 160 { | 160 { |
| 282 | 282 |
| 283 return(WRITESTAT); | 283 return(WRITESTAT); |
| 284 } | 284 } |
| 285 | 285 |
| 286 bool | 286 bool |
| 287 rs_read_int(int inf, int *i) | 287 rs_read_int(FILE *inf, int *i) |
| 288 { | 288 { |
| 289 char bytes[4]; | 289 char bytes[4]; |
| 290 int input; | 290 int input; |
| 291 char *buf = (char *)&input; | 291 char *buf = (char *)&input; |
| 292 | 292 |
| 305 | 305 |
| 306 return(READSTAT); | 306 return(READSTAT); |
| 307 } | 307 } |
| 308 | 308 |
| 309 bool | 309 bool |
| 310 rs_read_uint(int inf, unsigned int *i) | 310 rs_read_uint(FILE *inf, unsigned int *i) |
| 311 { | 311 { |
| 312 char bytes[4]; | 312 char bytes[4]; |
| 313 int input; | 313 int input; |
| 314 char *buf = (char *)&input; | 314 char *buf = (char *)&input; |
| 315 | 315 |
| 328 | 328 |
| 329 return(READSTAT); | 329 return(READSTAT); |
| 330 } | 330 } |
| 331 | 331 |
| 332 bool | 332 bool |
| 333 rs_read_ulong(int inf, unsigned long *i) | 333 rs_read_ulong(FILE *inf, unsigned long *i) |
| 334 { | 334 { |
| 335 char bytes[4]; | 335 char bytes[4]; |
| 336 unsigned long input; | 336 unsigned long input; |
| 337 char *buf = (char *) &input; | 337 char *buf = (char *) &input; |
| 338 | 338 |
| 353 *i = *((unsigned long *) buf); | 353 *i = *((unsigned long *) buf); |
| 354 return(READSTAT); | 354 return(READSTAT); |
| 355 } | 355 } |
| 356 | 356 |
| 357 bool | 357 bool |
| 358 rs_read_long(int inf, long *i) | 358 rs_read_long(FILE *inf, long *i) |
| 359 { | 359 { |
| 360 char bytes[4]; | 360 char bytes[4]; |
| 361 long input; | 361 long input; |
| 362 char *buf = (char *) &input; | 362 char *buf = (char *) &input; |
| 363 | 363 |
| 378 *i = *((long *) buf); | 378 *i = *((long *) buf); |
| 379 return(READSTAT); | 379 return(READSTAT); |
| 380 } | 380 } |
| 381 | 381 |
| 382 bool | 382 bool |
| 383 rs_read_boolean(int inf, bool *i) | 383 rs_read_boolean(FILE *inf, bool *i) |
| 384 { | 384 { |
| 385 char buf; | 385 char buf; |
| 386 | 386 |
| 387 rs_read(inf, &buf, 1); | 387 rs_read(inf, &buf, 1); |
| 388 | 388 |
| 421 | 421 |
| 422 return(WRITESTAT); | 422 return(WRITESTAT); |
| 423 } | 423 } |
| 424 | 424 |
| 425 bool | 425 bool |
| 426 rs_read_short(int inf, short *s) | 426 rs_read_short(FILE *inf, short *s) |
| 427 { | 427 { |
| 428 char bytes[2]; | 428 char bytes[2]; |
| 429 short input; | 429 short input; |
| 430 char *buf = (char *)&input; | 430 char *buf = (char *)&input; |
| 431 | 431 |
| 494 | 494 |
| 495 return(WRITESTAT); | 495 return(WRITESTAT); |
| 496 } | 496 } |
| 497 | 497 |
| 498 bool | 498 bool |
| 499 rs_read_ints(int inf, int *i, int count) | 499 rs_read_ints(FILE *inf, int *i, int count) |
| 500 { | 500 { |
| 501 int n=0,value=0; | 501 int n=0,value=0; |
| 502 | 502 |
| 503 if (rs_read_int(inf,&value) != 0) | 503 if (rs_read_int(inf,&value) != 0) |
| 504 { | 504 { |
| 513 | 513 |
| 514 return(READSTAT); | 514 return(READSTAT); |
| 515 } | 515 } |
| 516 | 516 |
| 517 bool | 517 bool |
| 518 rs_read_shorts(int inf, short *i, int count) | 518 rs_read_shorts(FILE *inf, short *i, int count) |
| 519 { | 519 { |
| 520 int n=0,value=0; | 520 int n=0,value=0; |
| 521 | 521 |
| 522 if (rs_read_int(inf,&value) != 0) | 522 if (rs_read_int(inf,&value) != 0) |
| 523 { | 523 { |
| 532 | 532 |
| 533 return(READSTAT); | 533 return(READSTAT); |
| 534 } | 534 } |
| 535 | 535 |
| 536 bool | 536 bool |
| 537 rs_read_longs(int inf, long *i, int count) | 537 rs_read_longs(FILE *inf, long *i, int count) |
| 538 { | 538 { |
| 539 int n=0,value=0; | 539 int n=0,value=0; |
| 540 | 540 |
| 541 if (rs_read_int(inf,&value) != 0) | 541 if (rs_read_int(inf,&value) != 0) |
| 542 { | 542 { |
| 551 | 551 |
| 552 return(READSTAT); | 552 return(READSTAT); |
| 553 } | 553 } |
| 554 | 554 |
| 555 bool | 555 bool |
| 556 rs_read_ulongs(int inf, unsigned long *i, int count) | 556 rs_read_ulongs(FILE *inf, unsigned long *i, int count) |
| 557 { | 557 { |
| 558 int n=0,value=0; | 558 int n=0,value=0; |
| 559 | 559 |
| 560 if (rs_read_int(inf,&value) != 0) | 560 if (rs_read_int(inf,&value) != 0) |
| 561 { | 561 { |
| 570 | 570 |
| 571 return(READSTAT); | 571 return(READSTAT); |
| 572 } | 572 } |
| 573 | 573 |
| 574 bool | 574 bool |
| 575 rs_read_booleans(int inf, bool *i, int count) | 575 rs_read_booleans(FILE *inf, bool *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 { |
| 609 | 609 |
| 610 return(WRITESTAT); | 610 return(WRITESTAT); |
| 611 } | 611 } |
| 612 | 612 |
| 613 bool | 613 bool |
| 614 rs_read_levtype(int inf, LEVTYPE *l) | 614 rs_read_levtype(FILE *inf, LEVTYPE *l) |
| 615 { | 615 { |
| 616 int lt; | 616 int lt; |
| 617 | 617 |
| 618 rs_read_int(inf, <); | 618 rs_read_int(inf, <); |
| 619 | 619 |
| 638 | 638 |
| 639 return(WRITESTAT); | 639 return(WRITESTAT); |
| 640 } | 640 } |
| 641 | 641 |
| 642 bool | 642 bool |
| 643 rs_read_char(int inf, char *c) | 643 rs_read_char(FILE *inf, char *c) |
| 644 { | 644 { |
| 645 rs_read(inf, c, 1); | 645 rs_read(inf, c, 1); |
| 646 | 646 |
| 647 return(READSTAT); | 647 return(READSTAT); |
| 648 } | 648 } |
| 655 | 655 |
| 656 return(WRITESTAT); | 656 return(WRITESTAT); |
| 657 } | 657 } |
| 658 | 658 |
| 659 bool | 659 bool |
| 660 rs_read_uchar(int inf, unsigned char *c) | 660 rs_read_uchar(FILE *inf, unsigned char *c) |
| 661 { | 661 { |
| 662 rs_read(inf, c, 1); | 662 rs_read(inf, c, 1); |
| 663 | 663 |
| 664 return(READSTAT); | 664 return(READSTAT); |
| 665 } | 665 } |
| 676 | 676 |
| 677 return(WRITESTAT); | 677 return(WRITESTAT); |
| 678 } | 678 } |
| 679 | 679 |
| 680 bool | 680 bool |
| 681 rs_read_string_index(int inf, struct words master[], int maxindex, char **str) | 681 rs_read_string_index(FILE *inf, struct words master[], int maxindex, char **str) |
| 682 { | 682 { |
| 683 int i; | 683 int i; |
| 684 | 684 |
| 685 if (rs_read_int(inf,&i) != 0) | 685 if (rs_read_int(inf,&i) != 0) |
| 686 { | 686 { |
| 716 rs_write_int(savef,-1); | 716 rs_write_int(savef,-1); |
| 717 return(WRITESTAT); | 717 return(WRITESTAT); |
| 718 } | 718 } |
| 719 | 719 |
| 720 bool | 720 bool |
| 721 rs_read_scrolls(int inf) | 721 rs_read_scrolls(FILE *inf) |
| 722 { | 722 { |
| 723 int i; | 723 int i; |
| 724 | 724 |
| 725 for(i = 0; i < MAXSCROLLS; i++) | 725 for(i = 0; i < MAXSCROLLS; i++) |
| 726 { | 726 { |
| 745 } | 745 } |
| 746 return(WRITESTAT); | 746 return(WRITESTAT); |
| 747 } | 747 } |
| 748 | 748 |
| 749 bool | 749 bool |
| 750 rs_read_potions(int inf) | 750 rs_read_potions(FILE *inf) |
| 751 { | 751 { |
| 752 int i; | 752 int i; |
| 753 | 753 |
| 754 for(i = 0; i < MAXPOTIONS; i++) | 754 for(i = 0; i < MAXPOTIONS; i++) |
| 755 { | 755 { |
| 775 | 775 |
| 776 return(WRITESTAT); | 776 return(WRITESTAT); |
| 777 } | 777 } |
| 778 | 778 |
| 779 bool | 779 bool |
| 780 rs_read_rings(int inf) | 780 rs_read_rings(FILE *inf) |
| 781 { | 781 { |
| 782 int i; | 782 int i; |
| 783 | 783 |
| 784 for(i = 0; i < MAXRINGS; i++) | 784 for(i = 0; i < MAXRINGS; i++) |
| 785 { | 785 { |
| 805 | 805 |
| 806 return(WRITESTAT); | 806 return(WRITESTAT); |
| 807 } | 807 } |
| 808 | 808 |
| 809 bool | 809 bool |
| 810 rs_read_misc(int inf) | 810 rs_read_misc(FILE *inf) |
| 811 { | 811 { |
| 812 int i; | 812 int i; |
| 813 | 813 |
| 814 for(i = 0; i < MAXMM; i++) | 814 for(i = 0; i < MAXMM; i++) |
| 815 { | 815 { |
| 857 | 857 |
| 858 return(WRITESTAT); | 858 return(WRITESTAT); |
| 859 } | 859 } |
| 860 | 860 |
| 861 bool | 861 bool |
| 862 rs_read_sticks(int inf) | 862 rs_read_sticks(FILE *inf) |
| 863 { | 863 { |
| 864 int i = 0, list = 0; | 864 int i = 0, list = 0; |
| 865 | 865 |
| 866 for(i = 0; i < MAXSTICKS; i++) | 866 for(i = 0; i < MAXSTICKS; i++) |
| 867 { | 867 { |
| 882 | 882 |
| 883 return(READSTAT); | 883 return(READSTAT); |
| 884 } | 884 } |
| 885 | 885 |
| 886 bool | 886 bool |
| 887 rs_read_string(int inf, char *s, int max) | 887 rs_read_string(FILE *inf, char *s, int max) |
| 888 { | 888 { |
| 889 int len = 0; | 889 int len = 0; |
| 890 | 890 |
| 891 if (rs_read_int(inf, &len) != FALSE) | 891 if (rs_read_int(inf, &len) != FALSE) |
| 892 { | 892 { |
| 902 | 902 |
| 903 return(READSTAT); | 903 return(READSTAT); |
| 904 } | 904 } |
| 905 | 905 |
| 906 bool | 906 bool |
| 907 rs_read_new_string(int inf, char **s) | 907 rs_read_new_string(FILE *inf, char **s) |
| 908 { | 908 { |
| 909 int len=0; | 909 int len=0; |
| 910 char *buf=0; | 910 char *buf=0; |
| 911 | 911 |
| 912 if (rs_read_int(inf, &len) != 0) | 912 if (rs_read_int(inf, &len) != 0) |
| 964 | 964 |
| 965 return(WRITESTAT); | 965 return(WRITESTAT); |
| 966 } | 966 } |
| 967 | 967 |
| 968 bool | 968 bool |
| 969 rs_read_words(int inf, struct words *w, int count) | 969 rs_read_words(FILE *inf, struct words *w, int count) |
| 970 { | 970 { |
| 971 int n = 0; | 971 int n = 0; |
| 972 int value = 0; | 972 int value = 0; |
| 973 | 973 |
| 974 rs_read_int(inf,&value); | 974 rs_read_int(inf,&value); |
| 987 | 987 |
| 988 return(READSTAT); | 988 return(READSTAT); |
| 989 } | 989 } |
| 990 | 990 |
| 991 bool | 991 bool |
| 992 rs_read_new_strings(int inf, char **s, int count) | 992 rs_read_new_strings(FILE *inf, char **s, int count) |
| 993 { | 993 { |
| 994 int len = 0; | 994 int len = 0; |
| 995 int n = 0; | 995 int n = 0; |
| 996 int value = 0; | 996 int value = 0; |
| 997 | 997 |
| 1032 | 1032 |
| 1033 return(WRITESTAT); | 1033 return(WRITESTAT); |
| 1034 } | 1034 } |
| 1035 | 1035 |
| 1036 bool | 1036 bool |
| 1037 rs_read_coord(int inf, coord *c) | 1037 rs_read_coord(FILE *inf, coord *c) |
| 1038 { | 1038 { |
| 1039 rs_read_int(inf,&c->x); | 1039 rs_read_int(inf,&c->x); |
| 1040 rs_read_int(inf,&c->y); | 1040 rs_read_int(inf,&c->y); |
| 1041 | 1041 |
| 1042 return(READSTAT); | 1042 return(READSTAT); |
| 1231 | 1231 |
| 1232 return(WRITESTAT); | 1232 return(WRITESTAT); |
| 1233 } | 1233 } |
| 1234 | 1234 |
| 1235 bool | 1235 bool |
| 1236 rs_read_daemons(int inf, struct delayed_action *d_list, int count) | 1236 rs_read_daemons(FILE *inf, struct delayed_action *d_list, int count) |
| 1237 { | 1237 { |
| 1238 int i = 0; | 1238 int i = 0; |
| 1239 int func = 0; | 1239 int func = 0; |
| 1240 int value = 0; | 1240 int value = 0; |
| 1241 int id = 0; | 1241 int id = 0; |
| 1436 } | 1436 } |
| 1437 return(WRITESTAT); | 1437 return(WRITESTAT); |
| 1438 } | 1438 } |
| 1439 | 1439 |
| 1440 bool | 1440 bool |
| 1441 rs_read_rooms(int inf, struct room *r, int count) | 1441 rs_read_rooms(FILE *inf, struct room *r, int count) |
| 1442 { | 1442 { |
| 1443 int value = 0, n = 0, i = 0, index = 0, id = 0; | 1443 int value = 0, n = 0, i = 0, index = 0, id = 0; |
| 1444 struct linked_list *fires=NULL, *item = NULL; | 1444 struct linked_list *fires=NULL, *item = NULL; |
| 1445 | 1445 |
| 1446 if (rs_read_int(inf,&id) != 0) | 1446 if (rs_read_int(inf,&id) != 0) |
| 1549 | 1549 |
| 1550 return(WRITESTAT); | 1550 return(WRITESTAT); |
| 1551 } | 1551 } |
| 1552 | 1552 |
| 1553 bool | 1553 bool |
| 1554 rs_read_object(int inf, struct object *o) | 1554 rs_read_object(FILE *inf, struct object *o) |
| 1555 { | 1555 { |
| 1556 int id; | 1556 int id; |
| 1557 | 1557 |
| 1558 if (rs_read_int(inf, &id) != 0) | 1558 if (rs_read_int(inf, &id) != 0) |
| 1559 { | 1559 { |
| 1610 | 1610 |
| 1611 return(WRITESTAT); | 1611 return(WRITESTAT); |
