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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
include <mach.h>
include <fset.h>
include "../import.h"
define DEBUG false
# IP_GSTR -- Get a string of the specifed length from the given offset.
procedure ip_gstr (fd, offset, len, outstr)
int fd
int offset
int len
char outstr[ARB]
int nstat, read()
pointer sp, buf
begin
call smark (sp)
call salloc (buf, len+2, TY_CHAR)
call aclrc (Memc[buf], len+2)
call aclrc (outstr, len+2)
call ip_lseek (fd, offset)
nstat = read (fd, Memc[buf], len)
if (mod(offset,2) == 0 && offset > 1)
call bytmov (Memc[buf], 2, Memc[buf], 1, len)
call chrupk (Memc[buf], 1, outstr, 1, len)
if (DEBUG) { call eprintf ("ip_gstr: :%s: len=%d\n");
call pargstr(outstr) ; call pargi (len) }
call sfree (sp)
end
# IP_GETB -- Get a byte from the given offset.
short procedure ip_getb (fd, offset)
int fd
int offset
int nstat, read()
short val
char buf[2]
begin
call ip_lseek (fd, offset)
nstat = read (fd, buf, 2)
if (mod(offset,2) == 0)
call bytmov (buf, 2, buf, 1, 2)
call chrupk (buf, 1, buf, 1, 2)
if (DEBUG) { call eprintf ("ip_getb: %d\n"); call pargs(buf[1]) }
if (buf[1] < 0)
val = buf[1] + 256
else
val = buf[1]
return (val)
end
# IP_GETU -- Get a unsigned short integer from the given offset.
int procedure ip_getu (fd, offset)
int fd
int offset
int val
short ip_gets()
begin
val = ip_gets (fd, offset)
if (val < 0)
val = val + 65536
return (val)
end
# IP_GET[silrd] -- Get a value of <type> from the given offset.
$for (silrd)
PIXEL procedure ip_get$t (fd, offset)
int fd
int offset
int nstat, read()
PIXEL val
begin
call ip_lseek (fd, offset)
$if (datatype == il)
nstat = read (fd, val, SZ_INT32 * SZB_CHAR)
if (SZ_INT != SZ_INT32)
call iupk32 (val, val, 1)
$else
nstat = read (fd, val, SZ_PIXEL * SZB_CHAR)
$endif
$if (datatype == rd)
call ieeupk$t (val)
$endif
if (DEBUG) { call eprintf ("ip_get: %g\n"); call parg$t(val) }
return (val)
end
$endfor
# IP_GETN -- Get a native floating point number from the given offset.
real procedure ip_getn (fd, offset)
int fd
int offset
int nstat, read()
real rval
begin
call ip_lseek (fd, offset)
nstat = read (fd, rval, SZ_REAL)
if (DEBUG) { call eprintf ("ip_getn: %g\n"); call pargr(rval) }
return (rval)
end
# IP_GETN8 -- Get a native double precision floating point number from the
# given offset.
double procedure ip_getn8 (fd, offset)
int fd
int offset
int nstat, read()
double dval
begin
call ip_lseek (fd, offset)
nstat = read (fd, dval, SZ_DOUBLE)
if (DEBUG) { call eprintf ("ip_getn8: %g\n"); call pargd(dval) }
return (dval)
end
# IP_AGETB -- Get an array of bytes from the file. The data pointer is
# allocated if necessary and contains the data on output.
procedure ip_agetb (fd, ptr, len)
int fd #i file descriptor
pointer ptr #i data pointer
int len #i length of array
pointer sp, buf
int fp, nval, nstat
int ip_lnote(), read()
begin
fp = ip_lnote(fd)
if (mod(fp,2) == 0 && fp != 1)
nval = len
else
nval = len + 1
call smark (sp)
call salloc (buf, nval, TY_CHAR)
if (ptr == NULL)
call malloc (ptr, nval * SZB_CHAR, TY_CHAR)
nstat = read (fd, Memc[buf], nval / SZB_CHAR + 1)
fp = ip_lnote(fd)
if (mod(fp,2) == 0 && fp != 1)
call bytmov (Memc[buf], 2, Memc[buf], 1, nval)
call achtbc (Memc[buf], Memc[ptr], len)
call sfree (sp)
end
# IP_AGETU -- Get an array of <type> from the file. The data pointer is
# allocated if necessary and contains the data on output.
procedure ip_agetu (fd, ptr, len)
int fd #i file descriptor
pointer ptr #i data pointer
int len #i length of array
begin
call ip_agets (fd, ptr, len)
call achtsu (Mems[ptr], Mems[ptr], len)
end
# IP_AGET[silrd] -- Get an array of <type> from the file. The data pointer is
# allocated if necessary and contains the data on output.
$for (silrd)
procedure ip_aget$t (fd, ptr, len)
int fd #i file descriptor
pointer ptr #i data pointer
int len #i length of array
int nstat
int read()
begin
if (ptr == NULL)
call malloc (ptr, len, TY_PIXEL)
$if (datatype == il)
nstat = read (fd, Mem$t[ptr], len * SZ_INT32)
if (SZ_INT != SZ_INT32)
call iupk32 (Mem$t[ptr], Mem$t[ptr], len)
$else
nstat = read (fd, Mem$t[ptr], len * SZ_PIXEL)
$endif
$if (datatype == rd)
call ieevupk$t (Mem$t[ptr], Mem$t[ptr], len)
$endif
end
$endfor
# IP_AGETN -- Get an array of native floats from the file. The data pointer is
# allocated if necessary and contains the data on output.
procedure ip_agetn (fd, ptr, len)
int fd #i file descriptor
pointer ptr #i data pointer
int len #i length of array
int nstat
int read()
begin
if (ptr == NULL)
call malloc (ptr, len, TY_REAL)
nstat = read (fd, Memr[ptr], len * SZ_REAL)
end
# IP_AGETN8 -- Get an array of native doubles from the file. The data pointer
# is allocated if necessary and contains the data on output.
procedure ip_agetn8 (fd, ptr, len)
int fd #i file descriptor
pointer ptr #i data pointer
int len #i length of array
int nstat
int read()
begin
if (ptr == NULL)
call malloc (ptr, len, TY_DOUBLE)
nstat = read (fd, Memd[ptr], len * SZ_DOUBLE)
end
# -----------------------------------------------------------------
# ------------------ UTILITY FILE I/O FUNCTIONS -------------------
# -----------------------------------------------------------------
define BLKSIZE 1024
# IP_LINE -- Return the offset of the start of the given line number.
int procedure ip_line (fd, line)
int fd #i input file descriptor
int line #i line number to search
pointer sp, cbuf, buf
int nl, offset, i, nread, fsize
int read(), fstati()
define done_ 99
define err_ 98
begin
if (line == 1) {
return (1)
} else {
call smark (sp)
call salloc (buf, BLKSIZE, TY_CHAR)
call salloc (cbuf, BLKSIZE, TY_CHAR)
# Rewind file descriptor
call ip_lseek (fd, BOF)
nl = 1
offset = 1
nread = BLKSIZE / SZB_CHAR
fsize = fstati (fd, F_FILESIZE)
while (read (fd, Memc[buf], nread) != EOF) {
# Convert it to spp chars.
call ip_lskip (fd, nread)
call chrupk (Memc[buf], 1, Memc[cbuf], 1, BLKSIZE)
do i = 1, BLKSIZE {
if (Memc[cbuf+i-1] == '\n') {
nl = nl + 1
offset = offset + 1
if (nl == line)
goto done_
} else
offset = offset + 1
if (offset >= fsize)
goto err_
}
}
err_ call sfree (sp)
call ip_lseek (fd, BOF)
return (ERR)
done_ if (DEBUG) { call eprintf("ip_line: '%s'\n"); call pargi(offset) }
call sfree (sp)
call ip_lseek (fd, offset)
return (offset)
}
end
# IP_LOCATE -- Return the offset of the start of the given pattern.
int procedure ip_locate (fd, offset, pattern)
int fd #i input file descriptor
int offset #i offset to begin search
char pattern[ARB] #i pattern to locate
pointer sp, cbuf, buf
int fsize, nread, patlen, cur_offset, loc
int fstati(), read(), strsearch(), strlen()
define done_ 99
begin
# Rewind file descriptor
call ip_lseek (fd, offset)
cur_offset = offset
call smark (sp)
call salloc (buf, BLKSIZE, TY_CHAR)
call salloc (cbuf, BLKSIZE, TY_CHAR)
if (DEBUG) { call eprintf("ip_loc: offset %d\n"); call pargi(offset)}
nread = BLKSIZE / SZB_CHAR
fsize = fstati (fd, F_FILESIZE)
patlen = strlen (pattern)
while (read (fd, Memc[buf], nread) != EOF) {
# Convert it to spp chars.
call ip_lskip (fd, nread)
call chrupk (Memc[buf], 1, Memc[cbuf], 1, BLKSIZE)
loc = strsearch (Memc[cbuf], pattern)
if (loc != 0) {
cur_offset = cur_offset + loc - 1 - patlen
goto done_
} else {
# Allow some overlap in case the pattern broke over the blocks.
cur_offset = cur_offset + BLKSIZE - 2 * patlen
call ip_lseek (fd, cur_offset)
if (cur_offset + BLKSIZE > fsize)
nread = fsize - cur_offset + 1
}
}
call sfree (sp)
call ip_lseek (fd, BOF)
return (ERR)
done_ if (DEBUG) { call eprintf("ip_loc: %d\n"); call pargi(cur_offset)}
call sfree (sp)
call ip_lseek (fd, offset)
return (cur_offset)
end
# IP_LSEEK -- Set the file position as a byte offset.
procedure ip_lseek (fd, offset)
int fd #i file descriptor
int offset #i requested offset
long cur_offset, where, fsize
int fstati()
common /fiocom/ cur_offset
begin
if (offset == BOF || offset == ERR) {
cur_offset = 1
call seek (fd, BOF)
} else {
fsize = fstati (fd, F_FILESIZE) * SZB_CHAR
cur_offset = min (fsize, offset)
where = min (fsize, (offset/SZB_CHAR+mod(offset,2)))
call seek (fd, where)
}
end
# IP_LNOTE -- Note the file position as a byte offset.
int procedure ip_lnote (fd)
int fd #i file descriptor (unused)
long cur_offset
common /fiocom/ cur_offset
begin
return (cur_offset)
end
# IP_LSKIP -- Bump the file position by a byte offset.
procedure ip_lskip (fd, skip)
int fd #i file descriptor
int skip
long cur_offset
common /fiocom/ cur_offset
begin
call ip_lseek (fd, cur_offset+skip)
end
|