home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / obero / oberon-a / source / misc / text2ascii.mod < prev    next >
Encoding:
Text File  |  1994-08-08  |  6.2 KB  |  209 lines

  1. (***************************************************************************
  2.  
  3.      $RCSfile: Text2Ascii.mod $
  4.   Description: A utility to convert Project Oberon text files into plain
  5.                ASCII text.  Formatting information is stripped and CRs are
  6.                converted into LFs
  7.      Requires: AmigaDOS V2.04 or greater
  8.  
  9.    Created by: fjc (Frank Copeland)
  10.     $Revision: 1.4 $
  11.       $Author: fjc $
  12.         $Date: 1994/06/17 17:35:24 $
  13.  
  14.   Copyright © 1994, Frank Copeland.
  15.   This file is part of Oberon-A.
  16.   See Oberon-A.doc for conditions of use and distribution.
  17.  
  18.   Log entries are at the end of the file.
  19.  
  20. ***************************************************************************)
  21.  
  22. MODULE Text2Ascii;
  23.  
  24. (*
  25.   The example source code and documentation I downloaded from
  26.   neptune.inf.ethz.ch was in Project Oberon's Text format which is
  27.   unreadable by my Amiga text viewers and editors.  The main problem is
  28.   that lines are terminated by CR (0DX) instead of LF (0AX).  The
  29.   formatting information is also a pain.
  30.  
  31.   Text2Ascii reads one or more files in Text format and outputs the
  32.   contents as pure ASCII text, converting CRs to LFs in the process.
  33.   Formatting information is ignored.  Lines longer than 75 characters
  34.   are broken at the first space after the 75th character.  Tabs are
  35.   replaced with two spaces.
  36.  
  37.   The syntax of a Text file, as far as we are concerned, is:
  38.  
  39.     TextBlockId:2 off:4 {byte} len:4 {char}
  40.     TextBlockId = 01F0H or F001H
  41.  
  42.   off is the offset of the start of the text proper from the location of
  43.   TextBlockId.  len is the length in characters of the text and is at
  44.   Position(TextBlockId) + off - 4.
  45.  
  46.   off and len are stored in little-endian form: that is, the low-order
  47.   word and low-order byte first.  This must be converted to the internal
  48.   representation used by the MC68k processors.
  49. *)
  50.  
  51. (* $P- allow non-portable code *)
  52.  
  53. IMPORT
  54.   Args, Dos, Files, IO := StdIO, Exec, SYS := SYSTEM;
  55.  
  56. VAR
  57.   pattern, dest : ARRAY 256 OF CHAR;
  58.  
  59. (*------------------------------------*)
  60. PROCEDURE GetArgs ();
  61.  
  62. BEGIN (* GetArgs *)
  63.   IF Args.argc = 3 THEN
  64.     COPY (Args.argv [1]^, pattern);
  65.     COPY (Args.argv [2]^, dest);
  66.   ELSE
  67.     IO.WriteStr ("Usage : Text2Ascii <pattern> <dest>\n");
  68.     HALT (Dos.returnWarn)
  69.   END; (* ELSE *)
  70. END GetArgs;
  71.  
  72. (*------------------------------------*)
  73. PROCEDURE ConvertFile (fileName : ARRAY OF CHAR);
  74.  
  75.   CONST
  76.     TextBlockId1 = 01F0H;
  77.     TextBlockId2 = -0FFFH;
  78.     CR = 0DX; LF = 0AX; TAB = 09X;
  79.  
  80.   VAR
  81.     newFileName : ARRAY 256 OF CHAR;
  82.     oldFile, newFile : Files.File;
  83.     r, w : Files.Rider;
  84.     id, col : INTEGER;
  85.     off, len, i : LONGINT;
  86.     ch : CHAR; ignore : BOOLEAN;
  87.     filePart : Exec.STRPTR;
  88.  
  89.   (*------------------------------------*)
  90.   PROCEDURE ReadLongint (VAR l : LONGINT);
  91.  
  92.     VAR byte : ARRAY 4 OF SYS.BYTE; msb : INTEGER;
  93.  
  94.   BEGIN (* ReadLongint *)
  95.     Files.ReadBytes (r, byte, 4);
  96.     msb := ORD (byte [3]); IF msb > 127 THEN DEC (msb, 256) END;
  97.     l :=
  98.       (msb * 1000000H) + (ORD (byte [2]) * 10000H)
  99.       + (LONG (ORD (byte [1])) * 100H) + LONG (ORD (byte [0]))
  100.   END ReadLongint;
  101.  
  102. (*$D-*)
  103. BEGIN (* ConvertFile *)
  104.   oldFile := Files.Old (fileName);
  105.   IF oldFile # NIL THEN
  106.     Files.Set (r, oldFile, 0);
  107.     Files.ReadBytes (r, id, 2);
  108.     IF (id = TextBlockId1) OR (id = TextBlockId2) THEN
  109.       COPY (dest, newFileName);
  110.       filePart := Dos.base.FilePart (fileName);
  111.       ignore := Dos.base.AddPart (newFileName, filePart^, 256);
  112.       newFile := Files.New (newFileName);
  113.       IF newFile # NIL THEN
  114.         IO.WriteF2
  115.           (" !! %s -> %s\n", SYS.ADR (fileName), SYS.ADR (newFileName));
  116.         Files.Set (w, newFile, 0);
  117.         ReadLongint (off);
  118.         Files.Set (r, oldFile, off - 4);
  119.         ReadLongint (len); i := Files.Length (oldFile) - off;
  120.         IF len > i THEN len := i END;
  121.         i := 0; col := 1;
  122.         WHILE i < len DO
  123.           Files.Read (r, ch);
  124.           IF ch = CR THEN
  125.             Files.Write (w, LF); col := 1
  126.           ELSIF ch = TAB THEN
  127.             Files.Write (w, " "); Files.Write (w, " ");
  128.             INC (col, 2)
  129.           ELSIF (ch = " ") & (col > 75) THEN
  130.             Files.Write (w, LF); col := 1
  131.           ELSIF (ch >= 80X) & (ch <= 85X) THEN
  132.             CASE ch OF
  133.               80X : Files.Write (w, "Ä") |
  134.               81X : Files.Write (w, "Ö") |
  135.               82X : Files.Write (w, "Ü") |
  136.               83X : Files.Write (w, "ä") |
  137.               84X : Files.Write (w, "ö") |
  138.               85X : Files.Write (w, "ü") |
  139.             END; (* CASE ch *)
  140.             INC (col)
  141.           ELSIF ch >= " " THEN
  142.             Files.Write (w, ch); INC (col)
  143.           END;
  144.           INC (i)
  145.         END; (* WHILE *)
  146.         Files.Register (newFile)
  147.       ELSE
  148.         IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (newFileName))
  149.       END; (* ELSE *)
  150.     ELSE
  151.       IO.WriteF1 (" !! %s is not a Text file\n", SYS.ADR (fileName))
  152.     END; (* ELSE *)
  153.     Files.Close (oldFile)
  154.   ELSE
  155.     IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (fileName))
  156.   END; (* ELSE *)
  157. END ConvertFile;
  158.  
  159. (*------------------------------------*)
  160. PROCEDURE ConvertFiles ();
  161.  
  162.   VAR
  163.     myAnchor : Dos.AnchorPath;
  164.     result : LONGINT;
  165.  
  166. BEGIN (* ConvertFiles *)
  167.   myAnchor.strlen := 256;
  168.   result := Dos.base.MatchFirst (pattern, myAnchor);
  169.   WHILE result = 0 DO
  170.     ConvertFile (myAnchor.buf);
  171.     result := Dos.base.MatchNext (myAnchor)
  172.   END; (* WHILE *)
  173. END ConvertFiles;
  174.  
  175. BEGIN (* Text2Ascii *)
  176.   IO.WriteStr ("Text2Ascii\nWritten by Frank Copeland\n");
  177.   IF Dos.base.version < 37 THEN
  178.     IO.WriteStr ("Requires AmigaDOS 2.04+\nSorry :-(\n");
  179.   ELSE
  180.     GetArgs ();
  181.     ConvertFiles ()
  182.   END; (* ELSE *)
  183. END Text2Ascii.
  184.  
  185. (***************************************************************************
  186.  
  187.   $Log: Text2Ascii.mod $
  188.   Revision 1.4  1994/06/17  17:35:24  fjc
  189.   - Updated for release
  190.  
  191.   Revision 1.3  1994/06/09  14:30:29  fjc
  192.   - Incorporates changes to Amiga interface
  193.  
  194.   Revision 1.2  1994/06/05  22:23:09  fjc
  195.   - Changed to use new Amiga interface
  196.  
  197.   Revision 1.1  1994/05/12  20:20:07  fjc
  198.   - Prepared for release
  199.  
  200.   Revision 1.0  1994/01/16  14:02:35  fjc
  201.   Start of version control
  202.  
  203.  
  204.   0.2  (30-Nov-93)  Added conversions for German chars
  205.   0.1  (02-Nov-93)  Initial version
  206.  
  207. ***************************************************************************)
  208.  
  209.