]> 4ch.mooo.com Git - 16.git/blob - 16/xlib/xfileio.asm
wwww
[16.git] / 16 / xlib / xfileio.asm
1 ;-----------------------------------------------------------------------\r
2 ; MODULE XFILEIO\r
3 ;\r
4 ; Sequential binary file I/O functions\r
5 ;\r
6 ; Some functions based on a r.g.p post by Joshua Jensen\r
7 ;\r
8 ; Compile with Tasm.\r
9 ; C callable.\r
10 ;\r
11 ;\r
12 ; ****** XLIB - Mode X graphics library                ****************\r
13 ; ******                                               ****************\r
14 ; ****** Written By Themie Gouthas                     ****************\r
15 ;\r
16 ; egg@dstos3.dsto.gov.au\r
17 ; teg@bart.dsto.gov.au\r
18 ;-----------------------------------------------------------------------\r
19 COMMENT $\r
20 \r
21 \r
22 $\r
23 \r
24 LOCALS\r
25 .8086\r
26 \r
27 include model.inc\r
28 include xfileio.inc\r
29         .data\r
30 \r
31         _file_err dw (?)  ; error value\r
32 \r
33         .code\r
34 \r
35 PUSH_DS macro\r
36     IFNDEF s\r
37     push  ds\r
38     ENDIF\r
39     endm\r
40 \r
41 POP_DS macro\r
42     IFNDEF s\r
43     pop   ds\r
44     ENDIF\r
45     endm\r
46 \r
47 LDS_M macro arg1,arg2\r
48     IFNDEF s\r
49     lds &arg1&,&arg2&\r
50     ELSE\r
51     mov &arg1&,word ptr &arg2&\r
52     ENDIF\r
53     endm\r
54 \r
55 \r
56 \r
57 ;****************************************************************\r
58 ;\r
59 ; name: f_open\r
60 ;\r
61 ; C Prototype:\r
62 ;\r
63 ;     extern int f_open(char * filename, char access)\r
64 ;\r
65 ; Opens a file according to the access char:\r
66 ;\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
70 ;\r
71 ; Returns the file handle on success, -1 on failure\r
72 ;\r
73 ;\r
74 proc _f_open\r
75 IFNDEF s\r
76   ARG   filename:dword,access:byte\r
77 ELSE\r
78   ARG   filename:word,access:byte\r
79 ENDIF\r
80         push  bp             ; Preserve caller's stack frame\r
81         mov   bp,sp\r
82         PUSH_DS\r
83         LDS_M dx,[filename]  ; point DS:DX to file name string\r
84         mov  [_file_err],0\r
85         cmp   [access],1\r
86         je    @@creat\r
87 \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
92 \r
93 \r
94         cmp   [access],2\r
95         jne   @@error\r
96 @@creat:\r
97         mov   ah,3ch         ; select "creat file" DOS service\r
98         mov   cx,0\r
99         int   21h            ; call DOS service\r
100         jnb   @@Done         ; If carry flag set we have failed\r
101 @@error:\r
102         mov   [_file_err],ax\r
103         mov   ax,-1          ;  indicate failure\r
104 @@Done:                      ; otherwise return file handle\r
105         POP_DS\r
106         pop  bp              ;restore caller's stack frame\r
107         ret\r
108 _f_open endp\r
109 \r
110 \r
111 ;****************************************************************\r
112 ;\r
113 ; name: f_close\r
114 ;\r
115 ; C Prototype:\r
116 ;\r
117 ;     extern int f_close(int handle)\r
118 ;\r
119 ; Closes the file associated with the specified handle\r
120 ;\r
121 ; Returns 0 on success, -1 on failure\r
122 ;\r
123 proc _f_close\r
124 ARG   handle:word\r
125         push bp             ; Preserve caller's stack frame\r
126         mov  bp,sp\r
127 \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
135         jmp  short @@Done\r
136 @@Fix:                      ; otherwise\r
137         xor  ax,ax          ;  return 0\r
138 @@Done:\r
139         pop  bp             ;restore caller's stack frame\r
140         ret\r
141 _f_close endp\r
142 \r
143 \r
144 ;****************************************************************\r
145 ;\r
146 ; name: f_read\r
147 ;\r
148 ; C Prototype:\r
149 ;\r
150 ;     extern int f_read(int handle, char  *buffer, int count)\r
151 ;\r
152 ; Reads a block of count bytes from the file specified by the handle\r
153 ; into the buffer\r
154 ;\r
155 ; Returns count on success, failure is detectable via _file_err\r
156 ;\r
157 proc _f_read\r
158 IFNDEF s\r
159   ARG   handle:word,buffer:dword,count:word\r
160 ELSE\r
161   ARG   handle:word,buffer:word,count:word\r
162 ENDIF\r
163         push bp             ; Preserve caller's stack frame\r
164         mov  bp,sp\r
165         PUSH_DS\r
166 \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
170         mov   cx,[count]\r
171         LDS_M dx,[buffer]\r
172         int   21h            ; call DOS service\r
173         jnb   @@Done         ; failed if carry flag set\r
174         mov   [_file_err],ax\r
175         xor   ax,ax          ;  return error\r
176         jmp   short @@Done\r
177 @@Done:\r
178         POP_DS\r
179         pop  bp             ;restore caller's stack frame\r
180         ret\r
181 _f_read endp\r
182 \r
183 ;****************************************************************\r
184 ;\r
185 ; name: f_write\r
186 ;\r
187 ; C Prototype:\r
188 ;\r
189 ;     extern int f_write(int handle, char *buffer, int count)\r
190 ;\r
191 ; Writes a block of count bytes to the file specified by the handle\r
192 ; from the buffer\r
193 ;\r
194 ; Returns count on success, error is indicated by _file_err iff count = 0\r
195 ;\r
196 proc _f_write\r
197 IFNDEF s\r
198   ARG   handle:word,buffer:dword,count:word\r
199 ELSE\r
200   ARG   handle:word,buffer:word,count:word\r
201 ENDIF\r
202         push bp             ; Preserve caller's stack frame\r
203         mov  bp,sp\r
204         PUSH_DS\r
205 \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
209         mov   cx,[count]\r
210         LDS_M dx,[buffer]\r
211         int   21h            ; call DOS service\r
212         jnb   @@Done         ; has the function failed ?\r
213         mov   [_file_err],ax\r
214         xor   ax,ax          ;  yes, return error\r
215         jmp   short @@Done\r
216 @@Done:                      ; otherwise return bytes written\r
217         POP_DS\r
218         pop  bp              ; restore caller's stack frame\r
219         ret\r
220 _f_write endp\r
221 \r
222 ;****************************************************************\r
223 ;\r
224 ; name: f_readfar\r
225 ;\r
226 ; C Prototype:\r
227 ;\r
228 ;     extern int f_readfar(int handle, char far * buffer, int count)\r
229 ;\r
230 ; Reads a block of count bytes from the file specified by the handle\r
231 ; into the buffer\r
232 ;\r
233 ; Returns count on success, failure is detectable via _file_err\r
234 ;\r
235 proc _f_readfar\r
236 ARG   handle:word,buffer:dword,count:word\r
237         push bp             ; Preserve caller's stack frame\r
238         mov  bp,sp\r
239         push ds\r
240 \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
244         mov  cx,[count]\r
245         lds  dx,[buffer]\r
246         int  21h            ; call DOS service\r
247         jnb  @@Done         ; failed if carry flag set\r
248         mov  [_file_err],ax\r
249         xor  ax,ax          ;  return error\r
250         jmp  short @@Done\r
251 @@Done:\r
252         pop  ds\r
253         pop  bp             ;restore caller's stack frame\r
254         ret\r
255 _f_readfar endp\r
256 \r
257 ;****************************************************************\r
258 ;\r
259 ; name: f_writefar\r
260 ;\r
261 ; C Prototype:\r
262 ;\r
263 ;     extern int f_writefar(int handle, char far * buffer, int count)\r
264 ;\r
265 ; Writes a block of count bytes to the file specified by the handle\r
266 ; from the buffer\r
267 ;\r
268 ; Returns count on success, error is indicated by _file_err iff count = 0\r
269 ;\r
270 proc _f_writefar\r
271 ARG   handle:word,buffer:dword,count:word\r
272         push bp             ; Preserve caller's stack frame\r
273         mov  bp,sp\r
274         push ds\r
275 \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
279         mov  cx,[count]\r
280         lds  dx,[buffer]\r
281         int  21h            ; call DOS service\r
282         jnb  @@Done         ; has the function failed ?\r
283         mov  [_file_err],ax\r
284         xor  ax,ax          ;  yes, return error\r
285         jmp  short @@Done\r
286 @@Done:                     ; otherwise return bytes written\r
287         pop  ds\r
288         pop  bp             ; restore caller's stack frame\r
289         ret\r
290 _f_writefar endp\r
291 \r
292 \r
293 \r
294 ;****************************************************************\r
295 ;\r
296 ; name: f_seek\r
297 ;\r
298 ; C Prototype:\r
299 ;\r
300 ;   extern long int f_seek(int handle, long int position, char method_code)\r
301 ;\r
302 ; Moves the file pointer according to the position and method code\r
303 ;\r
304 ; Returns file pointer position on success, -1 on failure\r
305 ;\r
306 proc _f_seek\r
307 ARG   handle:word,position:dword,method_code:byte\r
308         push bp             ; Preserve caller's stack frame\r
309         mov  bp,sp\r
310 \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
319         mov  [_file_err],ax\r
320         mov  ax,-1          ;  yes, return error\r
321         mov  dx,-1          ;\r
322         jmp  short @@Done\r
323 @@Done:                     ; otherwise return bytes written\r
324         pop  bp             ; restore caller's stack frame\r
325         ret\r
326 _f_seek endp\r
327 \r
328 ;****************************************************************\r
329 ;\r
330 ; name: f_tell\r
331 ;\r
332 ; C Prototype:\r
333 ;\r
334 ;   extern long int f_tell(int handle)\r
335 ;\r
336 ; Returns file pointer position on success, -1 on failure\r
337 ;\r
338 proc _f_tell\r
339 ARG   handle:word,position:dword,method_code:byte\r
340         push bp             ; Preserve caller's stack frame\r
341         mov  bp,sp\r
342 \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
346         xor  dx,dx\r
347         mov  cx,dx\r
348         int  21h\r
349         jnb  @@Done\r
350         mov  [_file_err],ax\r
351         mov  ax,-1          ;  yes, return error\r
352         mov  dx,-1          ;\r
353         jmp  short @@Done\r
354 @@Done:                     ; otherwise return bytes written\r
355         pop  bp             ; restore caller's stack frame\r
356         ret\r
357 _f_tell endp\r
358 \r
359 \r
360 ;****************************************************************\r
361 ;\r
362 ; name: f_filelength\r
363 ;\r
364 ; C Prototype:\r
365 ;\r
366 ;   extern long int f_filelength(int handle)\r
367 ;\r
368 ; Returns the length of the file associated with the specified handle\r
369 ;\r
370 ; Returns file length on success, -1 on failure\r
371 ;\r
372 proc _f_filelength\r
373 ARG     handle:word\r
374 LOCAL   low:word,high:word=LocalStk\r
375         push bp             ; Preserve caller's stack frame\r
376         mov  bp,sp\r
377         sub  sp,LocalStk\r
378 \r
379         mov  [_file_err],0  ; Clear error\r
380 \r
381         ; Get ptr's current location in file and save it\r
382 \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
386         xor  cx,cx\r
387         xor  dx,dx\r
388         int  21h\r
389         jb   @@Error\r
390         mov  [low],ax\r
391         mov  [high],dx\r
392 \r
393         ; Get ptr's value at end of file\r
394 \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
398         xor  cx,cx\r
399         xor  dx,dx\r
400         int  21h\r
401         jb   @@Error\r
402 \r
403         ; Save the results while returning pointer to its previous location\r
404 \r
405         push ax\r
406         push dx\r
407 \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
411         mov  cx,[high]\r
412         mov  dx,[low]\r
413         int  21h\r
414 \r
415         ; restore resultant length\r
416 \r
417         pop  dx\r
418         pop  ax\r
419 \r
420         jnb   @@Done        ; Was the operation a success ?\r
421 @@Error:\r
422         mov  [_file_err],ax\r
423         mov  ax,-1          ;  no, return error\r
424         mov  dx,-1          ;\r
425 @@Done:                     ; otherwise return bytes written\r
426         mov  sp,bp\r
427         pop  bp             ; restore caller's stack frame\r
428         ret\r
429 _f_filelength endp\r
430 \r
431 \r
432         end\r