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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
include <ctype.h>
define MAX_RANGES 100
define MAX_FIELDS 100
define MAX_LINES 10000
define LEN_WS 3
# FIELDS -- Extract whitespace delimited fields from specified lines of
# an input list. A new list consisting of the extracted fields is output.
# Which lines and fields to extract is specified by the user.
procedure t_fields ()
pointer sp, f_str, l_str, fin
bool quit, name
int list, fields[MAX_FIELDS], lines[3, MAX_LINES], nfields, nlines
int ranges[3,MAX_RANGES], nranges
bool clgetb()
int decode_ranges(), fi_decode_ranges(), clpopni(), clgfil()
begin
# Allocate space on stack for char buffers
call smark (sp)
call salloc (f_str, SZ_LINE, TY_CHAR)
call salloc (l_str, SZ_LINE, TY_CHAR)
call salloc (fin, SZ_LINE, TY_CHAR)
# Open template of input files
list = clpopni ("files")
# Get boolean parameters
quit = clgetb ("quit_if_missing")
name = clgetb ("print_file_names")
# Get the lines and fields to be extracted. Decode ranges.
call clgstr ("fields", Memc[f_str], SZ_LINE)
call clgstr ("lines", Memc[l_str], SZ_LINE)
# Don't impose ordering on field specification
if (fi_decode_ranges (Memc[f_str], ranges, MAX_RANGES, 1,
MAX_FIELDS, nranges) == ERR) {
call error (0, "Error in field specification")
} else
call fi_xpand (ranges, fields, nfields)
# Lines range will be accessed in ascending order
if (decode_ranges (Memc[l_str], lines, MAX_LINES, nlines) == ERR)
call error (0, "Error in line specification")
# While list of input files is not depleted, extract fields
while (clgfil (list, Memc[fin], SZ_FNAME) != EOF)
call fi_xtract (Memc[fin], lines, fields, nfields, quit, name)
call clpcls (list)
call sfree (sp)
end
# FI_XPAND -- expands the output from decode_ranges into an array.
# The output array contains the ordinal of each element in the range;
# no ordering is imposed.
procedure fi_xpand (ranges, out, num)
int ranges[3*MAX_RANGES] # Input ranges array
int out[MAX_LINES] # Output unordered list
int num # Number of entries in output list
int ip, number
int first, last, step
begin
num = 0
for (ip=1; ranges[ip] != NULL; ip=ip+3) {
first = ranges[ip]
last = ranges[ip+1]
step = ranges[ip+2]
if (first == last) {
num = num + 1
out[num] = first
next
}
if (first > last)
step = -1 * step
do number = first, last, step {
num = num + 1
out[num] = number
}
}
end
# FI_DECODE_RANGES -- Parse a string containing a list of integer numbers or
# ranges, delimited by either spaces or commas. Return as output a list
# of ranges defining a list of numbers, and the count of list numbers.
# Range limits must be positive nonnegative integers. ERR is returned as
# the function value if a conversion error occurs. The list of ranges is
# delimited by a single NULL.
int procedure fi_decode_ranges (range_string, ranges, max_ranges, minimum,
maximum, nvalues)
char range_string[SZ_LINE] # Range string to be decoded
int ranges[3, max_ranges] # Range array
int max_ranges # Maximum number of ranges
int minimum, maximum # Minimum and maximum range values allowed
int nvalues # The number of values in the ranges
int ip, nrange, a, b, first, last, step, ctoi()
begin
ip = 1
nrange = 1
nvalues = 0
while (nrange < max_ranges) {
# Default values
a = minimum
b = maximum
step = 1
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get first limit.
# Must be a number, '-', 'x', or EOS. If not return ERR.
if (range_string[ip] == EOS) { # end of list
if (nrange == 1) {
# Null string defaults
ranges[1, 1] = a
ranges[2, 1] = b
ranges[3, 1] = step
ranges[1, 2] = NULL
nvalues = (b - a) / step + 1
return (OK)
} else {
ranges[1, nrange] = NULL
return (OK)
}
} else if (range_string[ip] == '-')
;
else if (range_string[ip] == 'x')
;
else if (IS_DIGIT(range_string[ip])) { # ,n..
if (ctoi (range_string, ip, a) == 0)
return (ERR)
} else
return (ERR)
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get last limit
# Must be '-', or 'x' otherwise b = a.
if (range_string[ip] == 'x')
;
else if (range_string[ip] == '-') {
ip = ip + 1
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
if (range_string[ip] == EOS)
;
else if (IS_DIGIT(range_string[ip])) {
if (ctoi (range_string, ip, b) == 0)
return (ERR)
} else if (range_string[ip] == 'x')
;
else
return (ERR)
} else
b = a
# Skip delimiters
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
# Get step.
# Must be 'x' or assume default step.
if (range_string[ip] == 'x') {
ip = ip + 1
while (IS_WHITE(range_string[ip]) || range_string[ip] == ',')
ip = ip + 1
if (range_string[ip] == EOS)
;
else if (IS_DIGIT(range_string[ip])) {
if (ctoi (range_string, ip, step) == 0)
;
} else if (range_string[ip] == '-')
;
else
return (ERR)
}
# Output the range triple.
first = a
last = b
ranges[1, nrange] = first
ranges[2, nrange] = last
ranges[3, nrange] = step
nvalues = nvalues + abs (last - first) / step + 1
nrange = nrange + 1
}
return (ERR) # ran out of space
end
# FI_XTRACT -- filter out lines from which fields are to be extracted.
# Called once per input file, FI_XTRACT calls FI_PRECORD to process
# each extracted line.
procedure fi_xtract (in_fname, lines, fields, nfields, quit, name)
char in_fname[SZ_FNAME] # Input file name
int lines[3,MAX_LINES] # Ranges of lines to be extracted
int fields[MAX_FIELDS] # Fields to be extracted
int nfields # Number of fields to extract
bool quit # Quit if missing field (y/n)?
bool name # Print file name in each line (y/n)?
pointer sp, lbuf
int in, in_line
bool is_in_range()
int open(), getlongline()
errchk salloc, open, getlongline, fi_precord
begin
# Allocate space for line buffer
call smark (sp)
call salloc (lbuf, SZ_LINE, TY_CHAR)
# Open input file
in = open (in_fname, READ_ONLY, TEXT_FILE)
# Position to specified input line
in_line = 0
repeat {
repeat {
if (getlongline (in, Memc[lbuf], SZ_LINE, in_line) == EOF) {
call close (in)
call sfree (sp)
return
}
} until (is_in_range (lines, in_line))
call fi_precord (in_fname, Memc[lbuf], fields, nfields, quit, name)
}
call close (in)
call sfree (sp)
end
# FI_PRECORD -- extract and output a record of fields.
procedure fi_precord (in_fname, linebuf, fields, nfields, quit, name)
char in_fname[SZ_FNAME] # Name of input file
int linebuf[SZ_LINE] # Line containing fields
int fields[MAX_FIELDS] # List of fields to extract
int nfields # Number of fields to extract
bool quit # Quit if missing field (y/n)?
bool name # Print name in output line (y/n)?
char word[SZ_LINE], white_space[LEN_WS]
int ip, in_field, out_field, i
int ctowrd()
errchk ctowrd
begin
# Fill white space array to be used a field delimeter
do i = 1, LEN_WS
call strcpy (" ", white_space[i], 1)
# Print file name as first field of output list?
if (name) {
call printf ("%s%s")
call pargstr (in_fname)
call pargstr (white_space)
}
# Position to specific field
for (i=1; i <= nfields; i=i+1) {
out_field = fields[i]
in_field = 0
ip = 1
repeat {
if (ctowrd (linebuf, ip, word, SZ_LINE) == 0) {
if (quit) {
call eprintf ("Missing field in input. FILE: %s\n")
call pargstr (in_fname)
call error (0, "Missing field")
} else {
call printf ("\n")
return
}
} else
in_field = in_field + 1
} until (in_field == out_field)
call printf ("%s%s")
call pargstr (word)
call pargstr (white_space)
}
call printf ("\n")
end
|