; ASCSTAT
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; This program displays a graph (LENGTH columns wide) that represents
; the relative frequency of characters found in the specified file;
; output is written to console.
; Implicit parameters: NULL input and output
;!proteus -z
Width = 53
IF ISEMPTY(ARGV(5))
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " [filename [length]]"
CONSOLELN ""
CONSOLELN "Purpose: displays to console the relative frequency of characters found"
CONSOLELN "into \"filename\"; \"length\" sets the width of the graph"
CONSOLELN "in columns (default= " Width ")"
ABORT 0
FI
FileName = ARGV(5)
IF ISNOTEMPTY(ARGV(6))
Width = ARGV(6)
FI
; Open filename
FH = FOPEN(FileName, 1)
IF EQ(FH, -1)
CONSOLELN "File " FileName " not found."
ABORT 0
FI
; Create an array to count occurrences of each character
VH = VECNEW(256)
FOR X = 1 TO 256
VECSET(VH, X, 0)
NEXT
FS = FSIZE(FH)
; Process the file
WHILE NOT(FEOF(FH))
S = FREAD(FH, 2048)
STRTRAVERSE(S, UpdateTable)
LOOP
FCLOSE(FH)
; Determine the highest number of repetitions
Max = 0
FOR X = 1 TO 256
IF LT(Max, VECGET(VH, X))
Max = VECGET(VH, X)
FI
NEXT
IF EQ(Max, 0)
CONSOLELN "File is empty."
ABORT 0
FI
; Determine the length of a single element of the graph
IF GT(Max, Width)
Increment = FDIV(Max, Width)
ELSE
Increment = 1
FI
CONSOLELN "Character frequencies for " FileName \
" (" DATE() ", " RTRIM(TIME(), " ") "):"
CONSOLELN "CHR ASC NUMBER FREQ"
FS = FDIV(FS, 100)
FOR X = 1 TO 256
IF ISET(N, VECGET(VH, X))
CONSOLELN PrintChar(DEC(X)) " " PADL(X, 3, "0") " " \
PADR(N, 8, " ") PADR(PFORMAT("04.2lf", FDIV(N, FS)) \
"%", 8, " ") REPLICATE("+", FDIV(N, Increment))
FI
NEXT
ABORT 0
FUNCTION UpdateTable(car, dummy)
i = ASC(car)
VECSET(_VH, i, INC(VECGET(_VH, i)))
RETURN 0
FUNCTION PrintChar(x)
IF GE(x, 32)
RETURN CHR(x) " "
FI
RETURN "0x" PFORMAT("02X", x)