1 ;-----------------------------------------------------------------------
\r
4 ; Sequential binary file I/O functions
\r
6 ; Some functions based on a r.g.p post by Joshua Jensen
\r
12 ; ****** XLIB - Mode X graphics library ****************
\r
13 ; ****** ****************
\r
14 ; ****** Written By Themie Gouthas ****************
\r
16 ; egg@dstos3.dsto.gov.au
\r
17 ; teg@bart.dsto.gov.au
\r
18 ;-----------------------------------------------------------------------
\r
31 _file_err dw (?) ; error value
\r
47 LDS_M macro arg1,arg2
\r
51 mov &arg1&,word ptr &arg2&
\r
57 ;****************************************************************
\r
63 ; extern int f_open(char * filename, char access)
\r
65 ; Opens a file according to the access char:
\r
67 ; 0 = read only - If doesnt exist return error
\r
68 ; 1 = write only - If doesnt exist create it otherwise clear it
\r
69 ; 2 = read/write - If doesnt exist create it
\r
71 ; Returns the file handle on success, -1 on failure
\r
76 ARG filename:dword,access:byte
\r
78 ARG filename:word,access:byte
\r
80 push bp ; Preserve caller's stack frame
\r
83 LDS_M dx,[filename] ; point DS:DX to file name string
\r
88 mov ah,3dh ; select "open file" DOS service
\r
89 mov al,[access] ; select access type code
\r
90 int 21h ; call DOS service
\r
91 jnb @@Done ; If carry flag set we have failed
\r
97 mov ah,3ch ; select "creat file" DOS service
\r
99 int 21h ; call DOS service
\r
100 jnb @@Done ; If carry flag set we have failed
\r
103 mov ax,-1 ; indicate failure
\r
104 @@Done: ; otherwise return file handle
\r
106 pop bp ;restore caller's stack frame
\r
111 ;****************************************************************
\r
117 ; extern int f_close(int handle)
\r
119 ; Closes the file associated with the specified handle
\r
121 ; Returns 0 on success, -1 on failure
\r
125 push bp ; Preserve caller's stack frame
\r
128 mov [_file_err],0 ; Clear error
\r
129 mov ah,3eh ; select "close file handle" DOS service
\r
130 mov bx,[handle] ; select handle of file to close
\r
131 int 21h ; call DOS service
\r
132 jnb @@Fix ; failed if carry flag set
\r
133 mov [_file_err],ax;
\r
134 mov ax,-1 ; return error
\r
137 xor ax,ax ; return 0
\r
139 pop bp ;restore caller's stack frame
\r
144 ;****************************************************************
\r
150 ; extern int f_read(int handle, char *buffer, int count)
\r
152 ; Reads a block of count bytes from the file specified by the handle
\r
155 ; Returns count on success, failure is detectable via _file_err
\r
159 ARG handle:word,buffer:dword,count:word
\r
161 ARG handle:word,buffer:word,count:word
\r
163 push bp ; Preserve caller's stack frame
\r
167 mov [_file_err],0 ; Clear error
\r
168 mov ah,3fh ; select "read from file or device" DOS service
\r
169 mov bx,[handle] ; select handle of file to close
\r
172 int 21h ; call DOS service
\r
173 jnb @@Done ; failed if carry flag set
\r
175 xor ax,ax ; return error
\r
179 pop bp ;restore caller's stack frame
\r
183 ;****************************************************************
\r
189 ; extern int f_write(int handle, char *buffer, int count)
\r
191 ; Writes a block of count bytes to the file specified by the handle
\r
194 ; Returns count on success, error is indicated by _file_err iff count = 0
\r
198 ARG handle:word,buffer:dword,count:word
\r
200 ARG handle:word,buffer:word,count:word
\r
202 push bp ; Preserve caller's stack frame
\r
206 mov [_file_err],0 ; Clear error
\r
207 mov ah,40h ; select "write to file or device" DOS service
\r
208 mov bx,[handle] ; select handle of file to write
\r
211 int 21h ; call DOS service
\r
212 jnb @@Done ; has the function failed ?
\r
214 xor ax,ax ; yes, return error
\r
216 @@Done: ; otherwise return bytes written
\r
218 pop bp ; restore caller's stack frame
\r
222 ;****************************************************************
\r
228 ; extern int f_readfar(int handle, char far * buffer, int count)
\r
230 ; Reads a block of count bytes from the file specified by the handle
\r
233 ; Returns count on success, failure is detectable via _file_err
\r
236 ARG handle:word,buffer:dword,count:word
\r
237 push bp ; Preserve caller's stack frame
\r
241 mov [_file_err],0 ; Clear error
\r
242 mov ah,3fh ; select "read from file or device" DOS service
\r
243 mov bx,[handle] ; select handle of file to close
\r
246 int 21h ; call DOS service
\r
247 jnb @@Done ; failed if carry flag set
\r
249 xor ax,ax ; return error
\r
253 pop bp ;restore caller's stack frame
\r
257 ;****************************************************************
\r
263 ; extern int f_writefar(int handle, char far * buffer, int count)
\r
265 ; Writes a block of count bytes to the file specified by the handle
\r
268 ; Returns count on success, error is indicated by _file_err iff count = 0
\r
271 ARG handle:word,buffer:dword,count:word
\r
272 push bp ; Preserve caller's stack frame
\r
276 mov [_file_err],0 ; Clear error
\r
277 mov ah,40h ; select "write to file or device" DOS service
\r
278 mov bx,[handle] ; select handle of file to write
\r
281 int 21h ; call DOS service
\r
282 jnb @@Done ; has the function failed ?
\r
284 xor ax,ax ; yes, return error
\r
286 @@Done: ; otherwise return bytes written
\r
288 pop bp ; restore caller's stack frame
\r
294 ;****************************************************************
\r
300 ; extern long int f_seek(int handle, long int position, char method_code)
\r
302 ; Moves the file pointer according to the position and method code
\r
304 ; Returns file pointer position on success, -1 on failure
\r
307 ARG handle:word,position:dword,method_code:byte
\r
308 push bp ; Preserve caller's stack frame
\r
311 mov [_file_err],0 ; Clear error
\r
312 mov ah,42h ; select "move file pointer" DOS service
\r
313 mov bx,[handle] ; select handle of file to close
\r
314 mov al,[method_code]
\r
315 mov cx,word ptr [position+2]
\r
316 mov dx,word ptr [position]
\r
317 int 21h ; call DOS service
\r
318 jnb @@Done ; has the function failed ?
\r
320 mov ax,-1 ; yes, return error
\r
323 @@Done: ; otherwise return bytes written
\r
324 pop bp ; restore caller's stack frame
\r
328 ;****************************************************************
\r
334 ; extern long int f_tell(int handle)
\r
336 ; Returns file pointer position on success, -1 on failure
\r
339 ARG handle:word,position:dword,method_code:byte
\r
340 push bp ; Preserve caller's stack frame
\r
343 mov [_file_err],0 ; Clear error
\r
344 mov ah,42h ; select "move file pointer" DOS service
\r
345 mov bx,[handle] ; select handle of file to close
\r
351 mov ax,-1 ; yes, return error
\r
354 @@Done: ; otherwise return bytes written
\r
355 pop bp ; restore caller's stack frame
\r
360 ;****************************************************************
\r
362 ; name: f_filelength
\r
366 ; extern long int f_filelength(int handle)
\r
368 ; Returns the length of the file associated with the specified handle
\r
370 ; Returns file length on success, -1 on failure
\r
374 LOCAL low:word,high:word=LocalStk
\r
375 push bp ; Preserve caller's stack frame
\r
379 mov [_file_err],0 ; Clear error
\r
381 ; Get ptr's current location in file and save it
\r
383 mov ah,42h ; select "move file pointer" DOS service
\r
384 mov al,1 ; select "from current location" method
\r
385 mov bx,[handle] ; select handle of file to close
\r
393 ; Get ptr's value at end of file
\r
395 mov ah,42h ; select "move file pointer" DOS service
\r
396 mov al,2 ; select "from end of file" method
\r
397 mov bx,[handle] ; select handle of file to close
\r
403 ; Save the results while returning pointer to its previous location
\r
408 mov ah,42h ; select "move file pointer" DOS service
\r
409 mov al,0 ; select "from start of file" method
\r
410 mov bx,[handle] ; select handle of file to close
\r
415 ; restore resultant length
\r
420 jnb @@Done ; Was the operation a success ?
\r
423 mov ax,-1 ; no, return error
\r
425 @@Done: ; otherwise return bytes written
\r
427 pop bp ; restore caller's stack frame
\r