MODULE FileCountChars; (* ========================================================================= Example GPCP .NET Console Program Count the number of occurrences of different categories of characters in a text file using .NET StreamReader, Command-line arguments and a CASE statement Author : Chris Burrows Created: Dec 2006 (c) 2006-2008 CFB Software http://www.cfbsoftware.com/gpcp ========================================================================= *) IMPORT Sys := "[mscorlib]System", IO := "[mscorlib]System.IO", Out, ProgArgs, CPmain; VAR sr: IO.StreamReader; s: Sys.String; argCount: INTEGER; filename: ARRAY 256 OF CHAR; vowels, letters, capitals, digits, others: INTEGER; (* ==================================================================== *) PROCEDURE WriteCount(count: INTEGER; IN msg: ARRAY OF CHAR); BEGIN Out.Int(count, 3); Out.String(msg); Out.Ln() END WriteCount; (* ==================================================================== *) PROCEDURE CountChars(IN s: ARRAY OF CHAR); VAR i: INTEGER; BEGIN FOR i := 0 TO LEN(s$) - 1 DO CASE s[i] OF 'a', 'e', 'i', 'o', 'u': INC(vowels); INC(letters) | 'A', 'E', 'I', 'O', 'U': INC(vowels); INC(letters); INC(capitals) | 'b'..'d', 'f'..'h', 'j'..'n', 'p'..'t', 'v'..'z': INC(letters) | 'B'..'D', 'F'..'H', 'J'..'N', 'P'..'T', 'V'..'Z': INC(letters); INC(capitals) | '0'..'9': INC(digits) ELSE INC(others) END END END CountChars; (* ==================================================================== *) BEGIN vowels := 0; letters := 0; capitals := 0; digits := 0; others := 0; argCount := ProgArgs.ArgNumber(); IF argCount # 1 THEN Out.String('Usage: FileCountChars '); Out.Ln() ELSE ProgArgs.GetArg(0, filename); Out.String(filename); Out.Ln(); IF ~IO.File.Exists(MKSTR(filename)) THEN Out.String("Error: file not found."); Out.Ln() ELSE sr := IO.StreamReader.init(MKSTR(filename)); s := sr.ReadLine(); WHILE s # NIL DO CountChars(s); s := sr.ReadLine() END; sr.Close; WriteCount(letters, ' letters'); WriteCount(capitals, ' capitals'); WriteCount(vowels, ' vowels'); WriteCount(letters - vowels, ' consonants'); WriteCount(digits, ' digits'); WriteCount(others, ' others') END END END FileCountChars.