;	ReadFileIn

;	Reads an entire file (with < 10000 bytes) into a buffer area

;	Preconditions:		DX must hold offset of file spec
;						BX must hold offset of the buffer
;	Postcondition:		AX holds the actual number of bytes read

ReadFileIn	proc
		pusha
		mov			adname$$, dx		; filename offset
		mov			where$$, bx			; buffer offset
		mov			ah, 1ah				; set up dta
		mov			dx, offset indta$$
		int			21h
		mov			ah, 4eh				; fill dta
		mov			cx, 0
		mov			dx, adname$$
		int			21h
		jc			error$$
		mov			ax, word ptr indta$$+26
		cmp			ax, 10000			; check for too large
		jg			big$$
		mov			bytekt$$, ax		; save number of bytes
		mov			ah, 3dh				; open file
		mov			al, 0
		mov			dx, adname$$
		int			21h
		jc			error$$
		mov			inhandle$$, ax		; save file handle
		mov			ah, 3fh				; read whole file
		mov			bx, inhandle$$
		mov			cx, bytekt$$
		mov			dx, where$$
		int			21h
		jc			error$$
		cmp			ax, cx				; make sure all read
		jne			error$$
		mov			ah, 3eh				; close file
		mov			bx, inhandle$$
		int			21h
		popa
		mov			ax, bytekt$$
		ret
		.data
adname$$	dw		?				; offset filespce
where$$		dw		?				; offset buffer
bytekt$$	dw		?				; byte count for file
inhandle$$	dw		?				; file handle
indta$$		db		43 dup(?)		; disk transfer address area
errcode$$	dw		?				; error indicator
prob$$		db		13,10,"Some problem with original file.",13,10,0
size$$		db		13,10,"File is too large.",13,10,0
		.code
big$$:	mov		dx, offset size$$		; complain if file too big
		call	Writestring
error$$:
		mov			dx, offset prob$$	; display error note
		call		Writestring
		mov			errcode$$, ax
		mov			ax, 4c00h
		int			21h
ReadFileIn	endp

;	WriteFileOut

;	Writes an entire file from a buffer

;	Preconditions:	DX must hold the offset of the file spec
;					CX must hold the offset of the buffer
;					BX must hold the number of bytes to write

WriteFileOut	proc
		pusha
		mov			destin$$, dx		; offset filename
		mov			source$$, cx		; offset out buffer
		mov			numbytes$$, bx		; byte count
		mov			ah, 3ch				; create or replace file
		mov			cx, 0
		mov			dx, destin$$
		int			21h
		jc			outerr$$
		mov			outhand$$, ax		; save handle
		mov			ah, 40h				; write whole file
		mov			bx, outhand$$
		mov			cx, numbytes$$
		mov			dx, source$$
		int			21h
		mov			ah, 3eh				; close file
		mov			bx, outhand$$
		int			21h
		popa
		ret
		.data
destin$$	dw		?			; offset file spec
source$$	dw		?			; offset of output buffer
numbytes$$	dw		?			; number of byte to write
outhand$$	dw		?			; output file handle
wrong$$		dw		?			; error indicator
oerr$$		db		13,10,"Some problem in creating new file.",13,10,0
		.code
outerr$$:
		mov			dx, offset oerr$$	; display standard message
		call		Writestring
		mov			wrong$$, ax
		mov			ax, 4c00h
		int			21h
WriteFileOut	endp
