-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadviio.c
93 lines (74 loc) · 1.74 KB
/
adviio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include "advint.h"
int advgetc(void)
{
return getchar();
}
void advwaitc(void)
{
getchar();
}
int advputc(int ch,FILE *fp)
{
return putc(ch,fp);
}
int advsave(char *hdr,int hlen,char *save,int slen)
{
char fname[50],*p;
FILE *fp;
trm_str("File name? ");
if (!(p = trm_get()))
return 0;
/* add the extension */
sprintf(fname,"%s.sav",p);
/* create the data file */
if ((fp = fopen(fname,"wb")) == NULL)
return 0;
/* write the header */
if (fwrite(hdr,hlen,1,fp) != 1) {
fclose(fp);
return 0;
}
/* write the data */
if (fwrite(save,slen,1,fp) != 1) {
fclose(fp);
return 0;
}
/* close the file and return successfully */
fclose(fp);
return 1;
}
int advrestore(char *hdr,int hlen,char *save,int slen)
{
char fname[50],hbuf[50],*p;
FILE *fp;
if (hlen > 50)
error("save file header buffer too small");
trm_str("File name? ");
if (!(p = trm_get()))
return 0;
/* add the extension */
sprintf(fname,"%s.sav",p);
/* open the data file */
if ((fp = fopen(fname,"rb")) == NULL)
return 0;
/* read the header */
if (fread(hbuf,hlen,1,fp) != 1) {
fclose(fp);
return 0;
}
/* compare the headers */
for (p = hbuf; hlen--; )
if (*hdr++ != *p++) {
trm_str("This save file does not match the adventure!\n");
return 0;
}
/* read the data */
if (fread(save,slen,1,fp) != 1) {
fclose(fp);
return 0;
}
/* close the file and return successfully */
fclose(fp);
return 1;
}