home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / cross_464.lzh / Cross / txt / PreProcessor.mod < prev   
Text File  |  1991-03-09  |  6KB  |  258 lines

  1. (***************************************************************************
  2.  :Program.    PreProcessor
  3.  :Author.     Jürgen Weinelt
  4.  :Address.    Zur Kanzel 1, D-8783 Hammelburg, Germany
  5.  :Version.    V1.1
  6.  :Copyright.  Freeware; copy it, but don't sell it!
  7.  :Language.   Modula-II
  8.  :Translator. M2Amiga V3.32d
  9.  :Imports.    FileReq
  10.  :Contents.   PreProcessor is part of the "Cross" (crossword puzzle
  11.  :Contents.   Creator) package.
  12.  :Contents.   PreProcessor takes any amiga ascii text file as input
  13.  :Contents.   and produces a CPC word data file as output.
  14.  :History.    V1.0  08-jan-91  first major release on AMOK
  15.  :History.    V1.1  06-feb-91  minor changes
  16.  **************************************************************************)
  17.  
  18.  
  19.  
  20. MODULE PreProcessor;
  21.  
  22.  
  23.  
  24. FROM Arts
  25.  IMPORT Assert;
  26.  
  27. FROM ASCII
  28.  IMPORT lf,nul;
  29.  
  30. FROM Dos
  31.  IMPORT Delay;
  32.  
  33. FROM FileReq
  34.  IMPORT FileReq,MakeFRD,FileRequestData,FileString;
  35.  
  36. FROM FileSystem
  37.  IMPORT Lookup,Close,ReadChar,WriteBytes,WriteChar,File,Response;
  38.  
  39. FROM Heap
  40.  IMPORT Allocate;
  41.  
  42. FROM InOut
  43.  IMPORT WriteString,WriteLn;
  44.  
  45. FROM Str
  46.  IMPORT Compare,Length;
  47.  
  48. FROM SYSTEM
  49.  IMPORT ADR;
  50.  
  51.  
  52.  
  53. CONST
  54.  wordlen=25;
  55.  
  56.  
  57.  
  58. TYPE
  59.  WordString=ARRAY[0..wordlen] OF CHAR;
  60.  WordNodePtr=POINTER TO WordNode;
  61.  WordNode=
  62.   RECORD
  63.    word: WordString;
  64.    left,right: WordNodePtr;
  65.   END;
  66.  
  67.  
  68.  
  69. VAR
  70.  inf,outf: File;
  71.  inname,outname: FileString;
  72.  wrd: WordString;
  73.  frd: FileRequestData;
  74.  root: WordNodePtr;
  75.  act: LONGINT;
  76.  
  77.  
  78.  
  79. PROCEDURE GetChar(VAR file: File; VAR c,c2: CHAR): BOOLEAN;
  80.  VAR
  81.   legal: BOOLEAN;
  82.  BEGIN
  83.   ReadChar(file,c);
  84.   CASE c OF
  85.    |"a".."z":
  86.          c:=CHAR(INTEGER(c)-INTEGER("a")+INTEGER("A")); (* capitalize *)
  87.          c2:=nul;
  88.          legal:=TRUE;
  89.    |"A".."Z":
  90.          legal:=TRUE;
  91.          c2:=nul;
  92.    |"ä","Ä":
  93.          c:="A"; c2:="E"; legal:=TRUE;
  94.    |"ö","Ö":
  95.          c:="O"; c2:="E"; legal:=TRUE;
  96.    |"ü","Ü":
  97.          c:="U"; c2:="E"; legal:=TRUE;
  98.    |"ß": c:="S"; c2:="S"; legal:=TRUE;
  99.    |ELSE legal:=FALSE;
  100.   END;
  101.   RETURN legal;
  102.  END GetChar;
  103.  
  104.  
  105.  
  106. PROCEDURE GetWord(VAR file: File; VAR wrd: WordString);
  107.  VAR
  108.   c,c2: CHAR;
  109.   inx: INTEGER;
  110.   legal: BOOLEAN;
  111.  BEGIN
  112.   wrd[0]:=nul;
  113.   REPEAT
  114.    legal:=GetChar(file,c,c2);
  115.   UNTIL (legal) OR (file.eof);
  116.  
  117.   IF (NOT file.eof) THEN
  118.    wrd[0]:=c;
  119.    IF c2<>nul THEN
  120.     wrd[1]:=c2;
  121.     inx:=2;
  122.    ELSE
  123.     inx:=1;
  124.    END;
  125.    REPEAT
  126.     legal:=GetChar(file,c,c2);
  127.     IF legal THEN
  128.      wrd[inx]:=c;
  129.      INC(inx);
  130.      IF c2<>nul THEN
  131.       wrd[inx]:=c2;
  132.       INC(inx);
  133.      END;
  134.     END;
  135.    UNTIL (inx>=wordlen) OR (NOT legal) OR (file.eof);
  136.    IF inx>wordlen THEN
  137.     DEC(inx);
  138.    END;
  139.    wrd[inx]:=nul;
  140.   END;
  141.  END GetWord;
  142.  
  143.  
  144.  
  145. PROCEDURE Remember(VAR w: WordString; VAR n: WordNodePtr);
  146.  BEGIN
  147.   (* builds a binary tree to store the words in *)
  148.   IF n=NIL THEN
  149.    Allocate(n,SIZE(WordNode));
  150.    Assert(n#NIL,ADR("ERROR: OUT OF MEMORY"));
  151.    n^.word:=w;
  152.   ELSE
  153.    IF Length(w)<Length(n^.word) THEN
  154.     Remember(w,n^.right);
  155.    END;
  156.    IF Length(w)>Length(n^.word) THEN
  157.     Remember(w,n^.left);
  158.    END;
  159.    IF Length(w)=Length(n^.word) THEN
  160.     CASE Compare(n^.word,w) OF
  161.      |1..wordlen+1:
  162.         Remember(w,n^.left);
  163.      |-wordlen-1..-1:
  164.         Remember(w,n^.right);
  165.      |ELSE (* NOP *)
  166.     END;
  167.    END;
  168.   END;
  169.  END Remember;
  170.  
  171.  
  172.  
  173. PROCEDURE PutWord(n: WordNodePtr; VAR f: File);
  174.  VAR
  175.   act: LONGINT;
  176.  (* traverses the binary tree and outputs the words alphabetically *)
  177.  BEGIN
  178.   IF (n^.left#NIL) THEN
  179.    PutWord(n^.left,f);
  180.   END;
  181.   WriteBytes(f,ADR(n^.word),Length(n^.word),act);
  182.   WriteChar(f,lf);
  183.   IF (n^.right#NIL) THEN
  184.    PutWord(n^.right,f);
  185.   END;
  186.  END PutWord;
  187.  
  188.  
  189.  
  190. BEGIN
  191.  WriteLn; WriteLn;
  192.  WriteString("Crossword Puzzle Creator: Word file PreProcessor");
  193.  WriteLn;
  194.  WriteString("------------------------------------------------");
  195.  WriteLn;
  196.  WriteString("Copyright © 1991 by J. Weinelt");
  197.  WriteLn; WriteLn;
  198.  WriteString("This is FreeWare; non-commercial distribution is encouraged");
  199.  WriteLn;
  200.  WriteString("No liabilities or warranties assumed; use at your own risk!");
  201.  WriteLn; WriteLn;
  202.  WriteString("Purpose:");
  203.  WriteLn;
  204.  WriteString("  Takes any ascii text file as input");
  205.  WriteLn;
  206.  WriteString("  Writes an CPC word data file as output");
  207.  WriteLn; WriteLn;
  208.  WriteString("PreProcessor has no command line parameters; file selection");
  209.  WriteLn;
  210.  WriteString("is completely done with file requesters.");
  211.  WriteLn; WriteLn;
  212.  Delay(100);
  213.  root:=NIL;
  214.  MakeFRD("Open input file","","",NIL,frd);
  215.  FileReq(frd,inname);
  216.  IF Length(inname)>0 THEN
  217.   Lookup(inf,inname,5000,FALSE);
  218.   Assert(inf.res=done,ADR("ERROR: CAN'T OPEN INPUT FILE"));
  219.   WriteString("Reading ");
  220.   WriteString(inname);
  221.   WriteString("... please wait!");
  222.   WriteLn; WriteLn;
  223.   REPEAT
  224.    GetWord(inf,wrd);
  225.    IF NOT (
  226.            (Length(wrd)<2) OR
  227.            (Length(wrd)>wordlen) OR
  228.            (NOT ODD(Length(wrd)))
  229.           )
  230.       OR (Length(wrd)=2)
  231.    THEN
  232.     Remember(wrd,root);
  233.    END;
  234.   UNTIL inf.eof;
  235.   frd.h:="Open output file";
  236.   FileReq(frd,outname);
  237.   IF Length(outname)>0 THEN
  238.    Lookup(outf,outname,5000,TRUE);
  239.    Assert(outf.res=done,ADR("ERROR: CAN'T OPEN OUTPUT FILE"));
  240.    WriteString("Writing ");
  241.    WriteString(outname);
  242.    WriteString("... please wait!");
  243.    WriteLn; WriteLn;
  244.    PutWord(root,outf);
  245.    WriteBytes(outf,ADR("***END***"),9,act);
  246.    WriteChar(outf,lf);
  247.    WriteString("Finished. Please edit output file with an ASCII editor and remove");
  248.    WriteLn;
  249.    WriteString("all unwanted words.");
  250.    WriteLn; WriteLn;
  251.    WriteString("Goodbye.");
  252.    WriteLn; WriteLn;
  253.   END;
  254.  END;
  255.  Close(inf);
  256.  Close(outf);
  257. END PreProcessor.
  258.