home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / fileprnt.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  106 lines

  1. ############################################################################
  2. #
  3. #    File:     fileprnt.icn
  4. #
  5. #    Subject:  Program to display characters in file
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     November 21, 1989
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #     This program reads the file specified as a command-line argument and
  18. #  writes out a representation of each character in several forms:
  19. #  hexadecimal, octal, decimal, symbolic, and ASCII code.
  20. #
  21. #     Input is from a named file rather than standard input, so that it
  22. #  can be opened in untranslated mode.  Otherwise, on some systems, input
  23. #  is terminated for characters like ^Z.
  24. #
  25. #     Since this program is comparatively slow, it is not suitable
  26. #  for processing very large files.
  27. #
  28. #     There are several useful extensions that could be added to this program,
  29. #  including other character representations, an option to skip an initial
  30. #  portion of the input file, and suppression of long ranges of identical
  31. #  characters.
  32. #
  33. ############################################################################
  34. #
  35. #  Requires: co-expressions
  36. #
  37. ############################################################################
  38. #
  39. #  Program note:
  40. #
  41. #     This program illustrates a situation in which co-expressions can be
  42. #  used to considerably simplify programming.  Try recasting it without
  43. #  co-expressions.
  44. #
  45. ############################################################################
  46.  
  47. procedure main(arg)
  48.    local width, chars, nonprint, prntc, asc, hex, sym, dec
  49.    local oct, ascgen, hexgen, octgen, chrgen, prtgen, c
  50.    local cnt, line, length, bar, input
  51.  
  52.    input := open(arg[1],"u") | stop("*** cannot open input file")
  53.    width := 16
  54.    chars := string(&cset)
  55.    nonprint := chars[1:33] || chars[128:0]
  56.    prntc := map(chars,nonprint,repl(" ",*nonprint))
  57.  
  58.    asc := table("   |")
  59.    hex := table()
  60.    sym := table()
  61.    dec := table()
  62.    oct := table()
  63.    ascgen := create "NUL" | "SOH" | "STX" | "ETX" | "EOT" | "ENQ" | "ACK" |
  64.       "BEL" | " BS" | " HT" | " LF" |  " VT" | " FF" | " CR" | " SO" | " SI" |
  65.       "DLE" | "DC1" | "DC2" | "DC3" | "DC4" | "NAK" | "SYN" |  "ETB" | "CAN" |
  66.       " EM" | "SUB" | "ESC" | " FS" | " GS" | " RS" | " US" | " SP"
  67.    hexgen := create !"0123456789ABCDEF" || !"0123456789ABCDEF"
  68.    octgen := create (0 to 3) || (0 to 7) || (0 to 7)
  69.    chrgen := create !chars
  70.    prtgen := create !prntc
  71.    every c := !&cset do {
  72.       asc[c] := @ascgen || "|"
  73.       oct[c] := @octgen || "|"
  74.       hex[c] := " " || @hexgen || "|"
  75.       sym[c] := " " || @prtgen || " |"
  76.       }
  77.    asc[char(127)] := "DEL|"            # special case
  78.  
  79.    cnt := -1    # to handle zero-indexing of byte count
  80.  
  81.    while line := reads(input,width) do {    # read one line's worth
  82.       length := *line    # may not have gotten that many
  83.       bar := "\n" || repl("-",5 + length * 4)
  84.       write()
  85.       writes("BYTE|")
  86.       every writes(right(cnt + (1 to length),3),"|")
  87.       write(bar)
  88.       writes(" HEX|")
  89.       every writes(hex[!line])
  90.       write(bar)
  91.       writes(" OCT|")
  92.       every writes(oct[!line])
  93.       write(bar)
  94.       writes(" DEC|")
  95.       every writes(right(ord(!line),3),"|")
  96.       write(bar)
  97.       writes(" SYM|")
  98.       every writes(sym[!line])
  99.       write(bar)
  100.       writes(" ASC|")
  101.       every writes(asc[!line])
  102.       write(bar)
  103.       cnt +:= length
  104.       }
  105. end
  106.