home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / obero / oberon-a / source / misc / stripcodes.mod < prev    next >
Encoding:
Text File  |  1995-07-02  |  5.4 KB  |  206 lines

  1. (*************************************************************************
  2.  
  3.      $RCSfile: StripCodes.mod $
  4.   Description: A utility to strip control codes from text files
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 1.7 $
  8.       $Author: fjc $
  9.         $Date: 1995/01/26 01:05:15 $
  10.  
  11.   Copyright © 1994-1995, Frank Copeland.
  12.   This file is part of Oberon-A.
  13.   See Oberon-A.doc for conditions of use and distribution.
  14.  
  15.   StripCodes is a program that strips most control characters from a
  16.   file, leaving only the printable characters and tabs, line breaks and
  17.   form feeds.  It is intended to be used to convert wordprocessor files
  18.   into plain text files for import into other programs that do not
  19.   understand the original format.
  20.  
  21. *************************************************************************)
  22.  
  23. <*STANDARD-*> (* allow non-portable code *)
  24.  
  25. MODULE StripCodes;
  26.  
  27. IMPORT
  28.   SYS := SYSTEM, Kernel, Exec, Dos, DosUtil, Args, Errors, Files,
  29.   IO := StdIO, Str := Strings;
  30.  
  31.  
  32. (*
  33.   The theoretical maximum size of an AmigaDOS file specification is 255
  34.   characters.
  35. *)
  36. CONST
  37.  
  38.   PathLen = 255;
  39.  
  40. TYPE
  41.  
  42.   Path = ARRAY PathLen + 1 OF CHAR;
  43.  
  44. VAR
  45.  
  46.   pattern,    (* The pattern to be searched for. *)
  47.   dest        (* The destination directory. *)
  48.     : Path;
  49.  
  50.   (*
  51.     These variables are global so that they may be found by the Cleanup()
  52.     procedure in the event of an abnormal exit
  53.   *)
  54.  
  55.   input,      (* The current input file. *)
  56.   output      (* The current output file. *)
  57.     : Files.File;
  58.  
  59. PROCEDURE GetArgs ();
  60.  
  61.   (*------------------------------------*)
  62.   PROCEDURE Greetings ();
  63.  
  64.   BEGIN (* Greetings *)
  65.     IO.WriteStr ("StripCodes\n");
  66.     IO.WriteStr ("Written by Frank Copeland\n\n");
  67.   END Greetings;
  68.  
  69.   (*------------------------------------*)
  70.   PROCEDURE Usage ();
  71.  
  72.   BEGIN (* Usage *)
  73.     IO.WriteStr ("format  : StripCodes <pattern> <destination>\n");
  74.     IO.WriteStr ("template: PATTERN/A, DESTINATION/A\n\n");
  75.     IO.WriteStr ("<pattern>    : files to be converted\n");
  76.     IO.WriteStr ("<destination>: destination directory\n\n");
  77.   END Usage;
  78.  
  79. BEGIN (* GetArgs *)
  80.   (* Make sure we have been run from the CLI. *)
  81.   Errors.Assert (Args.IsCLI, "Sorry, no Workbench support :-(");
  82.  
  83.   (* Say hello. *)
  84.   Greetings ();
  85.  
  86.   (* Check the number of arguments. *)
  87.   IF (Args.argc # 2) & (Args.argc # 3) THEN
  88.     Usage (); HALT (Dos.warn)
  89.   END;
  90.  
  91.   (* Just copy the pattern. *)
  92.   COPY (Args.argv [1]^, pattern);
  93.  
  94.   IF Args.argc = 3 THEN
  95.     (* The destination needs to be checked. *)
  96.     COPY (Args.argv [2]^, dest);
  97.     IF (DosUtil.ObjectExists (dest) < DosUtil.dir) THEN
  98.       IO.WriteStr ("Destination directory doesn't exist\n");
  99.       HALT (Dos.error)
  100.     END
  101.   ELSE
  102.     dest := ""
  103.   END
  104. END GetArgs;
  105.  
  106. PROCEDURE MakeOutputName
  107.   (inputName : ARRAY OF CHAR; VAR outputName : ARRAY OF CHAR);
  108.  
  109.   VAR filePart : Exec.LSTRPTR; i : INTEGER;
  110.  
  111. BEGIN (* MakeOutputName *)
  112.   IF dest [0] # 0X THEN
  113.     filePart := Dos.FilePart (inputName);
  114.     COPY (dest, outputName);
  115.     IF ~Dos.AddPart (outputName, filePart^, PathLen) THEN
  116.       IO.WriteStr ("Output file name too big\n");
  117.       HALT (Dos.error)
  118.     END;
  119.   ELSE
  120.     COPY (inputName, outputName)
  121.   END; (* ELSE *)
  122.   i := SHORT (Str.Length (outputName)) - 1;
  123.   WHILE (i >= 0) & (outputName [i] # ".") DO DEC (i) END;
  124.   IF i > (PathLen - 4) THEN
  125.     IO.WriteStr ("Output file name too big\n");
  126.     HALT (Dos.error)
  127.   ELSE
  128.     IF i >= 0 THEN outputName [i] := 0X END;
  129.     Str.Append (".WPS", outputName);
  130.   END; (* ELSE *)
  131. END MakeOutputName;
  132.  
  133. PROCEDURE Strip (inputName : ARRAY OF CHAR);
  134.  
  135.   CONST
  136.     CR = 0DX; LF = 0AX; TAB = 09X; FF = 0CX;
  137.  
  138.   VAR outputName : Path; r, w : Files.Rider; ch : CHAR;
  139.  
  140. <*$CopyArrays-*>
  141. BEGIN (* Strip *)
  142.   (* IO.WriteF1 ("inputName= %s\n", SYS.ADR (inputName)); *)
  143.   input := Files.Old (inputName);
  144.   IF input # NIL THEN
  145.     Files.Set (r, input, 0);
  146.     MakeOutputName (inputName, outputName);
  147.     (* IO.WriteF1 ("outputName= %s\n", SYS.ADR (outputName)); *)
  148.     output := Files.New (outputName);
  149.     IF output # NIL THEN
  150.       IO.WriteF2
  151.         (" !! %s -> %s\n", SYS.ADR (inputName), SYS.ADR (outputName));
  152.       Files.Set (w, output, 0);
  153.       WHILE ~r.eof DO
  154.         Files.Read (r, ch);
  155.         IF
  156.           ((ch >= " ") & (ch <= "~")) OR
  157.           (ch = CR) OR (ch = LF) OR (ch = TAB) OR (ch = FF)
  158.         THEN
  159.           Files.Write (w, ch)
  160.         END;
  161.       END; (* WHILE *)
  162.       Files.Register (output); SYS.DISPOSE (output)
  163.     ELSE
  164.       IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (outputName))
  165.     END; (* ELSE *)
  166.     Files.Close (input); SYS.DISPOSE (input)
  167.   ELSE
  168.     IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (inputName))
  169.   END; (* ELSE *)
  170. END Strip;
  171.  
  172. PROCEDURE Scan ();
  173.  
  174.   VAR myAnchor : Dos.AnchorPath; result : LONGINT;
  175.  
  176. BEGIN (* Scan *)
  177.   (*
  178.     Allocate an anchor structure and initialise with the length of the
  179.     path variable.
  180.   *)
  181.   myAnchor.strlen := 256;
  182.  
  183.   (* Find the first file matching the pattern *)
  184.   result := Dos.MatchFirst (pattern, myAnchor);
  185.   WHILE result = 0 DO
  186.     (* Strip the file and get the next name. *)
  187.     Strip (myAnchor.buf);
  188.     result := Dos.MatchNext (myAnchor)
  189.   END; (* WHILE *)
  190.   Dos.MatchEnd (myAnchor); (* Clean up anchor data *)
  191. END Scan;
  192.  
  193. PROCEDURE * Cleanup (VAR rc : LONGINT);
  194.  
  195. BEGIN (* Cleanup *)
  196.   IF input # NIL THEN Files.Close (input) END;
  197.   IF output # NIL THEN Files.Purge (output) END;
  198. END Cleanup;
  199.  
  200. BEGIN (* StripCodes *)
  201.   input := NIL; output := NIL;
  202.   Kernel.SetCleanup (Cleanup);
  203.   GetArgs ();
  204.   Scan ()
  205. END StripCodes.
  206.