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 / spread.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  88 lines

  1. ############################################################################
  2. #
  3. #    File:     spread.icn
  4. #
  5. #    Subject:  Program to format tab-separated data columns
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     June 6, 1999
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #    Spread reads data presented in tab-separated fields, such
  18. #    as some some spreadsheets export, and outputs the data in
  19. #    space-separated columns of the minimum necessary width.
  20. #
  21. #    Usage:  spread [-t c] [-g n] [-r] [file...]
  22. #
  23. #    -g n    set gutter width between output columns (default is 1)
  24. #    -r    right-justify the fields instead of left-justifying
  25. #    -t c    set separator character(s) for data (default is \t)
  26. #
  27. ############################################################################
  28. #
  29. #  Links:  options
  30. #
  31. ############################################################################
  32.  
  33. link options
  34.  
  35. procedure main(args)
  36.    local opts, sep, gutter, justify, fname, f 
  37.    local data, colsz, s, n, i, t
  38.  
  39.    # process options and set defaults
  40.    opts := options(args, "g+t:r")            # command options
  41.    sep := cset(\opts["t"]) | '\t'            # separator cset
  42.    gutter := integer(\opts["g"]) | 1            # output gutter width
  43.    justify := if \opts["r"] then right else left    # justifying procedure
  44.  
  45.    # load data into memory
  46.    data := []
  47.    if *args = 0 then
  48.       while put(data, read())
  49.    else {
  50.       every fname := !args do {
  51.          f := open(fname) | stop("can't open ", fname)
  52.      while put(data, read(f))
  53.      }
  54.       }
  55.  
  56.    # scan data to record maximum column widths needed
  57.    colsz := []
  58.    every s := !data do s ? {
  59.       i := 0
  60.       while n := (*tab(upto(sep)) | (0 < *tab(0))) do {
  61.      move(1)
  62.      i +:= 1
  63.      if n <= colsz[i] then
  64.         next
  65.      if i > *colsz then
  66.         put(colsz, n)
  67.      else
  68.         colsz[i] := n
  69.          }
  70.       }
  71.  
  72.    # adjust column sizes to allow for gutters
  73.    every !colsz +:= gutter
  74.    if justify === right then
  75.       colsz[1] -:= gutter
  76.  
  77.    # write padded output
  78.    every s := !data do s ? {
  79.       i := 0
  80.       while t := tab(upto(sep)) do {
  81.          writes(justify(t, colsz[i +:= 1]))
  82.      move(1)
  83.      }
  84.       write(justify(tab(0), colsz[i +:= 1]))
  85.       }
  86.    
  87. end
  88.