home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fish 'n' More 2
/
fishmore-publicdomainlibraryvol.ii1991xetec.iso
/
dirs
/
cross_464.lzh
/
Cross
/
txt
/
CPCDosIO.mod
< prev
next >
Wrap
Text File
|
1991-03-09
|
18KB
|
808 lines
IMPLEMENTATION MODULE CPCDosIO;
FROM Arts
IMPORT BreakPoint,CurrentLevel,Terminate,TermProcedure;
FROM ASCII
IMPORT lf,nul,esc;
FROM Conversions
IMPORT ValToStr,StrToVal;
FROM CPCError
IMPORT myAssert;
FROM FileReq
IMPORT FileRequestData,MakeFRD,FileReq,FileString;
FROM CPCGlobal
IMPORT string,lstring,stringlen,lstringlen,msg,maxmsg,window,maxwords,
puzzlewords,maxgrid,column,field,wordfield,puzzlewordfield,text,
kwr,words,hori,vert,xmax,ymax;
FROM CPCRequesters
IMPORT YesOrNo;
FROM CPCSleep
IMPORT NormalPointer,SleepPointer;
FROM Dos
IMPORT Open,Read,Write,newFile,FileHandlePtr;
IMPORT Dos;
FROM FileMessage
IMPORT ResponseText,StrPtr;
FROM FileSystem
IMPORT Response,File,Lookup,Close,ReadChar,WriteBytes,WriteChar,ReadBytes;
FROM InOut
IMPORT WriteLn,WriteString,WriteHex,WriteInt;
IMPORT InOut;
FROM RandomNumber
IMPORT RND,PutSeed;
FROM Requester
IMPORT Text,SetReqBorderPen,SetReqTextPen,ReqFlags,ReqFlagSet;
FROM RequesterSet
IMPORT BooleanRequest;
FROM Strings
IMPORT Copy,Compare,Length;
FROM SYSTEM
IMPORT ADDRESS,ADR,CAST;
CONST
errReadingMsg="ERROR READING MSG.TXT:";
ioErrReadingMsg="I/O-ERROR READING MSG.TXT:";
defworddir="Cross:data/";
defwordfile="words01.crw";
defdatadir="Cross:data/";
defdatafile="puzzle01.crd";
defmsg="Cross:data/msgtxt.data";
TYPE
IOMode=(readW,readC,writeC,printC,printS);
VAR
printer,parallel: FileHandlePtr;
myfile: File;
filepresent: BOOLEAN;
initialized: BOOLEAN;
dataReq,wordReq: FileRequestData;
PROCEDURE CoolDown;
BEGIN
IF printer#NIL THEN
Dos.Close(printer);
END;
IF parallel#NIL THEN
Dos.Close(parallel);
END;
END CoolDown;
PROCEDURE InitCPCDosIO;
BEGIN
IF NOT initialized THEN
initialized:=TRUE;
MakeFRD("",defwordfile,defworddir,CAST(ADDRESS,window),wordReq);
MakeFRD("",defdatafile,defdatadir,CAST(ADDRESS,window),dataReq);
END;
END InitCPCDosIO;
PROCEDURE Value(a: ARRAY OF CHAR): INTEGER;
VAR err,sgn: BOOLEAN;
val: LONGINT;
l: INTEGER;
BEGIN
FOR l:=0 TO 3 DO
IF (a[l]=" ") THEN
a[l]:="0";
END;
END;
sgn:=FALSE;
StrToVal(a,val,sgn,10,err);
IF (err) THEN
val:=-1;
END;
RETURN INTEGER(val);
END Value;
PROCEDURE AppendStr(VAR a: ARRAY OF CHAR; b: ARRAY OF CHAR);
VAR l,x,pos: INTEGER;
BEGIN
l:=Length(a);
FOR x:=0 TO Length(b)-1 DO
a[l+x]:=b[x];
END;
pos:=Length(b);
a[pos+l]:=nul;
END AppendStr;
PROCEDURE MakeComStr(VAR s: ARRAY OF CHAR; c0,c1,c2,c3,c4,c5,c6,c7: CHAR);
BEGIN
s[0]:=c0;
s[1]:=c1;
s[2]:=c2;
s[3]:=c3;
s[4]:=c4;
s[5]:=c5;
s[6]:=c6;
s[7]:=c7;
s[8]:=nul;
END MakeComStr;
PROCEDURE PutPRT(s: StrPtr; l: LONGINT): BOOLEAN; (* TRUE if error *)
VAR
act: LONGINT;
BEGIN
myAssert(printer#NIL,ADR(msg[66]),ADR(msg[67]));
act:=Write(printer,s,l);
RETURN (act#l);
END PutPRT;
PROCEDURE PutPAR(s: StrPtr; l: LONGINT): BOOLEAN; (* TRUE if ERROR *)
VAR
act: LONGINT;
BEGIN
myAssert(parallel#NIL,ADR(msg[66]),ADR(msg[68]));
act:=Write(parallel,s,l);
RETURN (act#l);
END PutPAR;
PROCEDURE readline(VAR f: File; VAR a,number: lstring; m: msgmode);
VAR c: CHAR;
n: INTEGER;
sp: StrPtr;
BEGIN
n:=0;
ReadChar(f,c);
IF ((ORD(c)>=ORD("0")) AND (ORD(c)<=ORD("9"))) THEN
a[n]:=c;
INC(n);
REPEAT
myAssert(n<=lstringlen,ADR(errReadingMsg),ADR("STRING TOO LONG"));
ReadChar(f,c);
ResponseText(f.res,sp);
myAssert(f.res=done,ADR(ioErrReadingMsg),sp);
a[n]:=c;
INC(n);
UNTIL (c=" ");
a[n-1]:=nul;
number:=a;
IF (m=nonumbers) THEN
n:=0;
a[0]:=nul;
ELSE
DEC(n);
END;
ELSE
number:="illegal";
a[0]:=c;
n:=1;
END;
REPEAT
myAssert(n<=lstringlen,ADR(errReadingMsg),ADR("STRING TOO LONG"));
ReadChar(f,c);
ResponseText(f.res,sp);
myAssert(f.res=done,ADR(ioErrReadingMsg),sp);
IF (c="@") THEN
c:=" ";
END;
a[n]:=c;
INC(n);
UNTIL (c=lf);
a[n-1]:=nul;
END readline;
PROCEDURE ReadMsg(m: msgmode);
VAR c: CHAR;
lcount,i,k,l,n,langno: INTEGER;
a,num: lstring;
b: ARRAY[1..9] OF lstring;
sp: StrPtr;
BEGIN
Lookup(myfile,defmsg,5000,FALSE);
filepresent:=TRUE;
myAssert(myfile.res=done,ADR(ioErrReadingMsg),ADR("CAN'T OPEN MSGTXT.DATA"));
ReadChar(myfile,c);
myAssert((c>"0")&(c<":"),ADR(errReadingMsg),ADR("ILLEGAL LANGUAGE COUNT"));
lcount:=(ORD(c)-ORD("0"));
ReadChar(myfile,c);
ResponseText(myfile.res,sp);
myAssert(myfile.res=done,ADR(ioErrReadingMsg),sp);
myAssert(c=lf,ADR(errReadingMsg),ADR("LC IS LONGER THAN ONE CHAR"));
WriteLn; WriteLn;
WriteString("Crossword Puzzle Creator: Language selection.");
WriteLn; WriteLn;
FOR l:=1 TO lcount DO
n:=0;
REPEAT
myAssert(n<=lstringlen,ADR("ERROR IN MSG.TXT:"),ADR("STRING TOO LONG"));
ReadChar(myfile,c);
ResponseText(myfile.res,sp);
myAssert(myfile.res=done,ADR(ioErrReadingMsg),sp);
a[n]:=c;
INC(n);
UNTIL (c=lf);
a[n-1]:=nul;
b[l]:=a;
WriteInt(l,1); WriteString(": ");
WriteString(b[l]);
WriteLn;
END;
WriteLn;
REPEAT
WriteString("Please select (press return when finished): ");
InOut.Read(c);
langno:=ORD(c)-ORD("0");
UNTIL ((langno>=1) AND (langno<=lcount));
WriteLn; WriteLn;
WriteString("Reading program messages... please wait!");
WriteLn;
WriteString("LANGUAGE="); WriteString(b[langno]);
WriteLn;
k:=1;
REPEAT
FOR l:=1 TO lcount DO
readline(myfile,a,num,m);
IF (l=langno) THEN
msg[k]:=a;
IF ((k#Value(num)) AND (Value(num)#999)) THEN
IF (Value(num)>=0) THEN
WriteString("illegal message number encountered in msgtxt.data...");
WriteLn;
WriteString("message number should be..... "); WriteInt(k,0);
WriteLn;
WriteString("message number is actually... "); WriteInt(Value(num),0);
WriteLn;
WriteString("string read: '"); WriteString(a); WriteString("'");
WriteLn;
myAssert(FALSE,ADR(errReadingMsg),
ADR("ILLEGAL MESSAGE NUMBER"));
ELSE
WriteString("message number missing in msgtxt.data...");
WriteLn;
WriteString("message number should be "); WriteInt(k,0);
WriteLn;
WriteString("string read: '"); WriteString(a); WriteString("'");
WriteLn;
myAssert(FALSE,ADR(errReadingMsg),
ADR("MESSAGE NUMBER MISSING"));
END;
END;
END;
END;
INC(k);
myAssert(k<=maxmsg,ADR(errReadingMsg),
ADR("TOO MANY MESSAGES"));
UNTIL (Value(num)=999);
WriteLn; WriteLn;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END ReadMsg;
PROCEDURE HandleIOErr(action: IOMode; body: ARRAY OF CHAR): BOOLEAN;
VAR
header: lstring;
t: ARRAY[0..1] OF Text;
result: BOOLEAN;
BEGIN
CASE action OF
|readW: Copy(t[0],msg[5],0,1000);
|readC: Copy(t[0],msg[6],0,1000);
|writeC: Copy(t[0],msg[7],0,1000);
|printC: Copy(t[0],msg[8],0,1000);
|printS: Copy(t[0],msg[9],0,1000);
|ELSE myAssert(FALSE,ADR(msg[10]),ADR(msg[11]));
END;
Copy(t[1],body,0,1000);
SetReqBorderPen(1);
SetReqTextPen(0);
NormalPointer;
result:=BooleanRequest(window,50,200,540,50,t,ADR(msg[12]),ADR(msg[13]),
nul,nul,ReqFlagSet{reqBorder,reqShadow});
SleepPointer;
RETURN result;
END HandleIOErr;
PROCEDURE ReadWords(forcewords: BOOLEAN): INTEGER;
VAR alldone,worddone,again,quit: BOOLEAN;
num,cnt,length: INTEGER;
c: CHAR;
s: string;
body: lstring;
name: FileString;
BEGIN
LOOP
Copy(wordReq.h,msg[69],0,1000);
REPEAT
NormalPointer;
FileReq(wordReq,name);
SleepPointer;
quit:=FALSE;
IF (forcewords) AND (Length(name)=0) THEN
NormalPointer;
quit:=YesOrNo(msg[79]);
SleepPointer;
END;
UNTIL (NOT forcewords) OR (Length(name)>0) OR quit;
num:=0;
IF quit THEN
Terminate(CurrentLevel());
END;
IF Length(name)=0 THEN EXIT END;
LOOP
alldone:=FALSE;
body:=msg[16];
Lookup(myfile,name,5000,FALSE);
filepresent:=TRUE;
IF (myfile.res#done) THEN EXIT END;
WHILE (NOT alldone) AND (NOT myfile.eof) DO
worddone:=FALSE;
cnt:=0;
body:=msg[17];
WHILE (NOT worddone) AND (NOT myfile.eof) DO
ReadChar(myfile,c);
IF (myfile.res#done) THEN
num:=0;
EXIT;
END;
IF (c>="a") AND (c<="z") THEN
c:=CHAR(INTEGER(c)-INTEGER("a")+INTEGER("A"));
END;
IF ( ((c<"A") OR (c>"Z"))
AND (c#lf) AND (c#"*") ) THEN
body:=msg[18];
num:=0;
EXIT;
END;
worddone:=(c=lf);
IF (NOT worddone) THEN
s[cnt]:=c;
INC(cnt);
IF (cnt>stringlen) THEN
body:=msg[19];
num:=0;
EXIT;
END;
END;
END;
IF (myfile.eof) AND (NOT worddone) THEN
num:=0;
body:=msg[20];
EXIT;
END;
s[cnt]:=nul;
IF (Compare(s,0,Length(s),"***END***",FALSE)#0) THEN
IF (num>maxwords) THEN
num:=0;
body:=msg[21];
EXIT;
END;
words[num]:=s;
INC(num);
ELSE
DEC(num);
alldone:=TRUE;
END;
IF (myfile.eof) AND (NOT alldone) THEN
num:=0;
body:=msg[22];
EXIT;
END;
END;
EXIT;
END;
IF (myfile.res#done) OR (num=0) THEN
again:=forcewords OR HandleIOErr(readW,body);
ELSE
again:=FALSE;
END;
IF (NOT again) THEN
EXIT;
END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
RETURN num;
END ReadWords;
PROCEDURE SaveData();
VAR a: FileString;
s: string;
act: LONGINT;
x,y,l,length: INTEGER;
err,again: BOOLEAN;
body: lstring;
BEGIN
LOOP
Copy(dataReq.h,msg[70],0,1000);
NormalPointer;
FileReq(dataReq,a);
SleepPointer;
IF Length(a)=0 THEN EXIT END;
LOOP
body:=msg[16];
Lookup(myfile,a,5000,TRUE);
filepresent:=TRUE;
IF (myfile.res#done) THEN EXIT END;
body:=msg[25];
WriteBytes(myfile,ADR("cpcdata!"),8,act);
IF (myfile.res#done) THEN EXIT END;
ValToStr(hori,FALSE,s,10,4," ",err);
WriteBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
ValToStr(vert,FALSE,s,10,4," ",err);
WriteBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
ValToStr(xmax,FALSE,s,10,4," ",err);
WriteBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
ValToStr(ymax,FALSE,s,10,4," ",err);
WriteBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
FOR y:=1 TO ymax DO
FOR x:=1 TO xmax DO
WriteChar(myfile,text[x,y]);
IF (myfile.res#done) THEN EXIT END;
END;
END;
FOR l:=0 TO hori+vert-1 DO
WriteBytes(myfile,ADR(kwr[l]),Length(kwr[l]),act);
IF (myfile.res#done) THEN EXIT END;
WriteChar(myfile,lf);
IF (myfile.res#done) THEN EXIT END;
END;
EXIT;
END;
IF (myfile.res#done) THEN
again:=HandleIOErr(writeC,body);
ELSE
again:=FALSE;
END;
IF (NOT again) THEN EXIT END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END SaveData;
PROCEDURE LoadData();
VAR a: FileString;
s: string;
act: LONGINT;
x,y,l,cnt,length: INTEGER;
err,again: BOOLEAN;
val: LONGINT;
body: lstring;
BEGIN
LOOP
Copy(dataReq.h,msg[71],0,1000);
NormalPointer;
FileReq(dataReq,a);
SleepPointer;
IF Length(a)=0 THEN EXIT END;
LOOP
body:=msg[16];
Lookup(myfile,a,5000,FALSE);
filepresent:=TRUE;
IF (myfile.res#done) THEN EXIT END;
body:=msg[34];
ReadBytes(myfile,ADR(s),8,act);
IF (myfile.res#done) THEN EXIT END;
IF Compare(s,0,8,"cpcdata!",TRUE)#0 THEN
body:=msg[35];
myfile.res:=notdone;
EXIT;
ELSE
s[4]:=nul;
ReadBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
hori:=Value(s);
ReadBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
vert:=Value(s);
ReadBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
xmax:=Value(s);
ReadBytes(myfile,ADR(s),4,act);
IF (myfile.res#done) THEN EXIT END;
ymax:=Value(s);
FOR x:=0 TO maxgrid DO
FOR y:=0 TO maxgrid DO
text[x,y]:=nul;
END;
END;
FOR y:=1 TO ymax DO
FOR x:=1 TO xmax DO
ReadChar(myfile,text[x,y]);
IF (myfile.res#done) THEN EXIT END;
END;
END;
FOR l:=0 TO hori+vert-1 DO
cnt:=-1;
REPEAT
INC(cnt);
ReadChar(myfile,s[cnt]);
IF (myfile.res#done) THEN EXIT END;
UNTIL (s[cnt]=lf);
s[cnt]:=nul;
kwr[l]:=s;
END;
END;
EXIT;
END;
IF (myfile.res#done) THEN
again:=HandleIOErr(readC,body);
ELSE
again:=FALSE;
END;
IF (NOT again) THEN EXIT END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END;
IF filepresent THEN
filepresent:=FALSE;
Close(myfile);
END;
END LoadData;
PROCEDURE PrintSolution;
VAR s: string;
a,b: INTEGER;
body: lstring;
err,again: BOOLEAN;
BEGIN
LOOP
LOOP
err:=TRUE;
body:=msg[36];
printer:=Open(ADR("PRT:"),newFile);
IF (printer=NIL) THEN
body:=msg[37];
EXIT;
END;
MakeComStr(s,esc,"c",nul,nul,nul,nul,nul,nul);
IF PutPRT(ADR(s),3) THEN EXIT END;
MakeComStr(s,esc,"[","0","z",nul,nul,nul,nul);
IF PutPRT(ADR(s),5) THEN EXIT END;
FOR a:=1 TO ymax DO
FOR b:=1 TO xmax DO
IF (text[b,a]=nul) THEN
s[0]:=" ";
IF PutPRT(ADR(s),1) THEN EXIT END;
ELSE
IF PutPRT(ADR(text[b,a]),1) THEN EXIT END;
END;
END;
s[0]:=lf;
IF PutPRT(ADR(s),1) THEN EXIT END;
END;
MakeComStr(s,esc,"c",nul,nul,nul,nul,nul,nul);
IF PutPRT(ADR(s),Length(s)) THEN EXIT END;
err:=FALSE;
EXIT;
END;
IF (err) THEN
again:=HandleIOErr(printS,body);
ELSE
again:=FALSE;
END;
IF (NOT again) THEN EXIT END;
Dos.Close(printer);
printer:=NIL;
END;
Dos.Close(printer);
printer:=NIL;
END PrintSolution;
PROCEDURE PrintCross;
VAR full: ARRAY [0..10] OF string;
mt: ARRAY [1..2] OF string;
graphon,graphoff,temp: string;
out: ARRAY [0..100] OF CHAR;
POW2: ARRAY[0..7] OF INTEGER;
m,n,ou,x,y,width: INTEGER;
s: CHAR;
act: LONGINT;
sorted,err,again: BOOLEAN;
pos,dotno: INTEGER;
body: lstring;
l1,l2: INTEGER;
lastlen: INTEGER;
converr: BOOLEAN;
BEGIN
LOOP
LOOP
err:=TRUE;
body:=msg[38];
parallel:=Open(ADR("PAR:"),newFile);
IF (parallel=NIL) THEN
body:=msg[39];
EXIT;
END;
width:=640 DIV xmax;
IF (width>16) THEN
width:=16;
END;
POW2[0]:=1; POW2[1]:=2; POW2[2]:=4; POW2[3]:=8;
POW2[4]:=16; POW2[5]:=32; POW2[6]:=64; POW2[7]:=128;
FOR m:=0 TO 10 DO
FOR n:=1 TO width-1 DO
dotno:=RND(8);
temp[n]:=CHAR(POW2[dotno]);
END;
temp[width]:=nul;
full[m]:=temp;
END;
mt[1,0]:=CHAR(255);
mt[2,0]:=CHAR(255);
FOR n:=1 TO width-2 DO
mt[1,n]:=CHAR(128);
mt[2,n]:=CHAR(1);
END;
mt[1,width-1]:=CHAR(255);
mt[2,width-1]:=CHAR(255);
MakeComStr(graphon,esc,"A",CHAR(8),esc,"*",CHAR(4),
CHAR((xmax*width) MOD 256),CHAR((xmax*width) DIV 256));
MakeComStr(graphoff,esc,"A",lf,nul,nul,nul,nul,nul);
FOR y:=1 TO ymax DO
FOR ou:=1 TO 2 DO
IF PutPAR(ADR(graphon),Length(graphon)) THEN EXIT END;
FOR x:=1 TO xmax DO
IF (text[x,y]=nul) THEN
n:=RND(11);
IF PutPAR(ADR(full[n]),width) THEN EXIT END;
ELSE
IF PutPAR(ADR(mt[ou]),width) THEN EXIT END;
END;
END;
s:=lf;
IF PutPAR(ADR(s),1) THEN EXIT END;
END;
END;
IF PutPAR(ADR(graphoff),Length(graphoff)) THEN EXIT END;
REPEAT
sorted:=TRUE;
FOR n:=1 TO hori+vert-1 DO
l1:=Length(kwr[n-1]);
l2:=Length(kwr[n]);
IF (l1<l2) OR ((l1=l2) AND (Compare(kwr[n-1],0,l1,kwr[n],FALSE)<0)) THEN
temp:=kwr[n-1];
kwr[n-1]:=kwr[n];
kwr[n]:=temp;
sorted:=FALSE;
END;
END;
UNTIL sorted;
out[0]:=lf;
IF PutPAR(ADR(out),1) THEN EXIT END;
n:=-1;
lastlen:=10000;
REPEAT
INC(n);
IF Length(kwr[n])#lastlen THEN
lastlen:=Length(kwr[n]);
ValToStr(Length(kwr[n]),FALSE,out,10,2," ",converr);
out[2]:=":";
out[3]:=" ";
out[4]:=nul;
ELSE
out:=" ";
END;
AppendStr(out,kwr[n]);
AppendStr(out," ");
REPEAT
INC(n);
IF Length(kwr[n])=lastlen THEN
AppendStr(out,kwr[n]);
AppendStr(out," ");
END;
UNTIL (n>=hori+vert-1) OR (Length(out)+Length(kwr[n+1])+2>=78)
OR (Length(kwr[n])#lastlen);
IF (n>=hori+vert-1) THEN
REPEAT
pos:=Length(out);
IF (out[pos-1]=" ") THEN
out[pos-1]:=nul;
END;
UNTIL (out[pos-1]#nul);
END;
IF Length(kwr[n])#lastlen THEN
DEC(n);
END;
IF PutPAR(ADR(out),Length(out)) THEN EXIT END;
out[0]:=lf;
IF PutPAR(ADR(out),1) THEN EXIT END;
UNTIL (n>=hori+vert-1);
err:=FALSE;
EXIT;
END;
IF (err) THEN
again:=HandleIOErr(printC,body);
ELSE
again:=FALSE;
END;
IF (NOT again) THEN EXIT END;
Dos.Close(parallel);
parallel:=NIL;
END;
Dos.Close(parallel);
parallel:=NIL;
END PrintCross;
BEGIN
TermProcedure(CoolDown);
printer:=NIL;
parallel:=NIL;
filepresent:=FALSE;
END CPCDosIO.