home *** CD-ROM | disk | FTP | other *** search
- (*************************************************************************
-
- $RCSfile: StripCodes.mod $
- Description: A utility to strip control codes from text files
-
- Created by: fjc (Frank Copeland)
- $Revision: 1.7 $
- $Author: fjc $
- $Date: 1995/01/26 01:05:15 $
-
- Copyright © 1994-1995, Frank Copeland.
- This file is part of Oberon-A.
- See Oberon-A.doc for conditions of use and distribution.
-
- StripCodes is a program that strips most control characters from a
- file, leaving only the printable characters and tabs, line breaks and
- form feeds. It is intended to be used to convert wordprocessor files
- into plain text files for import into other programs that do not
- understand the original format.
-
- *************************************************************************)
-
- <*STANDARD-*> (* allow non-portable code *)
-
- MODULE StripCodes;
-
- IMPORT
- SYS := SYSTEM, Kernel, Exec, Dos, DosUtil, Args, Errors, Files,
- IO := StdIO, Str := Strings;
-
-
- (*
- The theoretical maximum size of an AmigaDOS file specification is 255
- characters.
- *)
- CONST
-
- PathLen = 255;
-
- TYPE
-
- Path = ARRAY PathLen + 1 OF CHAR;
-
- VAR
-
- pattern, (* The pattern to be searched for. *)
- dest (* The destination directory. *)
- : Path;
-
- (*
- These variables are global so that they may be found by the Cleanup()
- procedure in the event of an abnormal exit
- *)
-
- input, (* The current input file. *)
- output (* The current output file. *)
- : Files.File;
-
- PROCEDURE GetArgs ();
-
- (*------------------------------------*)
- PROCEDURE Greetings ();
-
- BEGIN (* Greetings *)
- IO.WriteStr ("StripCodes\n");
- IO.WriteStr ("Written by Frank Copeland\n\n");
- END Greetings;
-
- (*------------------------------------*)
- PROCEDURE Usage ();
-
- BEGIN (* Usage *)
- IO.WriteStr ("format : StripCodes <pattern> <destination>\n");
- IO.WriteStr ("template: PATTERN/A, DESTINATION/A\n\n");
- IO.WriteStr ("<pattern> : files to be converted\n");
- IO.WriteStr ("<destination>: destination directory\n\n");
- END Usage;
-
- BEGIN (* GetArgs *)
- (* Make sure we have been run from the CLI. *)
- Errors.Assert (Args.IsCLI, "Sorry, no Workbench support :-(");
-
- (* Say hello. *)
- Greetings ();
-
- (* Check the number of arguments. *)
- IF (Args.argc # 2) & (Args.argc # 3) THEN
- Usage (); HALT (Dos.warn)
- END;
-
- (* Just copy the pattern. *)
- COPY (Args.argv [1]^, pattern);
-
- IF Args.argc = 3 THEN
- (* The destination needs to be checked. *)
- COPY (Args.argv [2]^, dest);
- IF (DosUtil.ObjectExists (dest) < DosUtil.dir) THEN
- IO.WriteStr ("Destination directory doesn't exist\n");
- HALT (Dos.error)
- END
- ELSE
- dest := ""
- END
- END GetArgs;
-
- PROCEDURE MakeOutputName
- (inputName : ARRAY OF CHAR; VAR outputName : ARRAY OF CHAR);
-
- VAR filePart : Exec.LSTRPTR; i : INTEGER;
-
- BEGIN (* MakeOutputName *)
- IF dest [0] # 0X THEN
- filePart := Dos.FilePart (inputName);
- COPY (dest, outputName);
- IF ~Dos.AddPart (outputName, filePart^, PathLen) THEN
- IO.WriteStr ("Output file name too big\n");
- HALT (Dos.error)
- END;
- ELSE
- COPY (inputName, outputName)
- END; (* ELSE *)
- i := SHORT (Str.Length (outputName)) - 1;
- WHILE (i >= 0) & (outputName [i] # ".") DO DEC (i) END;
- IF i > (PathLen - 4) THEN
- IO.WriteStr ("Output file name too big\n");
- HALT (Dos.error)
- ELSE
- IF i >= 0 THEN outputName [i] := 0X END;
- Str.Append (".WPS", outputName);
- END; (* ELSE *)
- END MakeOutputName;
-
- PROCEDURE Strip (inputName : ARRAY OF CHAR);
-
- CONST
- CR = 0DX; LF = 0AX; TAB = 09X; FF = 0CX;
-
- VAR outputName : Path; r, w : Files.Rider; ch : CHAR;
-
- <*$CopyArrays-*>
- BEGIN (* Strip *)
- (* IO.WriteF1 ("inputName= %s\n", SYS.ADR (inputName)); *)
- input := Files.Old (inputName);
- IF input # NIL THEN
- Files.Set (r, input, 0);
- MakeOutputName (inputName, outputName);
- (* IO.WriteF1 ("outputName= %s\n", SYS.ADR (outputName)); *)
- output := Files.New (outputName);
- IF output # NIL THEN
- IO.WriteF2
- (" !! %s -> %s\n", SYS.ADR (inputName), SYS.ADR (outputName));
- Files.Set (w, output, 0);
- WHILE ~r.eof DO
- Files.Read (r, ch);
- IF
- ((ch >= " ") & (ch <= "~")) OR
- (ch = CR) OR (ch = LF) OR (ch = TAB) OR (ch = FF)
- THEN
- Files.Write (w, ch)
- END;
- END; (* WHILE *)
- Files.Register (output); SYS.DISPOSE (output)
- ELSE
- IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (outputName))
- END; (* ELSE *)
- Files.Close (input); SYS.DISPOSE (input)
- ELSE
- IO.WriteF1 (" !! Could not open %s\n", SYS.ADR (inputName))
- END; (* ELSE *)
- END Strip;
-
- PROCEDURE Scan ();
-
- VAR myAnchor : Dos.AnchorPath; result : LONGINT;
-
- BEGIN (* Scan *)
- (*
- Allocate an anchor structure and initialise with the length of the
- path variable.
- *)
- myAnchor.strlen := 256;
-
- (* Find the first file matching the pattern *)
- result := Dos.MatchFirst (pattern, myAnchor);
- WHILE result = 0 DO
- (* Strip the file and get the next name. *)
- Strip (myAnchor.buf);
- result := Dos.MatchNext (myAnchor)
- END; (* WHILE *)
- Dos.MatchEnd (myAnchor); (* Clean up anchor data *)
- END Scan;
-
- PROCEDURE * Cleanup (VAR rc : LONGINT);
-
- BEGIN (* Cleanup *)
- IF input # NIL THEN Files.Close (input) END;
- IF output # NIL THEN Files.Purge (output) END;
- END Cleanup;
-
- BEGIN (* StripCodes *)
- input := NIL; output := NIL;
- Kernel.SetCleanup (Cleanup);
- GetArgs ();
- Scan ()
- END StripCodes.
-