home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d537 / cross.lha / Cross / Source / PreProcessor.mod < prev    next >
Text File  |  1991-08-26  |  6KB  |  226 lines

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