home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / m / makealf / Source / s / lowfile < prev    next >
Text File  |  1994-05-09  |  5KB  |  193 lines

  1. ; >s.lowfile
  2. ;
  3. ; functions for low-level file handling under RISC OS
  4. ;
  5. ; This code occupies about 200 bytes.
  6. ;
  7. ; It provides functions to:
  8. ;   open and close files
  9. ;   read or write a block of bytes
  10. ;   read or set the pointer
  11. ;   test for EOF
  12. ;   read the extent (size) of a file
  13. ;
  14. ; It does NOT give access to OS_BGet and OS_BPut because they are
  15. ; horribly slow. It's almost always much better to use OS_GBPB.
  16. ; Since the only reason for using these routines is efficiency, it doesn't
  17. ; make sense to provide inefficient things that can be done with
  18. ; the standard C calls.
  19. ;
  20. ; All these functions obey APCS-R. On entry, lr is saved on the
  21. ; stack; it's safe to use these in supervisor mode.
  22.  
  23.         GET h.asmregs
  24.  
  25.         GET h.asmSWIs
  26.  
  27.         AREA |A$$code|, CODE, READONLY
  28.  
  29.         EXPORT low_open
  30.         EXPORT low_close
  31.         EXPORT low_eof
  32.         EXPORT low_read
  33.         EXPORT low_write
  34.         EXPORT low_ptr
  35.         EXPORT low_setptr
  36.         EXPORT low_seek
  37.         EXPORT low_extent
  38.  
  39. ; int low_open(char *name, int mode);
  40. ; mode: only bits 4,7 matter.
  41. ;       bit 7 indicates "write"
  42. ;       bit 6 indicates "existing"
  43. ;       having both clear is a mistake
  44. ; returns file handle if OK, 0 if not OK
  45.  
  46. low_open
  47.         STMFD   sp!,{lr}
  48.         AND     r1,r1,#&C0
  49.         ORR     r1,r1,#&07
  50.         MOV     r2,r1       ;
  51.         MOV     r1,r0       ; swap r0,r1 
  52.         MOV     r0,r2       ;
  53.         SWI     XOS_Bit+SWI_OS_Find
  54.         MOVVS   r0,#0       ; if error
  55.         LDMFD   sp!,{pc}^   ; return, restoring flags
  56.  
  57. ; void low_close(int handle);
  58. ; if handle==0 this doesn't do anything. (This is not the same
  59. ; as raw OS_Find 0.)
  60.  
  61. low_close
  62.         STMFD   sp!,{lr}
  63.         CMP     r0,#0
  64.         MOVNE   r1,r0
  65.         MOVNE   r0,#0
  66.         SWINE   XOS_Bit+SWI_OS_Find
  67.         LDMFD   sp!,{pc}^
  68.  
  69. ; int low_eof(int handle);
  70. ; returns 0 if not at EOF, 1 if at EOF.
  71. ; undefined behaviour if arg isn't handle of an open file.
  72.  
  73. low_eof STMFD   sp!,{lr}
  74.         MOV     r1,r0
  75.         MOV     r0,#5
  76.         SWI     XOS_Bit+SWI_OS_Args
  77.         CMP     r2,#0       ; annoying. We need to return 0 or 1.
  78.         MOVEQ   r0,#0
  79.         MOVNE   r0,#1
  80.         LDMFD   sp!,{pc}^
  81.  
  82. ; int low_read(int handle, void *buffer, int nbytes);
  83. ; returns number of bytes actually read; 0 if there was an error.
  84.  
  85. low_read
  86.         STMFD   sp!,{r4,r5,lr}
  87.         MOV     r3,r2       ; number of bytes
  88.         MOV     r2,r1       ; buffer address
  89.         MOV     r1,r0       ; handle
  90.         MOV     r0,#4       ; read, using current file pointer
  91.         MOV     r5,r2       ; save for later use
  92.         SWI     XOS_Bit+SWI_OS_GBPB
  93.         MOVVS   r0,#0
  94.         SUBVC   r0,r2,r5    ; next_byte-first_byte
  95.         LDMFD   sp!,{r4,r5,pc}^
  96.  
  97. ; void low_write(int handle, void *buffer, int nbytes);
  98. ; doesn't return anything as the corresponding OS_GBPB call doesn't
  99. ; return any useful information.
  100.  
  101. low_write
  102.         STMFD   sp!,{r4,r5,lr}
  103.         MOV     r3,r2       ; number of bytes
  104.         MOV     r2,r1       ; address
  105.         MOV     r1,r0       ; handle
  106.         MOV     r0,#2       ; write, at current position
  107.         SWI     XOS_Bit+SWI_OS_GBPB
  108.         LDMFD   sp!,{r4,r5,pc}^
  109.  
  110. ; int low_ptr(int handle);
  111. ; returns file pointer
  112. ; an invalid handle will result in garbage
  113.  
  114. low_ptr STMFD   sp!,{lr}
  115.         MOV     r1,r0
  116.         MOV     r0,#0
  117.         SWI     XOS_Bit+SWI_OS_Args
  118.         MOV     r0,r2
  119.         LDMFD   sp!,{pc}^
  120.  
  121. ; void low_setptr(int handle, int ptr);
  122. ; sets the file pointer. Again, there isn't anything useful to return.
  123.  
  124. low_setptr
  125.         STMFD   sp!,{lr}
  126.         MOV     r2,r1
  127.         MOV     r1,r0
  128.         MOV     r0,#1
  129.         SWI     XOS_Bit+SWI_OS_Args
  130.         LDMFD   sp!,{pc}^
  131.  
  132. ; int low_seek(int handle, int n, int whence);
  133. ; sets pointer, but now
  134. ;  - if whence==0 does same as setptr
  135. ;  - if whence==1 moves on by n
  136. ;  - if whence==2 moves to end-n
  137. ; I'm not sure what correct behaviour is if we go past EOF or
  138. ; something. I'll use the RISC OS conventions.
  139. ; We return the new pointer position, or -1 for failure. (This
  140. ; appears to be standard for Unix lseek.)
  141.  
  142. low_seek
  143.         STMFD   sp!,{lr}
  144.         CMP     r0,#0
  145.         BEQ     low_seek_fail    ; 0 is never a valid handle
  146.         CMP     r2,#1
  147.         BGT     low_seek_end
  148.         BEQ     low_seek_advance
  149.         CMP     r1,#0
  150.         BLT     low_seek_fail
  151.         MOV     r2,r1
  152.         MOV     r1,r0
  153. low_seek_doit
  154.         MOV     r0,#1
  155.         SWI     XOS_Bit+SWI_OS_Args
  156.         BVS     low_seek_fail
  157.         MOV     r0,#0
  158.         LDMFD   sp!,{pc}^
  159. low_seek_advance
  160.         MOV     r3,r1    ; save
  161.         MOV     r1,r0
  162.         MOV     r0,#0
  163.         SWI     XOS_Bit+SWI_OS_Args
  164.         ADDS    r2,r2,r3 ; new pointer
  165.         BGE     low_seek_doit
  166. low_seek_fail
  167.         MVN     r0,#0
  168.         LDMFD   sp!,{pc}^
  169. low_seek_end
  170.         MOV     r3,r1    ; save
  171.         MOV     r1,r0
  172.         MOV     r0,#2
  173.         SWI     XOS_Bit+SWI_OS_Args
  174.         SUBS    r2,r2,r3 ; new pointer
  175.         BGE     low_seek_doit
  176.         B       low_seek_fail
  177.  
  178. ; int low_extent(int handle);
  179. ; reads extent of an open file. Returns a negative number if problems.
  180.  
  181. low_extent
  182.         STMFD   sp!,{lr}
  183.         MOVS    r1,r0
  184.         BEQ     low_ext_fail
  185.         MOV     r0,#2
  186.         SWI     XOS_Bit+SWI_OS_Args
  187.         BVS     low_ext_fail
  188.         MOV     r0,r2
  189.         LDMFD   sp!,{pc}^
  190. low_ext_fail
  191.         MVN     r0,#0
  192.         LDMFD   sp!,{pc}^
  193.