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