Filters in M2 
Author Message
 Filters in M2

Hi Modulants,
    I'd like to make some simple filters in Modula2 (with CHAR stdinput &
stdout). My first trials were unsatisfied. Could anyone help me on this?
I prefer TopSpeed modules (if it doesn't make unconvenience).

   Thanks in advance



Sun, 04 Sep 1994 17:10:35 GMT  
 Filters in M2

Quote:
>Hi Modulants,
>    I'd like to make some simple filters in Modula2 (with CHAR stdinput &
>stdout). My first trials were unsatisfied. Could anyone help me on this?
>I prefer TopSpeed modules (if it doesn't make unconvenience).

ok, here's a little ditty called tolower.mod, which lowercases everything
which is piped to it.  (for instance, "dir | tolower")

it accepets any of three command line options "-b" for binary files,
"-l" to force lowercase (default) and "-u" to UPCASE everything.

Let me know if you have any questions.

bt garner ("One of those weirdos who uses M2 in *real life*")

========= cut here ====================================================
MODULE tolower;

(*
 *   tolower.mod - an OS/2 (and DOS) pipe for CONVERting characters
 *   (C) copyright, 1991 by bt garner, all rights reserved.
 *)

(* some standard import files *)

FROM Str IMPORT  Caps,Lows,Copy,Length;
FROM FIO IMPORT  ErrorOutput,StandardOutput,StandardInput,OpenRead,Create,
                 RdBin,WrBin,WrStr,WrLn,File,EOF,AssignBuffer,IOcheck,Close,
                 RdStr;
FROM Lib IMPORT  ParamCount,ParamStr,SetReturnCode;

(* some consts defined for the heck of it *)

CONST
    AuthorString = "tolower.exe (filter) (C) copyright 1991 by bt garner";
    Instructions = "tolower [ -ulb ] <stdin> <stdout>";

    BufferSize =  (4 * 512);

    stdin  = StandardInput;
    stdout = StandardOutput;
    stderr = ErrorOutput;

(* some vars outlined for programming *)

VAR
    counter,np:        CARDINAL;                  (* just variables *)
    inchar,outchar:    ARRAY [0..127] OF CHAR;    (* input array *)
    parameter:         ARRAY [0..63] OF CHAR;     (* CL Param *)
    input,output:      BOOLEAN;                   (* has a file been found *)
    lowercase,binary:  BOOLEAN;                   (* which way to go *)
    param:             CHAR;                      (* for prog conv *)
    infile,outfile:    File;                      (* in and out handles *)

    InBuf,OutBuf:      ARRAY [0..BufferSize] OF BYTE;

BEGIN
    IOcheck := FALSE;
    lowercase := TRUE;

    infile  := stdin;
    outfile := stdout;
    input   := FALSE;
    output  := FALSE;
    binary  := FALSE;

    np := ParamCount();
    FOR counter := np TO 1 BY -1 DO
       ParamStr(parameter, counter);
       param := parameter[0];
       IF ((param = "-") OR (param = "/")) THEN
          Lows(parameter);
          CASE parameter[1] OF
             'b':  binary    := TRUE;
          |  'l':  lowercase := TRUE;
          |  'u':  lowercase := FALSE;
          END; (* case *)
       ELSE (* parameter is *not* a operational parameter *)
         IF (NOT input) THEN  (* no infile yet *)
            infile := OpenRead(parameter);
            IF (infile = MAX(CARDINAL)) THEN
                WrStr(stderr, "Cannot open ");
                WrStr(stderr, parameter);
                WrStr(stderr," as input.");
                WrLn(stderr);
                SetReturnCode(1);
                HALT;
            ELSE
                input := TRUE;
            END; (* if *)
         ELSIF (NOT output) THEN  (* no outfile yet *)
            outfile := Create(parameter);
            IF (outfile = MAX(CARDINAL)) THEN
                WrStr(stderr, "Cannot open ");
                WrStr(stderr, parameter);
                WrStr(stderr," as output.");
                WrLn(stderr);
                SetReturnCode(1);
                HALT;
            ELSE
                output := TRUE;
            END; (* if *)
         END;   (* if NOT input *)
      END; (* parameter if *)
    END; (* for looping through parameters *)

    AssignBuffer(infile, InBuf);
    AssignBuffer(outfile, OutBuf);

    LOOP
        IF (binary) THEN
            counter := RdBin(infile, inchar, SIZE(inchar));
            IF (lowercase)
               THEN Lows(inchar);
               ELSE Caps(inchar);
            END;
            WrBin(outfile, inchar, counter);
        ELSE
            RdStr(infile, inchar);
            IF (lowercase)
               THEN Lows(inchar);
               ELSE Caps(inchar);
            END;
            WrStr(outfile, inchar);
            IF Length(inchar) <> SIZE(inchar) THEN WrLn(outfile); END;
        END;
        IF EOF THEN EXIT; END;
    END; (* loop *)

    IF (input) THEN Close(infile); END;
    IF (output) THEN Close(outfile); END;

    IF (lowercase AND NOT lowercase) THEN
       Copy(inchar,AuthorString);
       Copy(outchar,Instructions);
    END; (* dummy if for embedded strings *)

END tolower.



Sun, 04 Sep 1994 21:15:27 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. M2 and MIPS and M2 to C Translators

2. M2, Oberon, and embedded systems (was Re: m2 / Oberon appendix to 3117)

3. M2, Oberon, and embedded systems (was m2 / Oberon appendix to 3117)

4. C to M2 (M2 review ?)

5. digital filter, dsp, real-time-filtering example

6. Kalman Filters, Particle Filters and SVM's

7. Z-80 based M2 compilers: FTL vs. Turbo M2

8. Simple text file m2 program (Stony Brook m2, ver. 4.0)

9. Is there a PD M2-to-C translator, or PD M2 parser?

10. _XTD M2, C++

11. TS C++ & M2

12. how to choose a language (was Re: Report on defect in M2 vs C)

 

 
Powered by phpBB® Forum Software