📄 io.c
字号:
/*- * Copyright (c) 1991, 1993 * The Regents of the University of California. All rights reserved. * * The game adventure was originally written in Fortran by Will Crowther * and Don Woods. It was later translated to C and enhanced by Jim * Gillogly. This code is derived from software contributed to Berkeley * by Jim Gillogly at The Rand Corporation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char sccsid[] = "@(#)io.c 8.1 (Berkeley) 5/31/93";#endif /* not lint *//* Re-coding of advent in C: file i/o and user i/o */#include "hdr.h"#include <stdio.h>getin(wrd1,wrd2) /* get command from user */char **wrd1,**wrd2; /* no prompt, usually */{ register char *s; static char wd1buf[MAXSTR],wd2buf[MAXSTR]; int first, numch; *wrd1=wd1buf; /* return ptr to internal string*/ *wrd2=wd2buf; wd2buf[0]=0; /* in case it isn't set here */ for (s=wd1buf, first=1, numch=0;;) { if ((*s=getchar())>='A' && *s <='Z') *s = *s - ('A' -'a'); /* convert to upper case */ switch(*s) /* start reading from user */ { case '\n': *s=0; return; case ' ': if (s==wd1buf||s==wd2buf) /* initial blank */ continue; *s=0; if (first) /* finished 1st wd; start 2nd */ { first=numch=0; s=wd2buf; break; } else /* finished 2nd word */ { FLUSHLINE; *s=0; return; } default: if (++numch>=MAXSTR) /* string too long */ { printf("Give me a break!!\n"); wd1buf[0]=wd2buf[0]=0; FLUSHLINE; return; } s++; } }}confirm(mesg) /* confirm irreversible action */char *mesg;{ register int result; printf("%s",mesg); /* tell him what he did */ if (getchar()=='y') /* was his first letter a 'y'? */ result=1; else result=0; FLUSHLINE; return(result);}yes(x,y,z) /* confirm with rspeak */int x,y,z;{ register int result; register char ch; for (;;) { rspeak(x); /* tell him what we want*/ if ((ch=getchar())=='y') result=TRUE; else if (ch=='n') result=FALSE; FLUSHLINE; if (ch=='y'|| ch=='n') break; printf("Please answer the question.\n"); } if (result==TRUE) rspeak(y); if (result==FALSE) rspeak(z); return(result);}yesm(x,y,z) /* confirm with mspeak */int x,y,z;{ register int result; register char ch; for (;;) { mspeak(x); /* tell him what we want*/ if ((ch=getchar())=='y') result=TRUE; else if (ch=='n') result=FALSE; FLUSHLINE; if (ch=='y'|| ch=='n') break; printf("Please answer the question.\n"); } if (result==TRUE) mspeak(y); if (result==FALSE) mspeak(z); return(result);}/* FILE *inbuf,*outbuf; */char *inptr; /* Pointer into virtual disk */int outsw = 0; /* putting stuff to data file? */char iotape[] = "Ax3F'\003tt$8h\315qer*h\017nGKrX\207:!l";char *tape = iotape; /* pointer to encryption tape */next() /* next virtual char, bump adr */{ int ch; ch=(*inptr ^ random()) & 0xFF; /* Decrypt input data */ if (outsw) /* putting data in tmp file */ { if (*tape==0) tape=iotape; /* rewind encryption tape */ *inptr = ch ^ *tape++; /* re-encrypt and replace value */ } inptr++; return(ch);}char breakch; /* tell which char ended rnum */rdata() /* "read" data from virtual file*/{ register int sect; register char ch; inptr = data_file; /* Pointer to virtual data file */ srandom(SEED); /* which is lightly encrypted. */ clsses=1; for (;;) /* read data sections */ { sect=next()-'0'; /* 1st digit of section number */#ifdef VERBOSE printf("Section %c",sect+'0');#endif if ((ch=next())!=LF) /* is there a second digit? */ { FLUSHLF;#ifdef VERBOSE putchar(ch);#endif sect=10*sect+ch-'0'; }#ifdef VERBOSE putchar('\n');#endif switch(sect) { case 0: /* finished reading database */ return; case 1: /* long form descriptions */ rdesc(1); break; case 2: /* short form descriptions */ rdesc(2); break; case 3: /* travel table */ rtrav(); break; case 4: /* vocabulary */ rvoc(); break; case 5: /* object descriptions */ rdesc(5); break; case 6: /* arbitrary messages */ rdesc(6); break; case 7: /* object locations */ rlocs(); break; case 8: /* action defaults */ rdflt(); break; case 9: /* liquid assets */ rliq(); break; case 10: /* class messages */ rdesc(10); break; case 11: /* hints */ rhints(); break; case 12: /* magic messages */ rdesc(12); break; default: printf("Invalid data section number: %d\n",sect); for (;;) putchar(next()); } if (breakch!=LF) /* routines return after "-1" */ FLUSHLF; }}char nbf[12];rnum() /* read initial location num */{ register char *s; tape = iotape; /* restart encryption tape */ for (s=nbf,*s=0;; s++) if ((*s=next())==TAB || *s=='\n' || *s==LF) break; breakch= *s; /* save char for rtrav() */ *s=0; /* got the number as ascii */ if (nbf[0]=='-') return(-1); /* end of data */ return(atoi(nbf)); /* convert it to integer */}char *seekhere;rdesc(sect) /* read description-format msgs */int sect;{ register char *s,*t; register int locc; char *seekstart, *maystart, *adrstart; char *entry; seekhere = inptr; /* Where are we in virtual file?*/ outsw=1; /* these msgs go into tmp file */ for (oldloc= -1, seekstart=seekhere;;) { maystart=inptr; /* maybe starting new entry */ if ((locc=rnum())!=oldloc && oldloc>=0 /* finished msg */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -