; ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
; ³ (c) Lord Julus - 2000		 Vxtasy #1 - Magazine source code ³
; ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

;==========( Go back CX lines )==============================================

go_back_CX_lines proc near		  ;
	cmp cx, 0			  ; if cx=0 go away...
	je no_back			  ;
					  ;
go_loop:				  ;
	push cx 			  ; save counter
	call go_back_one_line		  ; go back one line
	cmp ax, 0			  ;
	je exit_at_top			  ; we are at top
	pop cx				  ; restore counter
	loop go_loop			  ;
	clc				  ;
	ret				  ;
					  ;
exit_at_top:				  ;
	pop cx				  ;
	stc				  ;
	ret				  ;
					  ;
no_back:				  ;
	ret				  ;
go_back_CX_lines endp			  ;

;==========( Go forward CX lines )===========================================

go_forward_CX_lines proc near		  ;
	cmp cx, 0			  ; if cx=0 go away...
	je no_forward			  ;
					  ;
go_loop_forward:			  ;
	push cx 			  ; save counter
	call go_forward_one_line	  ; go back one line
	jc exit_at_bottom		  ; we are at top
	pop cx				  ; restore counter
	loop go_loop_forward		  ;
	clc				  ;
	ret				  ;
					  ;
exit_at_bottom: 			  ;
	stc				  ;
	ret				  ;
					  ;
no_forward:				  ;
	ret				  ;
go_forward_CX_lines endp		  ;

;==========( Go back one line )==============================================

go_back_one_line proc near		  ;
					  ;
	 lea di, firstlinepos		  ;
	 call comp_dword_zero		  ;
	 jc done_1			  ;
					  ;
	 mov crlf, 0			  ; set number of CR/LFs to 0
	 lea dx, linebuffer		  ;
	 mov _counter, 0		  ; reset counter
					  ;
read_line_above:			  ;
	 mov cx, 1			  ;
	 call readbytes 		  ;
	 jnc _no_error_7		  ;
	 mov miscdata, 0FFh		  ;
					  ;
_no_error_7:				  ;
	 cmp ax, 0			  ;
	 je fix_up			  ;
	 mov bx, dx			  ;
	 cmp byte ptr [bx], 0dh 	  ; check for CR
	 jne done_11			  ;
	 inc crlf			  ; increment crlf
	 cmp crlf, 3			  ; if three CRLFs met we are ready
	 je done_1			  ;
					  ;
done_11:				  ;
	 cmp byte ptr [bx], 0ah 	  ; check for LF
	 jne done_12			  ;
	 inc crlf			  ;
	 cmp crlf, 3			  ;
	 je done_1			  ;
					  ;
done_12:				  ;
	 cmp _counter, 0		  ; if counter=0
	 jne no_strip			  ;
	 cmp crlf, 0			  ;
	 je no_strip			  ; and CRLF<>0 it means we have an
	 dec crlf			  ; empty line exception
					  ;
no_strip:				  ;
	 inc _counter			  ; increment counter
	 push 2 			  ;
	 call go_back_bytes		  ;
	 cmp ax, 0			  ;
;	 jz done_1			  ;
	 jz exit_error_7		  ;
	 jmp read_line_above		  ;
					  ;
fix_up: 				  ;
	 xor cx, cx			  ;
	 xor dx, dx			  ;
	 call move_from_0		  ;
					  ;
done_1: 				  ;
	 call check_fseek		  ; take file position
	 lea di, firstlinepos		  ;
	 call fill_axdx 		  ; and put it into firstlinepos
	 dec crtdispline		  ;
	 mov miscdata, 0		  ;
	 clc				  ;
	 ret				  ;
					  ;
exit_error_7:				  ;
	 xor cx, cx			  ;
	 xor dx, dx			  ;
	 call move_from_0		  ;
	 call check_fseek		  ; take file position
	 lea di, firstlinepos		  ;
	 call fill_axdx 		  ; and put it into firstlinepos
	 mov ax, 0			  ;
	 mov crtdispline, ax		  ;
	 mov miscdata, 0ffh		  ;
	 stc				  ;
	 ret				  ;
go_back_one_line endp			  ;

;============( Go forward one line )=========================================

go_forward_one_line proc near		  ;
	 lea dx, linebuffer		  ;
	 mov crlf, 0			  ;
					  ;
read_line_below:			  ;
	 mov cx, 1			  ;
	 call readbytes 		  ;
	 jc exit_with_error		  ;
	 mov bx, dx			  ;
	 cmp byte ptr [bx], 0dh 	  ; check for CR
	 jne done_21			  ;
	 inc crlf			  ;
done_21:				  ;
	 cmp byte ptr [bx], 0ah 	  ; check for LF
	 jne done_22			  ;
	 inc crlf			  ;
	 cmp crlf, 2			  ;
	 je done_2			  ;
					  ;
done_22:				  ;
	 inc dx 			  ;
	 jmp read_line_below		  ;
					  ;
exit_with_error:			  ;
	 stc				  ;
	 ret				  ;
done_2: 				  ;
	 call check_fseek		  ;
	 lea di, firstlinepos		  ;
	 call fill_axdx 		  ;
	 inc crtdispline		  ;
	 ret				  ;
go_forward_one_line endp		  ;

;============( Read from file )==============================================

readbytes proc near			  ;
	mov ah, 3fh			  ; read function
	mov bx, handle			  ; handle
	int 21h 			  ; read them
	jc set_error_4			  ;
	cmp ax, 0			  ;
	je set_error_4			  ;
	clc				  ; no error
	ret				  ;
					  ;
set_error_4:				  ;
	stc				  ; error
	ret				  ;
readbytes endp				  ;

;============( Move relative to the beginning )==============================

move_from_0 proc near			  ;
	mov bx, handle			  ;
	mov ax, 4200h			  ;
	int 21h 			  ;
	jc set_error_0			  ;
	clc				  ; no error
	ret				  ;
					  ;
set_error_0:				  ;
	stc				  ; error
	ret				  ;
move_from_0 endp			  ;

;============( Move relative to the ending )=================================

move_from_2 proc near			  ;
	mov bx, handle			  ;
	mov ax, 4202h			  ;
	int 21h 			  ;
	jc set_error_2			  ;
	clc				  ; no error
	ret				  ;
					  ;
set_error_2:				  ;
	stc				  ;
	ret				  ; error
move_from_2 endp			  ;

;============( Move relative to the crt. pos. )==============================

move_from_1 proc near			  ;
	mov bx, handle			  ;
	mov ax, 4201h			  ;
	int 21h 			  ;
	jc set_error_1			  ;
	clc				  ; no error
	ret				  ;
					  ;
set_error_1:				  ;
	stc				  ; error
	ret				  ;
move_from_1 endp			  ;

;============( Routine to go back n bytes in the file (on stack) )===========

go_back_bytes proc near 		  ;
	 push bp			  ; save bp
	 mov bp, sp			  ; point the stack
	 pusha				  ; save all regs
	 mov bx, word ptr ss:[bp+04h]	  ; take the nr. of bytes
	 xor dx, dx			  ;
	 xor cx, cx			  ;
	 sub dx, bx			  ;
	 sbb cx, 0			  ;
	 call move_from_1		  ;
	 popa				  ;
	 pop bp 			  ;
	 ret 2				  ; return
go_back_bytes endp			  ;


;==========( Put fileposition in the DX:AX )=================================

check_fseek proc near			  ;
	 push cx			  ;
	 mov bx, handle 		  ;
	 xor cx, cx			  ;
	 xor dx, dx			  ;
	 mov ax, 4201h			  ;
	 int 21h			  ;
	 pop cx 			  ;
	 ret				  ;
check_fseek endp			  ;
;============================================================================