; HEXDUMP
;
; Program for Proteus
;
; (C) 1998-2004 Simone Zanella Productions
;
; This program prints out in readable format (hexadecimal) the contents of specified file,
; according to the following template:
;
; 1234: 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 | 123456789012345678
#!proteus -z
IF LT(ARGC, 6)
CONSOLELN "Syntax: proteus HEXDUMP.PRT <source_file> <destination>"
ABORT 0
FI
H = FOPEN(ARGV(5), 4)
D = FOPEN(ARGV(6), 26)
L = FSIZE(H)
; Create a mapping to space for all non-printable characters
S = ""
FOR X = 0 TO 31
S = S CHR(X)
NEXT
MP = MAPNEW(S, REPLICATE(" ", 32))
; Calculate the length of counter at the beginning of each line
M = STRLEN(PFORMAT("X", L))
B = 0
WHILE NOT(FEOF(H))
SB = FREAD(H, 16)
; Print counter
S = PFORMAT("0" M "X", B) ": "
; Print bytes in hexadecimal
FOR X = 1 TO STRLEN(SB)
S = S PFORMAT("02X", ASC(SUBSTR(SB, X, 1))) " "
IF EQ(X, 8)
S = S " "
FI
NEXT
; Pad line
X = STRLEN(SB)
IF LT(X, 16)
Y = MUL(3, SUB(16, X))
IF LT(X, 9)
Y = ADD(Y, 2)
FI
S = S REPLICATE(" ", Y)
FI
; Print out line, mapping to spaces all non-printable characters
S = S " | " MAP(MP, SB)
FWRITELN(D, S)
ADD(@B, STRLEN(SB))
LOOP
FCLOSE(H)
FCLOSE(D)
ABORT 0