; **************
; File IO Macros
; **************
; ---------------------------------------------------------------------
; create a new file with read / write access and return the file handle
; ---------------------------------------------------------------------
fcreate MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
; ------------------
; delete a disk file
; ------------------
fdelete MACRO filename
invoke DeleteFile,reparg(filename)
EXITM <eax>
ENDM
; ------------------------------
; flush open file buffer to disk
; ------------------------------
fflush MACRO hfile
invoke FlushFileBuffers,hfile
ENDM
; -------------------------------------------------------------------------
; open an existing file with read / write access and return the file handle
; -------------------------------------------------------------------------
fopen MACRO filename
invoke CreateFile,reparg(filename),GENERIC_READ or GENERIC_WRITE,
NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
EXITM <eax> ;; return file handle
ENDM
; ------------------
; close an open file
; ------------------
fclose MACRO arg:REQ
invoke CloseHandle,arg
ENDM
; ------------------------------------------------
; read data from an open file into a memory buffer
; ------------------------------------------------
fread MACRO hFile,buffer,bcnt
LOCAL var
.data?
var dd ?
.code
invoke ReadFile,hFile,buffer,bcnt,ADDR var,NULL
mov eax, var
EXITM <eax> ;; return bytes read
ENDM
; ----------------------------------------
; write data from a buffer to an open file
; ----------------------------------------
fwrite MACRO hFile,buffer,bcnt
LOCAL var
.data?
var dd ?
.code
invoke WriteFile,hFile,buffer,bcnt,ADDR var,NULL
mov eax, var
EXITM <eax> ;; return bytes written
ENDM
; ----------------------------------------------------
; write a line of zero terminated text to an open file
; ----------------------------------------------------
fprint MACRO hFile:REQ,text:VARARG ;; zero terminated text
LOCAL var
LOCAL pst
.data?
var dd ?
pst dd ?
.code
mov pst, repargv(text)
invoke WriteFile,hFile,pst,len(pst),ADDR var,NULL
invoke WriteFile,hFile,chr$(13,10),2,ADDR var,NULL
ENDM
; ---------------------------------
; write zero terminated text with C
; style formatting to an open file.
; ---------------------------------
fprintc MACRO hFile:REQ,text:VARARG ;; zero terminated text
LOCAL var
LOCAL pst
.data?
var dd ?
pst dd ?
.code
mov pst, cfm$(text)
invoke WriteFile,hFile,pst,len(pst),ADDR var,NULL
ENDM
; ------------------------------------
; set the position of the file pointer
; ------------------------------------
fseek MACRO hFile,distance,location
IFIDN <location>,<BEGIN>
var equ <FILE_BEGIN>
ELSEIFIDN <location>,<CURRENT>
var equ <FILE_CURRENT>
ELSEIFIDN <location>,<END>
var equ <FILE_END>
ELSE
var equ <location>
ENDIF
invoke SetFilePointer,hFile,distance,0,var
EXITM <eax> ;; return current file offset
ENDM
; ------------------------------------------------
; set end of file at current file pointer location
; ------------------------------------------------
fseteof MACRO hFile
invoke SetEndOfFile,hFile
ENDM
; -------------------------------
; return the size of an open file
; -------------------------------
fsize MACRO hFile
invoke GetFileSize,hFile,NULL
EXITM <eax>
ENDM
; ---------------------------------------
; extended formatting version writes text
; to the current file pointer location
; ---------------------------------------
ftext MACRO hFile:REQ,args:VARARG
push esi ;; preserve ESI
mov esi, alloc(16384) ;; allocate 16k of buffer
catargs ftext,esi,args ;; write ALL args to a single string
push eax ;; make 4 bytes on the stack
invoke WriteFile,hFile,esi,len(esi),esp,NULL
pop eax ;; release the 4 bytes
free esi ;; free the memory buffer
pop esi ;; restore ESI
ENDM