summaryrefslogtreecommitdiff
path: root/ipl/gprogs/pextract.icn
blob: 60f96d7908edb2889c25d9c66cc95d3edfdc78ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
############################################################################
#
#	File:     pextract.icn
#
#	Subject:  Program to separate good and bad patterns
#
#	Author:   Ralph E. Griswold
#
#	Date:     September 1, 1993
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  This program takes the name of a file containing tile specifications
#  on the command line.   Tiles to be extracted are entered from standard
#  input.  Extracted tiles are written to standard output.
#
#  Options:
#
#	-b		replace selected tiles by blank tiles
#	-d		delete selected tiles from specification file
#	-c		copy selected tiles, do not blank or delete
#			  them.  This is the default; -c overrides
#			  -b and -d.
#
############################################################################
#
#  Links:  options, patutils
#
############################################################################

link options
link patutils

procedure main(args)
   local file, input, i, hitlist, patlist, spec, lo, hi, output
   local subspec, opts

   opts := options(args, "cbd")
   if \opts["c"] then opts["b"] := opts["d"] := &null
   if \opts["d"] then opts["b"] := 1

   file := args[1] | stop("*** no pattern list specified")

   input := open(file) | stop(" *** cannot open input file")
 
   hitlist := set()			# construct set of indices to remove

   while spec := read() do {
      spec ? {
         while subspec := tab(upto(',') | 0) do {
            if insert(hitlist, integer(subspec)) then {		# integer
               move(1) | break
               tab(many(' '))
               }
            else {
               subspec ? {
                  lo := tab(many(&digits)) &
                  ="-" &
                  hi := tab(many(&digits)) &
                  lo <= hi &
                  pos(0) | {
                     write(&errout, "*** bad specification")
                     next
                     }
                  if not(integer(hi) & integer(lo)) then {
                     write(&errout, "*** bad specification")
                     next
                     }
                  every insert(hitlist, 0 < (lo to hi))
                  }
               move(1) | break
               tab(many(' '))
               }
            }
         }
      }

   patlist := []			# read in list of patterns

   while put(patlist, readpatt(input))

   close(input)

   output := open(file, "w") |
      stop("*** cannot reopen specified file for output")
   
   every i := !sort(hitlist) do {		# discard and "delete"
      write(patlist[i]) | write(&errout, "*** ", i, " out of bounds")
      if \opts["b"] then patlist[i] := "1,#0"
      }

   if \opts["d"] then
      every write(output, "1,#0" ~== !patlist)
   else
      every write(output, !patlist)

end