; ; $Id: filei.asm 1.4 1994/12/10 00:15:53 rthomas Exp rthomas $ ; ; The Press-Enterprise Co. ; 3512 Fourteenth Street ; Riverside, California 92501-3878 ; (c) Copyright 1992 ; ; Author: Randolph Thomas ; ; $Log: filei.asm $ ; Revision 1.4 1994/12/10 00:15:53 rthomas ; Added SkipWord() ; ; Revision 1.3 1994/12/09 23:38:25 rthomas ; Recovered this silly file. ; ; Revision 1.2 1992/05/14 16:18:59 rthomas ; Fixed bug in FileGotoMark. ; ; Revision 1.1 1992/04/01 19:51:24 rthomas ; Initial revision ; ; Revision 1.1 1992/03/27 21:29:27 rthomas ; Initial revision ; .MODEL small .DATA db "$Id: filei.asm 1.4 1994/12/10 00:15:53 rthomas Exp rthomas $" .FARDATA FileiData Handle dw ? FileSize dw ?,? ;Size of file FilePointer dw ?,? ;Current position within file MarkFilePointer dw ?,? ;saved position, for recall later VB1Contains dw -1,-1 ;The beginning address that each VB2Contains dw -1,-1 ;buffer contains OffsetToUse dw ? ;The offset within the buffer VBufferSize equ 25000 VBuffer1 db VBufferSize dup(?) VBuffer2 db VBufferSize dup(?) .CODE ;----------------------------------------------------------------------- ;Given: ; DS:SI =ASCIIZ filename ;Returns: ; Carry Set =error occurred, AX = error code ; Carry Clear =good ;----------------------------------------------------------------------- OpenFile proc USES ax bx cx dx ds mov ah,03Dh ;Open file mov al,0 ;read only mov dx,si int 21h jc OuttaHere mov dx,seg FileiData mov ds,dx assume ds:FileiData mov [Handle],ax mov bx,ax mov ah,042h ;Set file pointer mov al,2 ;Signed offset from end of file xor cx,cx xor dx,dx int 21h mov [FileSize],ax ;Least Significant mov [FileSize+2],dx ;Most Significant mov [FilePointer],0 mov [FilePointer+2],0 clc OuttaHere: ret OpenFile endp ;----------------------------------------------------------------------- ;Given: ; Nothing ;Returns: ;----------------------------------------------------------------------- CloseFile proc USES ax bx ds mov ax,seg FileiData mov ds,ax mov ah,03Eh ;Close file mov bx,[Handle] cmp bx,-1 ;Was there even an open file? je OuttaHere int 21h mov [Handle],-1 OuttaHere: ret CloseFile endp ;----------------------------------------------------------------------- ;Given: ; Nothing ;Returns: ; Carry set - error occurred ; Carry clear - good ; AX = -1, end of file, otherwise ; AL = byte read ;----------------------------------------------------------------------- ReadNextByte proc USES dx ds mov ax,seg FileiData mov ds,ax cmp [Handle],-1 ;do we have a file? jne @F stc jmp OuttaHere @@: mov ax,[FilePointer] mov dx,[FilePointer+2] call ReadByte cmp ax,-1 je @F inc [FilePointer] jnz @F inc [FilePointer+2] @@: clc OuttaHere: ret ReadNextByte endp ;-------------------------------------------------------------------- ; This routine serves as an internal cache, so that we can ; easily reference bytes by position. ; ;Given: ; DX:AX File position to read from ; DS = FileiData ;Returns: ; AX = -1,if end of file, or ; AL byte requested ;-------------------------------------------------------------------- ReadByte proc private USES bx cx dx si ; push bx ; push cx ; push dx ; push si cmp dx,[FileSize+2] ;Check to see if we're trying jb VRNotTooFar ; to read beyond the file. ja VRTooFar cmp ax,[FileSize] jbe VRNotTooFar VRTooFar: mov ax,-1 jmp VRend VRNotTooFar: mov cx,VBufferSize div cx mov [OffsetToUse],dx xor dx,dx mov cx,VBufferSize mul cx ;DX:AX now contains the buffer ; we want to have. cmp [VB1Contains+2],-1 ;Has the first buffer even been jne VRNope1 ; read in yet? jmp VRBUseVB1 VRNope1: cmp [VB2Contains+2],-1 ;Has the second buffer even been jne VRNope2 ; read in yet? jmp VRBUseVB2 VRNope2: cmp [VB1Contains+2],dx ;Match with buffer 1? jne VCantBe1 cmp [VB1Contains],ax jne VCantBe1 mov bx,offset VBuffer1 jmp VRGetIt VCantBe1: cmp [VB2Contains+2],dx ;Match with buffer 2? jne VCantBe2 cmp [VB2Contains],ax jne VCantBe2 mov bx,offset VBuffer2 jmp VRGetIt VCantBe2: ;Swap-out Policy decision made here cmp dx,[VB1Contains+2] jb VRRequestLess1 ja VRRequestGreat1 cmp ax,[VB1Contains] jb VRRequestLess1 VRRequestGreat1: ;Request is great than 1 cmp dx,[VB2Contains+2] jb VRRequestGreat1Less2 ja VRRequestGreat1Great2 cmp ax,[VB1Contains] jb VRRequestGreat1Less2 VRRequestGreat1Great2: ;Request is greater than both 1 & 2 mov bx,[VB1Contains] mov cx,[VB1Contains+2] cmp cx,[VB2Contains+2] jb VR1Less2 ja VR1Great2 cmp bx,[VB2Contains] jb VR1Less2 VR1Great2: ;Request is greater than both, ; and (1>2), so replace 2 jmp VRBUseVB2 VR1Less2: ;Request is greater than both, ; and (1<2), so replace 1 jmp VRBUseVB1 VRRequestGreat1Less2: jmp VRBUseVB1 VRRequestLess1: ;Request is less than 1 cmp dx,[VB2Contains+2] jb VRRequestLess1Less2 ja VRRequestLess1Great2 cmp ax,[VB2Contains] jb VRRequestLess1Less2 VRRequestLess1Great2: ;Request is less than 1, but is ; greater than 2, so replace 1 jmp VRBUseVB1 VRRequestLess1Less2: ;Request is less than both. mov bx,[VB1Contains] mov cx,[VB1Contains+2] cmp cx,[VB2Contains+2] jb VRRequestLess1Less2_1Less2 ja VRRequestLess1Less2_1Great2 cmp bx,[VB2Contains] jb VRRequestLess1Less2_1Less2 VRRequestLess1Less2_1Great2: ;Request is less than both, and ; (1>2), so replace 2 jmp VRBUseVB2 VRRequestLess1Less2_1Less2: ;Request is less than both, and ; (1<2), so replace 1 jmp VRBUseVB1 VRBUseVB1: mov [VB1Contains+2],dx mov [VB1Contains],ax mov bx,offset VBuffer1 jmp VReadTheBuffer VRBUseVB2: mov [VB2Contains+2],dx mov [VB2Contains],ax mov bx,offset VBuffer2 VReadTheBuffer: mov si,bx ;Temporary save mov cx,dx mov dx,ax mov ah,042h ;Set file pointer mov al,0 ;From absolute beginning mov bx,[Handle] int 21h mov ah,03Fh ;Read from the file mov cx,VBufferSize mov dx,si int 21h jnc VRReadOk ; S_OUT VCantRead VRReadOk: mov bx,si VRGetIt: add bx,[OffsetToUse] mov al,[bx] xor ah,ah VRend: ; pop si ; pop dx ; pop cx ; pop bx ret ReadByte endp ;----------------------------------------------------------------------- ;Given: ; ax =bytes to skip forward ;Returns: ; Nothing ;----------------------------------------------------------------------- SkipWord proc USES dx ds mov dx,seg FileiData mov ds,dx add [FilePointer],ax jnc @F inc [FilePointer+2] @@: ret SkipWord endp ;----------------------------------------------------------------------- ;Given: ; Nothing ;Returns: ; Nothing ;----------------------------------------------------------------------- FileSetMark proc USES ax ds mov ax,seg FileiData mov ds,ax mov ax,[FilePointer] mov [MarkFilePointer],ax mov ax,[FilePointer+2] mov [MarkFilePointer+2],ax ret FileSetMark endp ;----------------------------------------------------------------------- ;Given: ; Nothing ;Returns: ; Nothing ;----------------------------------------------------------------------- FileGotoMark proc USES ax ds mov ax,seg FileiData mov ds,ax mov ax,[MarkFilePointer] mov [FilePointer],ax mov ax,[MarkFilePointer+2] mov [FilePointer+2],ax ret FileGotoMark endp END