Files
Work/fileio/OPENFILE.INC
2024-08-07 09:16:27 -04:00

223 lines
12 KiB
Plaintext

;*************************************************************************************
; MODULE: OPENFILE.INC DATE: FEBRUARY 2, 1997
; AUTHOR: SEAN M. KESSLER
; TARGET: 32 BIT FLAT MODEL
; FUNCTION : FILE HANDLING MACROS
;*************************************************************************************
HANDLE STRUC
PHANDLE TYPEDEF FAR PTR HANDLE
HANDLE@@mHandle DD ?
HANDLE ENDS
INVALID_HANDLE equ 0FFFFFFFFh
CREATEFILE MACRO pathFileName,access,share,open,attribute
push 0000h ; save handle to template (null)
push attribute ; save attributes
push open ; save creation flags
push 0000h ; save security attributes
push share ; save share mode
push access ; save acess mode
push pathFileName ; save pathFileName
call dword ptr __imp__CreateFileA@28 ; call create
ENDM
READFILE MACRO hFileHandle,szBuffer,sizeBuffer
sub esp,04h ; create bytesRead on stack
mov eax,esp ; move bytesRead address to eax
push 0000h ; save overlapped structure address
push eax ; save pointer to bytesRead
push sizeBuffer ; save length of buffer
push szBuffer ; save address of read buffer
push hFileHandle ; save file handle
call dword ptr __imp__ReadFile@20 ; call read
mov eax,[esp] ; move bytesRead value to eax
add esp,04h ; remove bytesRead variable from stack
ENDM
WRITEFILE MACRO hFileHandle,szBuffer,sizeBuffer
sub esp,04h ; create bytesWritten variable on stack
mov eax,esp ; move bytesWritten address to eax
push 0000h ; save overlapped structure address
push eax ; save bytesWritten address
push sizeBuffer ; save write length
push szBuffer ; save address of write buffer
push hFileHandle ; save file handle
call dword ptr __imp__WriteFile@20 ; call write
mov eax,[esp] ; move number of bytes written to eax
add esp,04h ; remove bytesWritten variable from stack
ENDM
CLOSEHANDLE MACRO handle
push handle ; save file handle
call dword ptr __imp__CloseHandle@4 ; call close
ENDM
GETFILESIZE MACRO handle
sub esp,04h ; create pointer to high dword on stack
mov eax,esp ; move pointer to high dword into eax
push eax ; push pointer to high dword onto stack
push handle ; save file handle
call dword ptr __imp__GetFileSize@8 ; call get file size
add esp,04h ; remove pointer to high dword variable from atck
ENDM
SEEK MACRO hFileHandle,offset,whence
push whence ; push move method
push 0000h ; push 0000h (offset high)
push offset ; push offset (offset low)
push hFileHandle ; push hFileHandle
call dword ptr __imp__SetFilePointer@16 ; seek
ENDM
FOPEN MACRO pathFileName,sFileInfo,access,share,open,attribute
CREATEFILE pathFileName,access,share,open,attribute
mov sFileInfo.FileInfo@@mhFileHandle,eax
ENDM
FCLOSE MACRO sFileInfo
LOCAL @@FCLOSEend
cmp sFileInfo.FileInfo@@mhFileHandle,INVALID_HANDLE
je @@FCLOSEend
CLOSEHANDLE sFileInfo.FileInfo@@mhFileHandle
mov sFileInfo.FileInfo@@mhFileHandle,INVALID_HANDLE
@@FCLOSEend:
ENDM
FREAD MACRO sFileInfo,ptrByte
LOCAL @@FREADreadError,@@FREADsimpleRead,@@FREADdone
cmp [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,00h ; is buffer index zero
jne @@FREADsimpleRead ; if not then just grab next byte
READFILE [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,sFileInfo,MaxLength ; otherwise we fill the read buffer
cmp eax,0000h ; did we read any data ?
je @@FREADreadError ; if not then we're done
mov [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,eax ; move number of bytes read to buffer index
lea eax,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer ; load buffer address into eax register
mov [FileInfo ptr [sFileInfo]].FileInfo@@mlpBufferPointer,eax ; move address of buffer to lpBufferPointer
jmp @@FREADsimpleRead ; grab next byte
@@FREADreadError: ; read error sync address
xor eax,eax ; set return code
jmp @@FREADdone ; we're done
@@FREADsimpleRead: ; simple read sync address
mov eax,[FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer ; move address of buffer to eax
mov al,byte ptr[eax] ; move byte value to al
mov byte ptr[ptrByte],al ; move byte value to user buffer
inc [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer ; increment along lpBufferPointer
dec [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex ; decrement the buffer index
mov eax,01h ; set the return code
@@FREADdone: ; done reading sync
ENDM
FWRITE MACRO sFileInfo,ptrByte
LOCAL @@FWRITEsimpleWrite,@@FWRITEuseBuffer
push ebx ; macro uses ebx, must preserve it
cmp [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,MaxLength ; compare buffer index to MaxLength
jl @@FWRITEsimpleWrite ; if it's less then insert char into buffer
lea ebx,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer
WRITEFILE [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,ebx,MaxLength
mov [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,0000h ; set buffer index to zero
lea eax,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer
mov [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer,eax ; move address of buffer to lpBufferPointer
@@FWRITEsimpleWrite: ; simple write sync address
cmp [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer,0000h ; is buffer pointer null?
jne @@FWRITEuseBuffer ; if not then just use it
lea eax,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer
mov [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer,eax ; move address of buffer to lpBufferPointer
@@FWRITEuseBuffer: ; use buffer sync address
mov cl,byte ptr[ptrByte] ; move byte to write to cl
mov eax,[FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer ; move address of buffer to eax
mov byte ptr[eax],cl ; move byte into buffer
inc [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer ; increment buffer pointer
inc [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex ; increment buffer index
pop ebx ; restore ebx register
mov eax,01h ; set return code
ENDM
FREADLINE MACRO sFileInfo,szBuffer
LOCAL @@FGETScont,@@FGETexit,@@FGETsetError,@@FGETScrlf
push edi ; save source index register
mov edi,szBuffer ; move address of buffer into esi
@@FGETScont: ; loop control
FREAD sFileInfo,edi ; read a byte
cmp eax,0000h ; was our read successful?
je @@FGETexit ; if not we're done
cmp byte ptr[edi],0Dh ; is the byte a carriage return?
je @@FGETScrlf ; if yes then jump to handler
cmp byte ptr[edi],0Ah ; some (UNIX) files just have linefeed
je @@FGETSlf ; handle line feed
cmp byte ptr[edi],09h ; some files have embedded tabs
je @@FGETStab ; handle tabs
inc edi ; increment along edi
jmp @@FGETScont ; keep reading
@@FGETSlf: ; line feed handler
mov byte ptr[edi],00h ; move null into line data
jmp @@FGETexit ; jump to exit code
@@FGETStab: ; tab char handler
mov byte ptr[edi],' ' ; replace tabs with spaces
inc edi ; increment along edi
jmp @@FGETScont ; keep reading
@@FGETScrlf: ; carriage return control
mov byte ptr[edi],00h ; move null into line data
sub esp,04h ; create temp var on stack
mov edi,esp ; esi points to temp var
FREAD sFileInfo,edi ; read line-feed into temp var
add esp,04h ; restore stack
@@FGETexit: ; exit sync address
pop edi ; restore source index register
ENDM
FWRITELINE MACRO sFileInfo,szBuffer
LOCAL @@FWRITELINEcont,@@FWRITELINEend
@@FWRITELINEcont:
cmp byte ptr[szBuffer],00h
je @@FWRITELINEend
FWRITE sFileInfo,szBuffer
inc szBuffer
jmp @@FWRITELINEcont
@@FWRITELINEend:
ENDM
FFLUSH MACRO sFileInfo
LOCAL @@FFLUSHexit
cmp [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,INVALID_HANDLE ; valid handle?
je @@FFLUSHexit ; can't flush invalid file
push ebx ; save ebx register
lea ebx,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer ; load buffer address into ebx register
WRITEFILE [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,ebx,[FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex ; write buffer to disk
mov [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,0000h ; set buffer index to zero
mov [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer,ebx ; move address of buffer to lpBufferPointer
pop ebx ; restore ebx register
@@FFLUSHexit:
ENDM
FSEEK MACRO sFileInfo,offset,whence
LOCAL @@FSEEKExit,@@FSEEKErrorExit
cmp [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,INVALID_HANDLE ; valid handle
je @@FSEEKErrorExit ; if the handle is invalid....
mov [FileInfo ptr[sFileInfo]].FileInfo@@mBufferIndex,0000h ; discard the buffer on seek
push esi ; save source index register
lea esi,[FileInfo ptr[sFileInfo]].FileInfo@@mszBuffer ; load buffer address into ebx register
mov [FileInfo ptr[sFileInfo]].FileInfo@@mlpBufferPointer,esi ; move address of buffer to lpBufferPointer
pop esi ; restore source index register
SEEK [FileInfo ptr[sFileInfo]].FileInfo@@mhFileHandle,offset,whence ; seek to location
jmp @@FSEEKExit ; we're all done here
@@FSEEKErrorExit: ; error sync address
xor eax,eax ; clear eax on error
@@FSEEKExit: ; end of macro sync address
ENDM
FileInfo STRUC
MaxLength equ 800h
FileInfo@@mszBuffer DB MaxLength DUP(0) ; this field must appear first
FileInfo@@mhFileHandle HANDLE <INVALID_HANDLE>
FileInfo@@mBufferIndex DD (0)
FileInfo@@mlpBufferPointer DD (0)
FileInfo ENDS
extrn __imp__WriteFile@20:near
extrn __imp__CloseHandle@4:near
extrn __imp__CreateFileA@28:near
extrn __imp__ReadFile@20:near
extrn __imp__GetFileSize@8:near
extrn __imp__SetFilePointer@16:near