; ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
; ³ (c) Lord Julus - 2000		 Vxtasy #1 - Magazine source code ³
; ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

;===========( Open and read the first lines from the files )=================

readfile proc near			   ;
;	 lea dx, filename		   ; point the filename
	 mov ax, 3d02h			   ; open it
	 int 21h			   ; open file
	 jc error			   ; if error exit...
	 mov handle, ax 		   ; save handle
	 mov bx, ax			   ;
					   ;
	 xor cx, cx			   ;
	 xor dx, dx			   ;
	 mov ax, 4202h			   ;
	 int 21h			   ;
	 lea di, filesize		   ;
	 mov word ptr [di], ax		   ;
	 mov word ptr [di+2], dx	   ;
	 xor cx, cx			   ;
	 xor dx, dx			   ;
	 mov ax, 4200h			   ;
	 int 21h			   ;
					   ;
	 call check_fseek		   ; check the file position
	 lea di, firstlinepos		   ; and save it in the dword
	 call fill_axdx 		   ; filepos
					   ;
	 call read_all_lines		   ; read the necessary no. of lines
					   ;
	 call check_fseek		   ; check file seek again
	 lea di, lastlinepos		   ;
	 call fill_axdx 		   ;
	 jmp no_error			   ;
					   ;
error:					   ;
	 stc				   ;
	 ret				   ;
					   ;
no_error:				   ;
	 clc				   ;
	 ret				   ; return
readfile endp				   ;

;===========( Procedure used to read all the necesary lines )================

read_all_lines proc near		   ;
	 clc				   ;
	 mov linesread, 0		   ;
	 mov cx, 25d-skip_above-skip_below ; how many lines do we need?
	 lea di, buffer 		   ; point the buffer
	 mov count, 0			   ; and initiate counter
	 mov maxposition, 0		   ;
					   ;
get_lines:				   ;
	 lea si, linebuffer		   ; point the line buffer
	 call readline			   ; and read one line
	 jc erro			   ; if no error occured
	 push cx			   ; first save cx
	 mov cx, linelength		   ; then make cx = read line length
	 rep movsb			   ; and move the read line to buffer
	 pop cx 			   ; restore cx
	 loop get_lines 		   ; and get some more...
	 jmp over_erro			   ;
					   ;
erro:					   ; if an error occured, we read over
	 mov overend, 1 		   ; the EOF so we mark this.
	 stc				   ;
					   ;
over_erro:				   ;
					   ;
ready_fill:				   ;
	 mov maxposition, di		   ;
	 cmp di, offset bufferend	   ;
	 jae done_fill			   ;
	 mov ax, 0			   ;
	 stosw				   ;
	 jmp ready_fill 		   ;
					   ;
done_fill:				   ;
	 call check_fseek		   ;
	 lea di, lastlinepos		   ;
	 call fill_axdx 		   ;
	 lea si, buffer 		   ; we point si to the buffer start
	 mov crtpoint, si		   ;
	 ret				   ; and return
read_all_lines endp			   ;

;===========( Procedure to read one line from the file )=====================

readline proc near			   ;
	 pusha				   ; save all regs
	 lea dx, linebuffer		   ; dx points the linebuffer
	 mov crlf, 0			   ; reset crlfs
	 mov linelength, 0		   ; reset linelength
					   ;
read_line_loop: 			   ;
	 mov ah, 3fh			   ; read one byte from file
	 mov cx, 1			   ;
	 mov bx, handle 		   ;
	 int 21h			   ;
	 cmp ax, 0			   ; if ax=0 we have gone over EOF
	 je exit_error			   ;
	 inc count			   ;
	 mov bx, dx			   ; point to dx
	 cmp byte ptr [bx], 0dh 	   ; and check for CR
	 jne not_1			   ;
	 inc crlf			   ; if it is, increase crlf
not_1:					   ;
	 cmp byte ptr [bx], 0ah 	   ; and check of LF
	 jne not_2			   ;
	 inc crlf			   ; if it is, increase crlf
	 cmp crlf, 2			   ; if crlf >= 2 we are done reading
	 je over_read_line		   ; one line...
not_2:					   ;
					   ;
ok_crlf:				   ;
	 inc dx 			   ; otherwise, we increment dx,
	 cmp dx, offset endofline	   ; check for line override
	 jae over_read_line		   ; and read again if possible
	 jmp read_line_loop		   ;
					   ;
over_read_line: 			   ;
	 sub dx, offset linebuffer	   ; we substract the address of the
	 inc dx 			   ; buffer and obtain the length of
	 mov linelength, dx		   ; the line.
	 inc linesread			   ;
	 clc				   ; exit with no error
	 popa				   ;
	 ret				   ;
					   ;
exit_error:				   ;
	 stc				   ; exit with error
	 popa				   ;
	 ret				   ;
readline endp				   ;
;============================================================================