aboutsummaryrefslogtreecommitdiff
path: root/sys/imfort/bfio.x
blob: ff2d059df3e7f76c841443afceb14da2e921fa0e (plain) (blame)
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<config.h>
include	<mach.h>
include <fio.h>
include	"imfort.h"

.help bfio
.nf --------------------------------------------------------------------------
BFIO -- Binary file i/o.

The IMFORT interface needs its own little binary file i/o interface to deal
with the complexities of blocking and deblocking data in hardware disk blocks.
A little buffering is also desirable to reduce the number of disk transfers
required to read through an image.

		bfaloc (fname, nchars, status)
	fp =	bfopen (fname, acmode, advice)

		bfalcx (fname, nchars, status)
	fp =	bfopnx (fname, acmode, advice)
	nc =	bfbsiz (fp)				# get block size
	nc =	bffsiz (fp)				# get file size
	chan =  bfchan (fp)				# get channel
		bfclos (fp, status)

	stat =	bfread (fp, buf, nchars, offset)	# random i/o
	stat =	bfwrit (fp, buf, nchars, offset)

	stat =  bfseek (fp, offset)			# sequential i/o
	stat =  bfrseq (fp, buf, nchars)
	stat =  bfwseq (fp, buf, nchars)

	stat =	bfflsh (fp)				# flush buffered output

where
	fname	host file name (no virtual filenames here)
	acmode	access mode (READ_ONLY, etc.)
	advice	SEQUENTIAL or RANDOM
	fd	file descriptor, a struct pointer
	buf	char user data buffer
	nchars	amount of data to transfer, SPP chars
	offset	file offset of transfer, SPP chars, 1 indexed
	stat	nchars transferred or ERR

The advice parameter determines the size of the internal buffer allocated
by BFIO.  A small buffer is allocated for random access, a large buffer for
sequential access.  Sequential is usually best.  If advice is a large number
it is taken to be the actual block size in chars.
.endhelp --------------------------------------------------------------------

define	LEN_BFIO	10
define	BF_CHAN		Memi[$1]		# OS channel
define	BF_ACMODE	Memi[$1+1]		# access mode
define	BF_BUFP		Memi[$1+2]		# buffer pointer
define	BF_BUFSIZE	Memi[$1+3]		# buffer capacity, chars
define	BF_BUFCHARS	Memi[$1+4]		# amount of data in buffer
define	BF_BUFOFFSET	Memi[$1+5]		# file offset of buffer
define	BF_FILEOFFSET	Memi[$1+6]		# file offset for seq i/o
define	BF_UPDATE	Memi[$1+7]		# write buffer to disk
define	BF_BLKSIZE	Memi[$1+8]		# device block size

define	SZ_RANBUF	2048			# SPP chars
define	SZ_SEQBUF	131072
define	READ		0
define	WRITE		1


# BFOPEN -- Fortran callable version of BFOPNX.

int procedure bfopen (fname, acmode, advice)

%	character*(*) fname
int	acmode			# SPP access mode, as in FIO
int	advice			# seq. or random, or bufsize in chars

char	sppname[SZ_PATHNAME]
pointer	bfopnx()

begin
	call f77upk (fname, sppname, SZ_PATHNAME)
	return (bfopnx (sppname, acmode, advice))
end


# BFALOC -- Fortran callable version of BFALCX.

procedure bfaloc (fname, nchars, status)

%	character*(*) fname
int	nchars			# size of file to be allocated
int	status			# receives status

char	sppname[SZ_PATHNAME]

begin
	call f77upk (fname, sppname, SZ_PATHNAME)
	call strpak (sppname, sppname, SZ_PATHNAME)
	call zfaloc (sppname, nchars * SZB_CHAR, status)
end


# BFOPNX -- Open a binary file (SPP version).

pointer procedure bfopnx (fname, acmode, advice)

char	fname[ARB]		# HOST filename
int	acmode			# SPP access mode, as in FIO
int	advice			# seq. or random, or bufsize in chars

pointer	bp, fp
long	blksize
char	osfn[SZ_PATHNAME]
int	chan, bufsize
int	bfmode()
errchk	malloc

begin
	# Open or create the file.
	call strpak (fname, osfn, SZ_PATHNAME)
	call zopnbf (osfn, bfmode(acmode), chan)
	if (chan == ERR)
	    return (ERR)

	# Allocate and initialize file descriptor and i/o buffer.
	call malloc (fp, LEN_BFIO, TY_STRUCT)

	# Pick a buffer size.
	if (advice == RANDOM)
	    bufsize = SZ_RANBUF
	else if (advice == SEQUENTIAL)
	    bufsize = SZ_SEQBUF
	else
	    bufsize = advice

	call zsttbf (chan, FSTT_BLKSIZE, blksize)
	blksize = blksize / SZB_CHAR
	bufsize = (bufsize + blksize - 1) / blksize * blksize
	call malloc (bp, bufsize, TY_CHAR)

	BF_CHAN(fp)       = chan
	BF_ACMODE(fp)     = acmode
	BF_BUFP(fp)       = bp
	BF_BUFSIZE(fp)    = bufsize
	BF_BUFCHARS(fp)   = 0
	BF_BUFOFFSET(fp)  = 0
	BF_FILEOFFSET(fp) = 1
	BF_UPDATE(fp)     = NO
	BF_BLKSIZE(fp)    = blksize

	return (fp)
end


# BFCLOS -- Close a BFIO binary file.

procedure bfclos (fp, status)

pointer	fp			# BFIO file descriptor
int	status
int	bfflsh()

begin
	if (BF_UPDATE(fp) == YES) {
	    status = bfflsh (fp)
	    if (status == ERR)
		return
	}

	call zclsbf (BF_CHAN(fp), status)
	call mfree (BF_BUFP(fp), TY_CHAR)
	call mfree (fp, TY_STRUCT)
end


# BFALCX -- Allocate a fixed size binary file.

procedure bfalcx (fname, nchars, status)

char	fname[ARB]		# HOST filename
int	nchars			# size of file to be allocated
int	status			# receives status

char	osfn[SZ_PATHNAME]

begin
	call strpak (fname, osfn, SZ_PATHNAME)
	call zfaloc (osfn, nchars * SZB_CHAR, status)
end


# BFBSIZ -- Return the device block size in chars.

int procedure bfbsiz (fp)

pointer	fp			# BFIO file descriptor

begin
	return (BF_BLKSIZE(fp))
end


# BFFSIZ -- Return the file size in chars.

int procedure bffsiz (fp)

pointer	fp			# BFIO file descriptor
int	nbytes

begin
	call zsttbf (BF_CHAN(fp), FSTT_FILSIZE, nbytes)
	if (nbytes == ERR)
	    return (ERR)
	else
	    return ((nbytes + SZB_CHAR-1) / SZB_CHAR)
end


# BFCHAN -- Return the channel of the file.

int procedure bfchan (fp)

pointer	fp			# BFIO file descriptor

begin
	return (BF_CHAN(fp))
end


# BFREAD -- Read an arbitrary number of chars from a binary file at an
# arbitrary offset.

int procedure bfread (fp, buf, nchars, offset)

pointer	fp			# BFIO file descriptor
char	buf[ARB]		# user data buffer
int	nchars			# nchars of data to be read
long	offset			# file offset

pointer	bp
long	off, off1, off2
int	ip, op, nleft, chunk
int	bffill()

begin
	off1  = BF_BUFOFFSET(fp)
	off2  = off1 + BF_BUFCHARS(fp)
	off   = offset
	nleft = nchars
	op    = 1
	bp    = BF_BUFP(fp)

	while (nleft > 0) {
	    # Fault in new buffer if file offset falls outside current buffer.
	    if (off1 <= 0 || off < off1 || off >= off2)
		if (bffill (fp, off, nleft, READ) == ERR)
		    return (ERR)
		else {
		    off1 = BF_BUFOFFSET(fp)
		    off2 = off1 + BF_BUFCHARS(fp)
		}

	    # Return as much data as possible from the current buffer and
	    # advance all the pointers when done.

	    ip = off - off1
	    chunk = min (nleft, BF_BUFCHARS(fp) - ip)
	    if (chunk <= 0)
		break
	    call amovc (Memc[bp+ip], buf[op], chunk)

	    nleft = nleft - chunk
	    off   = off + chunk
	    op    = op + chunk
	}

	if (nleft >= nchars)
	    return (EOF)
	else
	    return (nchars - nleft)
end


# BFWRIT -- Write an arbitrary number of chars to a binary file at an
# arbitrary offset.

int procedure bfwrit (fp, buf, nchars, offset)

pointer	fp			# BFIO file descriptor
char	buf[ARB]		# user data buffer
int	nchars			# nchars of data to be written
long	offset			# file offset

pointer	bp
long	off, off1, off2
int	ip, op, nleft, chunk
int	bffill()

begin
	off1  = BF_BUFOFFSET(fp)
	off2  = off1 + BF_BUFSIZE(fp)
	off   = offset
	nleft = nchars
	ip    = 1
	bp    = BF_BUFP(fp)

	while (nleft > 0) {
	    # Fault in new buffer if file offset falls outside current buffer.
	    if (off1 <= 0 || off < off1 || off >= off2)
		if (bffill (fp, off, nleft, WRITE) == ERR)
		    return (ERR)
		else {
		    off1 = BF_BUFOFFSET(fp)
		    off2 = off1 + BF_BUFSIZE(fp)
		}

	    # Move as much data as possible into the current buffer and
	    # advance all the pointers when done.

	    op = off - off1
	    chunk = min (nleft, BF_BUFSIZE(fp) - op)
	    call amovc (buf[ip], Memc[bp+op], chunk)
	    BF_BUFCHARS(fp) = max (BF_BUFCHARS(fp), off+chunk - off1)
	    BF_UPDATE(fp) = YES

	    nleft = nleft - chunk
	    off   = off + chunk
	    ip    = ip + chunk
	}

	return (nchars)
end


# BFRSEQ -- Sequential read from a file.  Successive reads advance through
# the file.

int procedure bfrseq (fp, buf, nchars)

pointer	fp			#I BFIO file descriptor
char	buf[ARB]		#I user data buffer
int	nchars			#I nchars of data to be read

int	status
int	bfread()

begin
	status = bfread (fp, buf, nchars, BF_FILEOFFSET(fp))
	if (status > 0)
	    BF_FILEOFFSET(fp) = BF_FILEOFFSET(fp) + status

	return (status)
end


# BFWSEQ -- Sequential write to a file.  Successive writes advance through
# the file.

int procedure bfwseq (fp, buf, nchars)

pointer	fp			#I BFIO file descriptor
char	buf[ARB]		#O user data buffer
int	nchars			#I nchars of data to be written

int	status
int	bfwrit()

begin
	status = bfwrit (fp, buf, nchars, BF_FILEOFFSET(fp))
	if (status > 0)
	    BF_FILEOFFSET(fp) = BF_FILEOFFSET(fp) + status

	return (status)
end


# BFSEEK -- Set the file offset for sequential i/o using bf[rw]seq.
# If called as bfseek(fp,0) the current file offset is returned without
# changing the file position.

int procedure bfseek (fp, offset)

pointer	fp			#I BFIO file descriptor
int	offset			#I desired file offset (1-indexed)

int	bffsiz()
int	old_offset

begin
	old_offset = BF_FILEOFFSET(fp)

	switch (offset) {
	case BOF:
	    BF_FILEOFFSET(fp) = 1
	case EOF:
	    BF_FILEOFFSET(fp) = bffsiz(fp) + 1
	default:
	    if (offset > 0)
		BF_FILEOFFSET(fp) = offset
	}

	return (old_offset)
end


# BFFILL -- Move the BFIO buffer so that it contains the indicated offset.
# Flush the buffer to disk first if it has been written into.

int procedure bffill (fp, offset, nchars, rwflag)

pointer	fp			# BFIO descriptor
long	offset			# desired file offset
int	nchars			# nchars that will be read/written later
int	rwflag			# read or write when we return?

long	bufoff
int	status, bufsize
int	bfflsh()

begin
	if (BF_UPDATE(fp) == YES)
	    if (bfflsh (fp) == ERR)
		return (ERR)

	bufsize = BF_BUFSIZE(fp)
	bufoff = ((offset - 1) / bufsize) * bufsize + 1
	BF_BUFOFFSET(fp) = bufoff

	# If we are being called prior to a write, and the entire buffer
	# is being written into, there is no point in filling the buffer
	# from the file.  Also, if the file is open WRITE_ONLY, we do not
	# read from the file.

	if ((BF_ACMODE(fp) == WO) ||
	    (offset == bufoff && nchars >= bufsize && rwflag == WRITE))
	    return (nchars)

	# Fill the buffer from the file.
	call zardbf (BF_CHAN(fp), Memc[BF_BUFP(fp)], BF_BUFSIZE(fp) * SZB_CHAR,
	    (bufoff - 1) * SZB_CHAR + 1)
	call zawtbf (BF_CHAN(fp), status)

	if (status == ERR)
	    return (ERR)

	BF_BUFCHARS(fp) = status / SZB_CHAR
	return (BF_BUFCHARS(fp))
end


# BFFLSH -- Flush the BFIO buffer.

int procedure bfflsh (fp)

pointer	fp				# BFIO file descriptor
int	status

begin
	if (BF_UPDATE(fp) == NO)
	    return (OK)
	else
	    BF_UPDATE(fp) = NO

	# Flush the buffer to the file.
	call zawrbf (BF_CHAN(fp), Memc[BF_BUFP(fp)], BF_BUFCHARS(fp) * SZB_CHAR,
	    (BF_BUFOFFSET(fp) - 1) * SZB_CHAR + 1)
	call zawtbf (BF_CHAN(fp), status)

	if (status == ERR)
	    return (ERR)
	else
	    return (status / SZB_CHAR)
end


# BFMODE -- Map the IMFORT/BFIO access mode into the file access mode
# expected by the IRAF kernel.

int procedure bfmode (acmode)

int	acmode			# IMFORT access mode

begin
	switch (acmode) {
	case RO:
	    return (READ_ONLY)
	case WO:
	    return (WRITE_ONLY)
	case RW:
	    return (READ_WRITE)
	case NF:
	    return (NEW_FILE)
	default:
	    return (READ_ONLY)
	}
end