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 / delamc.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  119 lines

  1. ############################################################################
  2. #
  3. #    File:     delamc.icn
  4. #
  5. #    Subject:  Program to delaminate file using tab characters
  6. #
  7. #    Author:   Thomas R. Hicks
  8. #
  9. #    Date:     May 28, 1989
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #  
  17. #     This program delaminates standard input into several output
  18. #  files according to the separator characters specified by the
  19. #  string following the -t option.  It writes the fields in each
  20. #  line to the corresponding output files as individual lines. If no
  21. #  data occurs in the specified position for a given input line an
  22. #  empty output line is written. This insures that all output files
  23. #  contain the same number of lines as the input file.
  24. #  
  25. #     If - is used as an output file name, the corresponding field
  26. #  is written to the standard output. If the -t option is not used,
  27. #  an ascii horizontal tab character is assumed as the default field
  28. #  separator.
  29. #  
  30. #     The use of delamc is illustrated by the following examples.
  31. #  The command
  32. #  
  33. #          delamc labels opcodes operands
  34. #  
  35. #  writes the fields of standard input, each of which is separated
  36. #  by a tab character, to the output files labels, opcodes, and
  37. #  operands.  The command
  38. #  
  39. #          delamc -t: scores names matric ps1 ps2 ps3
  40. #  
  41. #  writes the fields of standard input, each of which are separated
  42. #  by a colon, to the indicated output files.  The command
  43. #  
  44. #          delamc -t,: oldata f1 f2
  45. #  
  46. #  separates the fields using either a comma or a colon.
  47. #  
  48. ############################################################################
  49. #
  50. #  Links:  usage
  51. #
  52. ############################################################################
  53.  
  54. link usage
  55.  
  56. procedure main(a)
  57.    local tabset, fylist, nxtarg
  58.    if match("-t",a[1]) then {        # tab char given
  59.       tabset := cset(a[1][3:0])
  60.       pop(a)                # get rid of that argument
  61.       }
  62.     if 0 = *(fylist := doutfyls(a)) then
  63.        Usage("delamc [-tc] {outputfile | -} ...")
  64.     /tabset := cset(&ascii[10])            # tab is default separator
  65.     delamrc(tabset,fylist)            # call main routine
  66. end
  67.  
  68. # delamrc - do actual division of input file using tab chars
  69. #
  70. procedure delamrc(tabset,fylist)
  71.     local i, flen, line
  72.     while line := read() do
  73.         {
  74.         i := 1
  75.         flen := *fylist
  76.         line ? while (i <= flen) do
  77.             {
  78.             if i = flen then
  79.                 write(fylist[i][2],tab(0) | "")
  80.             else
  81.                 write(fylist[i][2],tab(upto(tabset)) | tab(0) | "")
  82.             move(1)
  83.             i +:= 1
  84.             }
  85.         }
  86. end
  87.  
  88. # doutfyls - process output file arguments; return list
  89. #
  90. procedure doutfyls(a)
  91.    local lst, x, i
  92.    lst := []
  93.    i := 1
  94.    while \a[i] do {
  95.       if x := llu(a[i],lst) then        # already in list
  96.          lst |||:= [[a[i],lst[x][2]]]
  97.       else                    # not in list
  98.          if a[i] == "-" then            # standard out
  99.             lst |||:= [[a[i],&output]]
  100.          else                # a new file
  101.             if not (x := open(a[i],"w")) then
  102.                stop("Cannot open ",a[i]," for output")
  103.             else lst |||:= [[a[i],x]]
  104.       i +:= 1
  105.       }
  106.    return lst
  107. end
  108.  
  109. # llu - lookup file name in output file list
  110. #
  111. procedure llu(str,lst)
  112.    local i
  113.    i := 1
  114.    while \lst[i] do {
  115.       if \lst[i][1] == str then return i
  116.       i +:= 1
  117.       }
  118. end
  119.