home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / desklib / Libraries / File / s / FileOps < prev   
Encoding:
Text File  |  1994-05-22  |  9.1 KB  |  324 lines

  1. ;
  2. ;   ####             #    #     # #
  3. ;   #   #            #    #       #          The FreeWare C library for
  4. ;   #   #  ##   ###  #  # #     # ###             RISC OS machines
  5. ;   #   # #  # #     # #  #     # #  #   ___________________________________
  6. ;   #   # ####  ###  ##   #     # #  #
  7. ;   #   # #        # # #  #     # #  #    Please refer to the accompanying
  8. ;   ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9. ;   ________________________________________________________________________
  10. ;
  11. ;   File:    File.s.OpenClose
  12. ;   Author:  Copyright © 1993 Jason Williams
  13. ;   Version: 1.00 (12 Jul 1993)
  14. ;   Purpose: SWI veneers for file operations
  15. ;              open a file for read/write
  16. ;              close an open file
  17. ;              get EOF status
  18. ;              read/write file position, single byte & word, multiple bytes
  19.  
  20. ;   NOTES:   I have bundled all of these operations into a single .s/.o file
  21. ;            because typically if any of them are used, most of them are used,
  22. ;            and they are all pretty small. You can always use this source
  23. ;            file and remove the bits you don't want if you so desire.
  24. ;            By doing this, I have been able to make access to file_lasterror
  25. ;            more efficient (saving one LDR in each function)
  26.  
  27.         GET     h.regdefs
  28.         GET     h.swinos
  29.         GET     h.macros
  30. ;
  31.         PREAMBLE
  32.  
  33. ;
  34. ; ---------------------------------------------------------------------------
  35. ;
  36.  
  37. ; extern os_error *file_lasterror;
  38. ; (Global variable which holds the last file error generated)
  39. ;
  40.         EXPORT  file_lasterror
  41. file_lasterror
  42.         DCD 0
  43. ;
  44. ; ---------------------------------------------------------------------------
  45. ;
  46.         STARTCODE File_Open
  47. ;
  48. ; extern file_handle File_Open(char *filename, file_access access);
  49. ;
  50.         STMFD   sp!, {lr}
  51.  
  52.         MOV     a3, a2     ; Swap a1 and a2 to keep a consistent C interface
  53.         MOV     a2, a1
  54.         MOV     a1, a3
  55.  
  56.         MOV     a3, #0
  57.  
  58.         SWI     SWI_OS_Find + XOS_Bit
  59.  
  60.         MOVVC   a2, #0                   ; No error, so reset file_lasterror
  61.         MOVVS   a2, a1                   ; else store error pointer
  62.         STR     a2, file_lasterror
  63.  
  64.         MOVVS   a1, #0                   ; Error - return handle = NULL
  65.  
  66.         LDMFD   sp!, {pc}^
  67. ;
  68. ; ---------------------------------------------------------------------------
  69. ;
  70.         STARTCODE File_Close
  71. ;
  72. ; extern os_error *File_Close(file_handle a1);
  73. ;
  74.         STMFD   sp!, {lr}
  75.  
  76.         MOV     a2, a1
  77.         MOV     a1, #0
  78.  
  79.         SWI     SWI_OS_Find + XOS_Bit
  80.  
  81.         MOVVC   a1, #0                   ; No error, so reset file_lasterror
  82.         STR     a1, file_lasterror       ; Store error condition
  83.  
  84.         LDMFD   sp!, {pc}^
  85. ;
  86. ; ---------------------------------------------------------------------------
  87. ;
  88.         STARTCODE File_EOF
  89. ;
  90. ; extern BOOL File_EOF(file_handle a1);
  91. ;
  92.         STMFD   sp!, {lr}
  93.  
  94.         MOV     a2, a1
  95.         MOV     a1, #5
  96.  
  97.         SWI     SWI_OS_Args + XOS_Bit
  98.  
  99.         MOVVC   a2, #0
  100.         MOVVS   a2, a1
  101.         STR     a2, file_lasterror
  102.  
  103.         MOV     a1, a3             ; Return EOF flag
  104.  
  105.         LDMFD   sp!, {pc}^
  106. ;
  107. ; ---------------------------------------------------------------------------
  108. ;
  109.         STARTCODE File_Seek
  110. ;
  111. ; extern os_error *File_Seek(file_handle a1, file_position position);
  112. ;
  113.         STMFD   sp!, {lr}
  114.  
  115.         MOV     a3, a2
  116.         MOV     a2, a1
  117.         MOV     a1, #1
  118.  
  119.         SWI     SWI_OS_Args + XOS_Bit
  120.  
  121.         MOVVC   a1, #0
  122.         STR     a1, file_lasterror
  123.  
  124.         LDMFD   sp!, {pc}^
  125. ;
  126. ; ---------------------------------------------------------------------------
  127. ;
  128.         STARTCODE File_ReturnPos
  129. ;
  130. ; extern file_position File_ReturnPos(file_handle a1);
  131. ;
  132.         STMFD   sp!, {lr}
  133.  
  134.         MOV     a2, a1
  135.         MOV     a1, #0
  136.  
  137.         SWI     SWI_OS_Args + XOS_Bit
  138.  
  139.         MOVVC   a1, #0
  140.         STR     a1, file_lasterror
  141.  
  142.         MOV     a1, a3        ; return file position
  143.  
  144.         LDMFD   sp!, {pc}^
  145. ;
  146. ; ---------------------------------------------------------------------------
  147. ;
  148.         STARTCODE File_WriteBytes
  149. ;
  150. ; extern os_error *File_WriteBytes(file_handle a1, void *buffer, int numbytes);
  151. ;
  152.         STMFD   sp!, {v1-v4, lr}
  153.  
  154.         MOV     a4, a3
  155.         MOV     a3, a2
  156.         MOV     a2, a1
  157.         MOV     a1, #2
  158.  
  159.         SWI     SWI_OS_GBPB + XOS_Bit
  160.  
  161.         MOVVC   a1, #0
  162.         STR     a1, file_lasterror
  163.  
  164.         LDMFD   sp!, {v1-v4, pc}^
  165. ;
  166. ; ---------------------------------------------------------------------------
  167. ;
  168.         STARTCODE File_ReadBytes
  169. ;
  170. ; extern int File_ReadBytes(file_handle a1, char *buffer, int numbytes);
  171. ;
  172.         STMFD   sp!, {v1-v4, lr}
  173.  
  174.         MOV     a4, a3
  175.         MOV     a3, a2
  176.         MOV     a2, a1
  177.         MOV     a1, #4
  178.  
  179.         SWI     SWI_OS_GBPB + XOS_Bit
  180.  
  181.         MOVVC   a1, #0
  182.         STR     a1, file_lasterror
  183.  
  184.         MOV     a1, a4               ; Return number of bytes not read
  185.  
  186.         LDMFD   sp!, {v1-v4, pc}^
  187. ;
  188. ; ---------------------------------------------------------------------------
  189. ;
  190.         STARTCODE File_Read8
  191. ;
  192. ; extern int File_Read8(file_handle a1);
  193. ;
  194.         STMFD   sp!, {lr}
  195.  
  196.         MOV     a2, a1
  197.         MOV     a1, #0
  198.  
  199.         SWI     SWI_OS_BGet + XOS_Bit
  200.  
  201.         MOVVC   a2, #0
  202.         MOVVS   a2, a1
  203.         STR     a2, file_lasterror      ; Store error pointer or NULL
  204.  
  205.         MVNVS   a1, #0                  ; Return -1 to indicate error
  206.  
  207.         LDMFD   sp!, {pc}^
  208. ;
  209. ; ---------------------------------------------------------------------------
  210. ;
  211.         STARTCODE File_Read32
  212. ;
  213. ; extern int File_Read32(file_handle a1);
  214. ;
  215.         STMFD   sp!, {v1-v4, lr}
  216.  
  217.         SUB     sp, sp, #4       ; Reserve 4 bytes of workspace
  218.  
  219.         MOV     a4, #4           ; Read 4 bytes
  220.         MOV     a3, sp           ; Into space reserved on stack
  221.         MOV     a2, a1           ; From the given file
  222.         MOV     a1, #4
  223.  
  224.         SWI     SWI_OS_GBPB + XOS_Bit
  225.  
  226.         MOVVC   a2, #0
  227.         MOVVS   a2, a1
  228.         STR     a2, file_lasterror
  229.  
  230.         LDR     a1, [sp], #4     ; Return the word, restore stack pointer
  231.         MVNVS   a1, #0           ; If error, then return -1
  232.  
  233.         LDMFD   sp!, {v1-v4, pc}^
  234. ;
  235. ; ---------------------------------------------------------------------------
  236. ;
  237.         STARTCODE File_Read32R
  238. ;
  239. ; extern int File_Read32R(file_handle a1);
  240. ;
  241.         STMFD   sp!, {lr}
  242.         BL      File_Read32                 ; Read the 32-bit value into a1
  243.                                             ; ... and then reverse the bytes
  244.                                             ;  a1    a2    a3
  245.                                             ; 1234
  246.         MOV     a2, a1, LSR #24             ;       xxx1
  247.         BIC     a1, a1, #&FF000000          ; x234
  248.         MOV     a3, a1, LSR #16             ;             xxx2
  249.         ORR     a2, a2, a3, LSL #8          ;       xx21
  250.         AND     a3, a1, #&000000FF          ;             xxx4
  251.         AND     a1, a1, #&0000FF00          ; xx3x
  252.         ORR     a2, a2, a1, LSL #8          ;       x321
  253.         ORR     a1, a2, a3, LSL #24         ; 4321
  254.         LDMFD   sp!, {pc}^
  255. ;
  256. ; ---------------------------------------------------------------------------
  257. ;
  258.         STARTCODE File_Write8
  259. ;
  260. ; extern os_error *File_Write8(file_handle a1, int byte);
  261. ;
  262.         STMFD   sp!, {lr}
  263.  
  264.         MOV     a3, a2        ; Swap r0 and r1 over to retain a consistent
  265.         MOV     a2, a1        ; C interface (handle always 1st arg)
  266.         MOV     a1, a3
  267.  
  268.         SWI     SWI_OS_BPut + XOS_Bit
  269.  
  270.         MOVVC   a1, #0
  271.         STR     a1, file_lasterror
  272.  
  273.         LDMFD   sp!, {pc}^
  274. ;
  275. ; ---------------------------------------------------------------------------
  276. ;
  277.         STARTCODE File_Write32
  278. ;
  279. ; extern os_error *File_Write32(file_handle a1, int word);
  280. ;
  281.         STMFD   sp!, {v1-v4, lr}
  282.  
  283.         SUB     sp, sp, #4       ; Reserve 4 bytes of workspace
  284.         STR     a2, [sp]         ; Place word to write into workspace
  285.  
  286.         MOV     a4, #4           ; Write 4 bytes
  287.         MOV     a3, sp           ; From space reserved on stack
  288.         MOV     a2, a1           ; To the given file
  289.         MOV     a1, #2
  290.  
  291.         SWI     SWI_OS_GBPB + XOS_Bit
  292.  
  293.         ADD     sp, sp, #4       ; restore stack pointer
  294.  
  295.         MOVVC   a1, #0
  296.         STR     a1, file_lasterror
  297.  
  298.         LDMFD   sp!, {v1-v4, pc}^
  299. ;
  300. ; ---------------------------------------------------------------------------
  301. ;
  302.         STARTCODE File_Write32R
  303. ;
  304. ; extern os_error *File_Write32R(file_handle a1, int a2);
  305. ;
  306.         STMFD   sp!, {lr}
  307.                                             ;  a2    a4    a3
  308.                                             ; 1234
  309.         MOV     a4, a2, LSR #24             ;       xxx1
  310.         BIC     a2, a2, #&FF000000          ; x234
  311.         MOV     a3, a2, LSR #16             ;             xxx2
  312.         ORR     a4, a4, a3, LSL #8          ;       xx21
  313.         AND     a3, a2, #&000000FF          ;             xxx4
  314.         AND     a2, a2, #&0000FF00          ; xx3x
  315.         ORR     a4, a4, a2, LSL #8          ;       x321
  316.         ORR     a2, a4, a3, LSL #24         ; 4321
  317.  
  318.         BL      File_Write32                ; Write the word
  319.         LDMFD   sp!, {pc}^
  320. ;
  321. ; ---------------------------------------------------------------------------
  322. ;
  323.         END
  324.