include	clib.inc
ifdef __TE__
include	io.inc
include	dos.inc
include	iost.inc
include	string.inc
include	stdio.inc
include	conio.inc
include	tinfo.inc

	public	tiread	; AX: tinfo

_DATA	SEGMENT

	extrn	cp_linetolong:	BYTE

cp_filetolong	db 'File too big, no more memory',0

_DATA	ENDS

_TEXT	SEGMENT

tilineerror PROC
	push	dx
	call	oungetc
	test	[si.ti_flag],_T_MODIFIED
	jnz	tilineerror_end
	or	[si.ti_flag],_T_MODIFIED
	mov	ax,offset cp_linetolong
	call	@ermsg
    tilineerror_end:
	pop	dx
	ret
tilineerror ENDP

tireaderror PROC
	mov	ax,offset cp_filetolong
	call	@ermsg
	ret
tireaderror ENDP

tiiost	PROC
	push	ax
	invoke	memzero, ds::bx, SIZE S_IOST
	pop	ax
	mov	[bx.ios_file],ax
	mov	[bx.ios_size],2048
	mov	word ptr [bx.ios_bp],offset _bufin + 2048
	mov	word ptr [bx.ios_bp+2],ds
	ret
tiiost	ENDP

tiread	PROC
	push	si	; AX tinfo
	push	di
	push	bp
	mov	si,ax
	invoke	openfile,[si.ti_file],M_RDONLY,A_OPEN
	inc	ax
	mov	bp,ax
	jz	tiread_end
	dec	ax
	mov	bx,OSTDI
	call	tiiost
	invoke	getftime,ax
	mov     [si.ti_time],ax
	mov     [si.ti_date],dx
	invoke	lseek, STDI.ios_file, 0, SEEK_END
	stom	[si.ti_size]
	invoke	lseek, STDI.ios_file, 0, SEEK_SET
	xor	ax,ax
	mov	bp,ax
	call	tigetline
	jz	tiread_error
	assertt	ax,ax,jz,"tiread"
	mov	di,bp
    tiread_read:
	call	ogetc
	mov	es,dx
	jz	tiread_eof
	or	al,al
	jz	tiread_line	; binary file
	cmp	al,0Dh
	je	tiread_read
	cmp	al,0Ah
	je	tiread_line
	cmp	al,09h
	je	tiread_tab
	cmp	di,[si.ti_bcol]
	je	tiread_maxlen
	stosb
	jmp	tiread_read
    tiread_tab:
	stosb
	mov	al,TITABCHAR
    tiread_tloop:
	test	di,7
	jz	tiread_read
	cmp	di,[si.ti_bcol]
	je	tiread_maxlen
	stosb
	jmp	tiread_tloop
    tiread_maxlen:			; line to big..
	mov	es:[di-1],ah
	call    tilineerror
    tiread_line:
	inc	bp
	mov	ax,bp
	call	tigetline
	jz	tiread_tobig
	xor	di,di
	jmp	tiread_read
    tiread_tobig:
	call	tireaderror		; file to big..
    tiread_error:
	mov	bp,-1
    tiread_eof:
	invoke	close, STDI.ios_file
	inc	bp
	mov	[si.ti_lcnt],bp
    tiread_end:
;	mov	STDI.ios_size,ISIZE
	mov	ax,bp
	or	ax,ax
	pop	bp
	pop	di
	pop	si
	ret
tiread	ENDP

_TEXT	ENDS
endif
	END
