	title	Truncate lines in a file
	name	('TRUNC80')

	maclib	base80

; Program writes truncated lines (max. 80 characters per line)
; to another file

; Call it :	TRUNC80 filespec1 filespec2

; Where filespec1 is the source file to be truncated and
; filespec2 is the destination file.
; filespecs must be different

; Copyright (C)	Werner Cirsovius
;		Hohe Weide 44
;		D-2000 Hamburg 20
;		Phone: 040/4223247

;		Version 1.0, May 1987

; ===== Externals =====

	ext	chnini,chneof,chnout,chncls,delete,string,comp3

; ===== Constants =====

chnrd	equ	0		; Read channel#
chnwr	equ	1		; Write channel#
rdmod	equ	0		; Read mode word
wrmod	equ	2		; Write mode word
trunc	equ	80		; Max characters per line

; ===== start the program =====

	ld	sp,loc		; Get our stack

	ld	hl,fcb+16
	ld	de,fcbown
	ld	bc,16
	ldir			; Save 2nd file name

	ld	de,filnam
	ld	a,(fcbnam)
	cp	' '		; Test file name
	jp	z,comstr	; None
	ld	a,(fcbnam+16)
	cp	' '
	jp	z,comstr	; dito

	ld	hl,fcb
	ld	de,fcbown
	ld	c,12
	call	comp3		; Test same file specs
	ld	de,same
	jp	z,comstr	; Yes, abort

	ld	de,rdpar
	ld	c,chnrd
	ld	b,rdmod
	call	chnini		; Open source file
	ld	de,filno
	jp	c,comstr	; None

	ld	de,wrpar
	ld	c,chnwr
	ld	b,wrmod
	call	chnini		; Create destination file
	ld	de,dsker
	jp	c,comstr	; Impossible

	ld	e,0		; Reset character counter
loop:
	ld	c,chnrd
	call	chneof		; Get character
	jp	c,fileof	; Aha, end of file
	cp	' '
	jp	c,doctrl	; Test control
	jp	z,doblnk	; Test blank
wrchnc:
	inc	e		; Bump counter
wrchnl:
	call	wrchn		; Write character
	jp	nc,loop		; Loop on
wrerr:
	ld	de,filwr	; Write error
	jp	comdel
doctrl:
	cp	cr		; Test Carriage Return
	jp	z,rescnt	; Reset counter if so
	cp	lf		; Test Line feed
	jp	z,rescnt
	cp	tab		; Test tab
	jp	nz,wrchnl	; No, go on
	ld	a,e		; Get count
	and	000000111b
	ld	c,a
	ld	a,8
	sub	c		; Get fix
	add	a,e
	ld	e,a
	ld	a,tab
	jp	wrchnl
rescnt:
	ld	e,0		; Else reset counter
	jp	wrchnl		; And write cr or lf
doblnk:
	ld	a,e
	cp	trunc		; Test max
	ld	a,' '
	jp	c,wrchnc	; No, save
	ld	a,cr
	call	wrchn		; Close line
	jp	c,wrerr
	ld	e,0
	ld	a,lf
	jp	wrchnl		; Write lf
fileof:
	ld	c,chnwr
	call	chncls		; Close channel
	ld	de,canclo
	jp	c,comdel
	ld	de,ok
comstr:
	call	string		; Tell success or error
	jp	warm		; Bye, bye
comdel:
	push	de
	ld	de,fcbown
	call	delete		; Delete destination file
	pop	de
	jp	comstr
wrchn:
	ld	c,chnwr
	jp	chnout		; write character to channel

	dseg

; ===== Messages =====

filno:
	db	'Input file not found',cr,lf,eot
dsker:
	db	'Cannot create output fiel',cr,lf,eot
filwr:
	db	'Error writing file',cr,lf,eot
canclo:
	db	'Cannot close file',cr,lf,eot
same:
	db	'Same filespecs unallowed',cr,lf,eot
filnam:
	db	'File name(s) missing',cr,lf,eot
ok:
	db	'Truncation completed',cr,lf,eot

; ===== Variable and parameter areas =====

rdpar:
	dw	dma
	db	reclng
	dw	fcb
	db	-1
wrpar:
	dw	dmaown
	db	0
	dw	fcbown
	db	-1

fcbown:
	ds	36		; Destination FCB
dmaown:
	ds	reclng		; Destination DMA

	ds	32*2		; 32 level stack

loc:	end


