Monday, February 23, 2015

(* This is my second post!

I promised to present a Modula-2 program that properly processes mixed end-of-line characters in text files. Text files contain end-of-line characters and possibly horizontal tab characters but no other control characters.

Instead of simply modifying Crunch0, I decided to post an entirely new program.

CountLinesChars uses RawIO.Read and StreamFile. It disallows redirection from stdin. Instead it counts lines and characters in input text files whose names are specified as command line arguments.

This program is supposed to give a correct count regardless of the combination of end-of-line characters, where eol = cr [ lf ] | lf . Like Crunch0, it writes to stdout. *)

MODULE CountLinesChars;

(* Uses RawIO.Read and StreamFile *)

(* Outputs count of the number of lines and characters in each input text file *)
(* whose name is specified on the command line. *)

(* Outputs count of the total lines and characters in all the input text files. *)

(* input: text files only. *)
(* output: always to stdout. *)
(* stdin: only from a filename argument on the command-line. *)

(* Text files contain end-of-line characters and possibly horizontal tab *)
(* characters but no other control characters. *)

(* 1. input end-of-line characters are counted in the character count (nc) *)
(* input eol = cr [ lf ] | lf . *)
(* so cr lf = 2 in the character count (nc) *)
(* but eol only counts as 1 in the line count (nl) *)

(* 2. Last line of file is not counted in line count if not terminated in eol. *)

(* Tested on: *)
(* ISO m2: p1 compiler version 9.2/Apple Mac OS X 10.7.5 *)
(* Not tested on gm2 or other ISO m2 systems. *)

(* License: This software is released under the following M.I.T. License:

The MIT License (MIT)

Copyright (c) 2015 Carl M. Glassberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *)

  IMPORT StreamFile, RawIO, TextIO, IOResult, StdChans, ProgramArgs, STextIO, SWholeIO;

  CONST
    EOL = 36C; (* CHR(30) = 1EX *)

    cr = 15C;  (* CHR(13) = 0DX, carriage return *)
    lf = 12C;  (* CHR(10) = 0AX, linefeed *)

  VAR
    inf : StreamFile.ChanId;
    ifn : ARRAY [0 .. 79] OF CHAR;
    result : StreamFile.OpenResults;

    n : CARDINAL; (* nr. of input files *)

    nl : CARDINAL; (* line count *)
    nc : CARDINAL; (* character count *)

    ml : CARDINAL; (* total line count *)
    mc : CARDINAL; (* total character count *)

    ch : CHAR;

  PROCEDURE OutputCount(ifn : ARRAY OF CHAR);
  BEGIN
    SWholeIO.WriteCard(nl, 8);
    SWholeIO.WriteCard(nc, 8);
    STextIO.WriteString(" ");
    STextIO.WriteString(ifn);
    STextIO.WriteLn
  END OutputCount;

  PROCEDURE OutputTotal;
  BEGIN
    SWholeIO.WriteCard(ml, 8);
    SWholeIO.WriteCard(mc, 8);
    STextIO.WriteString(" total");
    STextIO.WriteLn
  END OutputTotal;

  PROCEDURE WriteErrMsg (str : ARRAY OF CHAR);
    (* output to stderr *)
  BEGIN
    TextIO.WriteString(StdChans.StdErrChan(), str)
  END WriteErrMsg;

  PROCEDURE WriteErrLn;
    (* output to stderr *)
  BEGIN
    TextIO.WriteLn(StdChans.StdErrChan());
  END WriteErrLn;

  PROCEDURE done () : BOOLEAN;
    (* inspect input from named file; return TRUE if not at end-of-file *)
  BEGIN
    RETURN (IOResult.ReadResult(inf) # IOResult.endOfInput)
  END done;

  PROCEDURE read_char(VAR ch : CHAR);
    (* return nul character CHR(0) at end-of-file; no end-of-line processing *)
  BEGIN
    RawIO.Read(inf, ch);
    IF NOT(done()) THEN
      ch := 0C
    END (* if *)
  END read_char;
         
  PROCEDURE Process;
  BEGIN
    read_char(ch);
    WHILE done() & (ch # 0C) DO
      INC(nc);

      IF (ch = cr) THEN (* end-of-line *)
        INC(nl);
        read_char(ch);
        INC(nc);
        IF (ch = lf) THEN
          read_char(ch)
        END (* if *)

      ELSIF (ch = lf) THEN (* end-of-line *)
        INC(nl);
        read_char(ch)

      ELSE (* not end-of-line *)
        read_char(ch)
      END (* if *);
                       
    END (* while *)
  END Process;

  PROCEDURE Help;
  BEGIN
    WriteErrLn;
    WriteErrMsg('Usage: "CountLinesChars" ifn { ifn } ');
    WriteErrLn;
    WriteErrMsg('   or  "CountLinesChars" ifn { ifn } ">" ofn');
    WriteErrLn
  END Help;

BEGIN
  ProgramArgs.NextArg();
  (* first argument is tool name so skip. *)

  IF ProgramArgs.IsArgPresent() THEN
    (* command-line arguments are named input files. *)
    ml := 0; mc := 0;
    n := 0;
    LOOP
      TextIO.ReadToken(ProgramArgs.ArgChan(), ifn);
      StreamFile.Open(inf, ifn, StreamFile.read + StreamFile.raw, result);
      IF result # StreamFile.opened THEN
        WriteErrLn;
        WriteErrMsg('Could not open input file: ');
        WriteErrMsg(ifn);
        EXIT
      END (* if *);

      nc := 0; nl := 0;
      INC(n);

      Process;
      OutputCount(ifn);

      INC(ml, nl); INC(mc, nc);

      StreamFile.Close(inf);
      ProgramArgs.NextArg();
      IF NOT (ProgramArgs.IsArgPresent()) THEN
        EXIT
      END (* if *)
    END (* loop *);
    IF n > 1 THEN OutputTotal END (* if *)
  ELSE
    Help
  END (* if *)
END CountLinesChars .

No comments:

Post a Comment