;************************************************************************************* ; 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 CreateFileA ; 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 offset szBuffer ; save address of read buffer push hFileHandle ; save file handle call ReadFile ; 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 offset szBuffer ; save address of write buffer push hFileHandle ; save file handle call WriteFile ; 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 CloseHandle ; call close 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 sFileInfo.FileInfo@@mBufferIndex,00h ; is buffer index zero jne @@FREADsimpleRead ; if not then just grab next byte READFILE sFileInfo.FileInfo@@mhFileHandle,sFileInfo.FileInfo@@mszBuffer,MaxLength ; otherwise we fill the read buffer cmp eax,0000h ; did we read any data ? je @@FREADreadError ; if not then we're done mov sFileInfo.FileInfo@@mBufferIndex,eax ; move number of bytes read to buffer index mov eax,offset sFileInfo.FileInfo@@mszBuffer ; move address of buffer to eax mov 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,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 sFileInfo.FileInfo@@mlpBufferPointer ; increment along lpBufferPointer dec 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 cmp sFileInfo.FileInfo@@mBufferIndex,MaxLength ; compare buffer index to MaxLength jl @@FWRITEsimpleWrite ; if it's less then insert char into buffer WRITEFILE sFileInfo.FileInfo@@mhFileHandle,sFileInfo.FileInfo@@mszBuffer,MaxLength ; other wise mov sFileInfo.FileInfo@@mBufferIndex,0000h ; set buffer index to zero mov eax,offset sFileInfo.FileInfo@@mszBuffer ; move address of buffer to eax mov sFileInfo.FileInfo@@mlpBufferPointer,eax ; move address of buffer to lpBufferPointer @@FWRITEsimpleWrite: ; simple write sync address cmp sFileInfo.FileInfo@@mlpBufferPointer,0000h ; is buffer pointer null? jne @@FWRITEuseBuffer ; if not then just use it mov eax,offset sFileInfo.FileInfo@@mszBuffer ; move address of buffer to eax mov 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,sFileInfo.FileInfo@@mlpBufferPointer ; move address of buffer to eax mov byte ptr[eax],cl ; move byte into buffer inc sFileInfo.FileInfo@@mlpBufferPointer ; increment buffer pointer inc sFileInfo.FileInfo@@mBufferIndex ; increment buffer index mov eax,01h ; set return code ENDM FFLUSH MACRO sFileInfo LOCAL @@FFLUSHexit cmp sFileInfo.FileInfo@@mhFileHandle,INVALID_HANDLE je @@FFLUSHexit WRITEFILE sFileInfo.FileInfo@@mhFileHandle,sFileInfo.FileInfo@@mszBuffer,sFileInfo.FileInfo@@mBufferIndex ; write buffer to disk mov sFileInfo.FileInfo@@mBufferIndex,0000h ; set buffer index to zero mov eax,offset sFileInfo.FileInfo@@mszBuffer ; move address of buffer to eax mov sFileInfo.FileInfo@@mlpBufferPointer,eax ; move address of buffer to lpBufferPointer @@FFLUSHexit: ENDM FGETS MACRO sFileInfo,szBuffer LOCAL @@FGETScont,@@FGETexit,@@FGETsetError,@@FGETScrlf push esi ; save source index register mov esi,offset szBuffer ; move address of buffer into esi @@FGETScont: ; loop control FREAD sFileInfo,esi ; read a byte cmp eax,0000h ; was our read successful? je @@FGETexit ; if not we're done cmp byte ptr[esi],0Dh ; is the byte a carriage return? je @@FGETScrlf ; if yes then jump to handler inc esi ; increment along esi jmp @@FGETScont ; keep reading @@FGETScrlf: ; carriage return control mov byte ptr[esi],00h ; move null into line data sub esp,04h ; create temp var on stack mov esi,esp ; esi points to temp var FREAD sFileInfo,esi ; read line-feed into temp var add esp,04h ; restore stack @@FGETexit: ; exit sync address pop esi ; restore source index register ENDM FileInfo STRUC MaxLength equ 800h FileInfo@@mhFileHandle HANDLE FileInfo@@mszBuffer DB MaxLength DUP(0) FileInfo@@mBufferIndex DD (0) FileInfo@@mlpBufferPointer DD (0) FileInfo ENDS extrn WriteFile:near extrn CloseHandle:near extrn CreateFileA:near extrn GetLastError:near extrn ReadFile:near