home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-05-09 | 2.8 KB | 91 lines | [TEXT/PICN] |
- ############################################################################
- #
- # fileprint.icn
- #
- # This program reads a file and writes out a representation of each
- # character in several forms: hexadecimal, octal, decimal, symbolic, and
- # ANSI code. The user is prompted for a file name with a Get File dialog.
- # Prompting continues until the user selects Cancel.
- #
- # Since this program is comparatively slow, it is not suitable
- # for processing very large files -- unless you have no alternative.
- #
- # There are several useful extensions that could be added to this program,
- # including other character representations, an option to skip an initial
- # portion of the input file, and suppression of long ranges of identical
- # characters.
- #
- ############################################################################
- #
- # Program note:
- #
- # This program illustrates a situation in which co-expressions can be
- # used to considerably simplify programming. Try recasting it without
- # co-expressions.
- #
- ############################################################################
-
- procedure main()
- local width, chars, nonprint, prntc, ans, hex, sym, dec, oct
- local ansgen, hexgen, symgen, decgen, octgen, chrgen, prtgen, c, cnt
- local name, infile, line, length, bar
-
- width := 16
- chars := string(&cset)
- nonprint := chars[1:33] || chars[128:0]
- prntc := map(chars,nonprint,repl(" ",*nonprint))
- while name := getfile("File?") do {
- close(\infile)
- infile := open(name) | {
- write(&errout,"*** cannot open ",name)
- next
- }
- ans := table(" |")
- hex := table()
- sym := table()
- dec := table()
- oct := table()
- ansgen := create !["NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL"," BS",
- " HT"," LF", " VT"," FF"," CR"," SO"," SI","DLE","DC1","DC2","DC3","DC4",
- "NAK","SYN", "ETB","CAN"," EM","SUB","ESC"," FS"," GS"," RS"," US","SPC",
- "DEL"]
- hexgen := create !"0123456789ABCDEF" || !"0123456789ABCDEF"
- octgen := create (0 to 3) || (0 to 7) || (0 to 7)
- chrgen := create !chars
- prtgen := create !prntc
- every c := !&cset do {
- ans[c] := @ansgen || "|"
- oct[c] := @octgen || "|"
- hex[c] := " " || @hexgen || "|"
- sym[c] := " " || @prtgen || " |"
- }
-
- cnt := -1 # to handle zero-indexing of byte count
-
- while line := reads(infile,width) do { # read one line's worth
- length := *line # may not have gotten that many
- bar := "\n" || repl("-",5 + length * 4)
- write()
- writes("BYTE|")
- every writes(right(cnt + (1 to length),3),"|")
- write(bar)
- writes(" HEX|")
- every writes(hex[!line])
- write(bar)
- writes(" OCT|")
- every writes(oct[!line])
- write(bar)
- writes(" DEC|")
- every writes(right(upto(!line,chars) - 1,3),"|")
- write(bar)
- writes(" SYM|")
- every writes(sym[!line])
- write(bar)
- writes(" ANS|")
- every writes(ans[!line])
- write(bar)
- cnt +:= length
- }
- }
- end
-