	title	Module from library BASE_7 : FIXREC
	name	('FIXREC')
	maclib	baselib.lib

; Change size of file with variable record length

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-20253 Hamburg
;		Tel.: +49-40-4223247

;		Version 1.0 December 1989

; ENTRY	Reg pair DE points to parameter block
;	Byte 0    Record length (1 .. 255)
;	Byte 1,2  Max record number (1 .. 65535)
;	Byte 3,4  FCB of file
;	Byte 5,6  Pointer to 128 byte buffer
;	Byte 7,8  Pointer to record data
;	Byte 9,10 Last record in old file

; EXIT	Carry set on error, either disk or parameter error
;	New empty (filled with blanks) file created
;	if carry reset, file opened

	entry	fixrec

	extrn	$rndrg,$rndpr,inirec,$rnrec
	extrn	open,create,filsiz,close,trncat

fixrec:
	call	$rndrg		; Get registers
	call	$rndpr		; Test parameter ok
	ret	c
	call	open		; Test file here
	ret	c
	push	hl
	push	de
	call	filsiz		; Get size of file
	ex	de,hl
	ld	hl,($rnrec)	; Get new size
	or	a
	sbc	hl,de		; Compare
	jr	c,trunc		; Truncate if new<old
	ld	($rnrec),hl	; Set count
	pop	de
	pop	hl
	ret	z		; End if new=old
	ld	l,(ix+9)	; Get last top record
	ld	h,(ix+10)
	ld	(inirec-2),hl	; Save it
	ld	e,(ix+1)	; Get top
	ld	d,(ix+2)
	ex	de,hl
	push	hl
	or	a
	sbc	hl,de
	ld	(ix+1),l	; Set new length
	ld	(ix+2),h
	push	ix
	push	ix
	pop	de
	call	inirec		; Write new records
	ld	hl,1
	ld	(inirec-2),hl	; .. reset to record 1
	pop	ix
	pop	hl
	ld	(ix+1),l	; Reset old count
	ld	(ix+2),h
	ret
trunc:
	ld	hl,_RRN
	pop	de
	push	de
	add	hl,de		; Point to random
	ex	de,hl
	ld	hl,($rnrec)	; Get new
	ld	a,h		; Test illegal zero
	or	l
	dec	hl		; Set to last
	ex	de,hl
	ld	(hl),e
	inc	hl
	ld	(hl),d
	pop	de
	pop	hl
	scf
	ret	z		; End on zero
	call	close		; Close file
	call	nc,trncat	; Then truncate
	call	nc,open		; Re-open it
	ret

	end

