home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / mep1 / Samples / Programs / fileprint.icn < prev    next >
Encoding:
Text File  |  1989-05-09  |  2.8 KB  |  91 lines  |  [TEXT/PICN]

  1. ############################################################################
  2. #
  3. #  fileprint.icn
  4. #
  5. #     This program reads a file and writes out a representation of each
  6. #  character in several forms:  hexadecimal, octal, decimal, symbolic, and
  7. #  ANSI code. The user is prompted for a file name with a Get File dialog.
  8. #  Prompting continues until the user selects Cancel.
  9. #
  10. #     Since this program is comparatively slow, it is not suitable
  11. #  for processing very large files -- unless you have no alternative.
  12. #
  13. #     There are several useful extensions that could be added to this program,
  14. #  including other character representations, an option to skip an initial
  15. #  portion of the input file, and suppression of long ranges of identical
  16. #  characters.
  17. #
  18. ############################################################################
  19. #
  20. #  Program note:
  21. #
  22. #     This program illustrates a situation in which co-expressions can be
  23. #  used to considerably simplify programming.  Try recasting it without
  24. #  co-expressions.
  25. #
  26. ############################################################################
  27.  
  28. procedure main()
  29.     local width, chars, nonprint, prntc, ans, hex, sym, dec, oct
  30.     local ansgen, hexgen, symgen, decgen, octgen, chrgen, prtgen, c, cnt
  31.     local name, infile, line, length, bar
  32.     
  33.     width := 16
  34.     chars := string(&cset)
  35.     nonprint := chars[1:33] || chars[128:0]
  36.     prntc := map(chars,nonprint,repl(" ",*nonprint))
  37.     while name := getfile("File?") do {
  38.         close(\infile)
  39.         infile := open(name) | {
  40.             write(&errout,"*** cannot open ",name)
  41.             next
  42.             }
  43.         ans := table("   |")
  44.         hex := table()
  45.         sym := table()
  46.         dec := table()
  47.         oct := table()
  48.         ansgen := create !["NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL"," BS",
  49.             " HT"," LF", " VT"," FF"," CR"," SO"," SI","DLE","DC1","DC2","DC3","DC4",
  50.             "NAK","SYN", "ETB","CAN"," EM","SUB","ESC"," FS"," GS"," RS"," US","SPC",
  51.             "DEL"]
  52.         hexgen := create !"0123456789ABCDEF" || !"0123456789ABCDEF"
  53.         octgen := create (0 to 3) || (0 to 7) || (0 to 7)
  54.         chrgen := create !chars
  55.         prtgen := create !prntc
  56.         every c := !&cset do {
  57.             ans[c] := @ansgen || "|"
  58.             oct[c] := @octgen || "|"
  59.             hex[c] := " " || @hexgen || "|"
  60.             sym[c] := " " || @prtgen || " |"
  61.             }
  62.     
  63.         cnt := -1    # to handle zero-indexing of byte count
  64.     
  65.         while line := reads(infile,width) do {    # read one line's worth
  66.             length := *line    # may not have gotten that many
  67.             bar := "\n" || repl("-",5 + length * 4)
  68.             write()
  69.             writes("BYTE|")
  70.             every writes(right(cnt + (1 to length),3),"|")
  71.             write(bar)
  72.             writes(" HEX|")
  73.             every writes(hex[!line])
  74.             write(bar)
  75.             writes(" OCT|")
  76.             every writes(oct[!line])
  77.             write(bar)
  78.             writes(" DEC|")
  79.             every writes(right(upto(!line,chars) - 1,3),"|")
  80.             write(bar)
  81.             writes(" SYM|")
  82.             every writes(sym[!line])
  83.             write(bar)
  84.             writes(" ANS|")
  85.             every writes(ans[!line])
  86.             write(bar)
  87.             cnt +:= length
  88.             }
  89.         }
  90. end
  91.