home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / Libraries / File / s / FileOps < prev   
Encoding:
Text File  |  1993-07-12  |  9.2 KB  |  327 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.         MOV     a3, #0
  79.  
  80.         SWI     SWI_OS_Find + XOS_Bit
  81.  
  82.         MOVVC   a1, #0                   ; No error, so reset file_lasterror
  83.         STR     a1, file_lasterror       ; Store error condition
  84.  
  85.         LDMFD   sp!, {pc}^
  86. ;
  87. ; ---------------------------------------------------------------------------
  88. ;
  89.         STARTCODE File_EOF
  90. ;
  91. ; extern BOOL File_EOF(file_handle a1);
  92. ;
  93.         STMFD   sp!, {lr}
  94.  
  95.         MOV     a2, a1
  96.         MOV     a1, #5
  97.         MOV     a3, #0
  98.  
  99.         SWI     SWI_OS_Args + XOS_Bit
  100.  
  101.         MOVVC   a2, #0
  102.         MOVVS   a2, a1
  103.         STR     a2, file_lasterror
  104.  
  105.         MOV     a1, a3             ; Return EOF flag
  106.  
  107.         LDMFD   sp!, {pc}^
  108. ;
  109. ; ---------------------------------------------------------------------------
  110. ;
  111.         STARTCODE File_Seek
  112. ;
  113. ; extern os_error *File_Seek(file_handle a1, file_position position);
  114. ;
  115.         STMFD   sp!, {lr}
  116.  
  117.         MOV     a3, a2
  118.         MOV     a2, a1
  119.         MOV     a1, #1
  120.  
  121.         SWI     SWI_OS_Args + XOS_Bit
  122.  
  123.         MOVVC   a1, #0
  124.         STR     a1, file_lasterror
  125.  
  126.         LDMFD   sp!, {pc}^
  127. ;
  128. ; ---------------------------------------------------------------------------
  129. ;
  130.         STARTCODE File_ReturnPos
  131. ;
  132. ; extern file_position File_ReturnPos(file_handle a1);
  133. ;
  134.         STMFD   sp!, {lr}
  135.  
  136.         MOV     a3, #0
  137.         MOV     a2, a1
  138.         MOV     a1, #0
  139.  
  140.         SWI     SWI_OS_Args + XOS_Bit
  141.  
  142.         MOVVC   a1, #0
  143.         STR     a1, file_lasterror
  144.  
  145.         MOV     a1, a3        ; return file position
  146.  
  147.         LDMFD   sp!, {pc}^
  148. ;
  149. ; ---------------------------------------------------------------------------
  150. ;
  151.         STARTCODE File_WriteBytes
  152. ;
  153. ; extern os_error *File_WriteBytes(file_handle a1, void *buffer, int numbytes);
  154. ;
  155.         STMFD   sp!, {v1-v4, lr}
  156.  
  157.         MOV     a4, a3
  158.         MOV     a3, a2
  159.         MOV     a2, a1
  160.         MOV     a1, #2
  161.  
  162.         SWI     SWI_OS_GBPB + XOS_Bit
  163.  
  164.         MOVVC   a1, #0
  165.         STR     a1, file_lasterror
  166.  
  167.         LDMFD   sp!, {v1-v4, pc}^
  168. ;
  169. ; ---------------------------------------------------------------------------
  170. ;
  171.         STARTCODE File_ReadBytes
  172. ;
  173. ; extern int File_ReadBytes(file_handle a1, char *buffer, int numbytes);
  174. ;
  175.         STMFD   sp!, {v1-v4, lr}
  176.  
  177.         MOV     a4, a3
  178.         MOV     a3, a2
  179.         MOV     a2, a1
  180.         MOV     a1, #4
  181.  
  182.         SWI     SWI_OS_GBPB + XOS_Bit
  183.  
  184.         MOVVC   a1, #0
  185.         STR     a1, file_lasterror
  186.  
  187.         MOV     a1, a4               ; Return number of bytes not read
  188.  
  189.         LDMFD   sp!, {v1-v4, pc}^
  190. ;
  191. ; ---------------------------------------------------------------------------
  192. ;
  193.         STARTCODE File_Read8
  194. ;
  195. ; extern int File_Read8(file_handle a1);
  196. ;
  197.         STMFD   sp!, {lr}
  198.  
  199.         MOV     a2, a1
  200.         MOV     a1, #0
  201.  
  202.         SWI     SWI_OS_BGet + XOS_Bit
  203.  
  204.         MOVVC   a2, #0
  205.         MOVVS   a2, a1
  206.         STR     a2, file_lasterror      ; Store error pointer or NULL
  207.  
  208.         MVNVS   a1, #0                  ; Return -1 to indicate error
  209.  
  210.         LDMFD   sp!, {pc}^
  211. ;
  212. ; ---------------------------------------------------------------------------
  213. ;
  214.         STARTCODE File_Read32
  215. ;
  216. ; extern int File_Read32(file_handle a1);
  217. ;
  218.         STMFD   sp!, {v1-v4, lr}
  219.  
  220.         SUB     sp, sp, #4       ; Reserve 4 bytes of workspace
  221.  
  222.         MOV     a4, #4           ; Read 4 bytes
  223.         MOV     a3, sp           ; Into space reserved on stack
  224.         MOV     a2, a1           ; From the given file
  225.         MOV     a1, #4
  226.  
  227.         SWI     SWI_OS_GBPB + XOS_Bit
  228.  
  229.         MOVVC   a2, #0
  230.         MOVVS   a2, a1
  231.         STR     a2, file_lasterror
  232.  
  233.         LDR     a1, [sp], #4     ; Return the word, restore stack pointer
  234.         MVNVS   a1, #0           ; If error, then return -1
  235.  
  236.         LDMFD   sp!, {v1-v4, pc}^
  237. ;
  238. ; ---------------------------------------------------------------------------
  239. ;
  240.         STARTCODE File_Read32R
  241. ;
  242. ; extern int File_Read32R(file_handle a1);
  243. ;
  244.         STMFD   sp!, {lr}
  245.         BL      File_Read32                 ; Read the 32-bit value into a1
  246.                                             ; ... and then reverse the bytes
  247.                                             ;  a1    a2    a3
  248.                                             ; 1234
  249.         MOV     a2, a1, LSR #24             ;       xxx1
  250.         BIC     a1, a1, #&FF000000          ; x234
  251.         MOV     a3, a1, LSR #16             ;             xxx2
  252.         ORR     a2, a2, a3, LSL #8          ;       xx21
  253.         AND     a3, a1, #&000000FF          ;             xxx4
  254.         AND     a1, a1, #&0000FF00          ; xx3x
  255.         ORR     a2, a2, a1, LSL #8          ;       x321
  256.         ORR     a1, a2, a3, LSL #24         ; 4321
  257.         LDMFD   sp!, {pc}^
  258. ;
  259. ; ---------------------------------------------------------------------------
  260. ;
  261.         STARTCODE File_Write8
  262. ;
  263. ; extern os_error *File_Write8(file_handle a1, int byte);
  264. ;
  265.         STMFD   sp!, {lr}
  266.  
  267.         MOV     a3, a2        ; Swap r0 and r1 over to retain a consistent
  268.         MOV     a2, a1        ; C interface (handle always 1st arg)
  269.         MOV     a1, a3
  270.  
  271.         SWI     SWI_OS_BPut + XOS_Bit
  272.  
  273.         MOVVC   a1, #0
  274.         STR     a1, file_lasterror
  275.  
  276.         LDMFD   sp!, {pc}^
  277. ;
  278. ; ---------------------------------------------------------------------------
  279. ;
  280.         STARTCODE File_Write32
  281. ;
  282. ; extern os_error *File_Write32(file_handle a1, int word);
  283. ;
  284.         STMFD   sp!, {v1-v4, lr}
  285.  
  286.         SUB     sp, sp, #4       ; Reserve 4 bytes of workspace
  287.         STR     a2, [sp]         ; Place word to write into workspace
  288.  
  289.         MOV     a4, #4           ; Write 4 bytes
  290.         MOV     a3, sp           ; From space reserved on stack
  291.         MOV     a2, a1           ; To the given file
  292.         MOV     a1, #2
  293.  
  294.         SWI     SWI_OS_GBPB + XOS_Bit
  295.  
  296.         ADD     sp, sp, #4       ; restore stack pointer
  297.  
  298.         MOVVC   a1, #0
  299.         STR     a1, file_lasterror
  300.  
  301.         LDMFD   sp!, {v1-v4, pc}^
  302. ;
  303. ; ---------------------------------------------------------------------------
  304. ;
  305.         STARTCODE File_Write32R
  306. ;
  307. ; extern os_error *File_Write32R(file_handle a1, int a2);
  308. ;
  309.         STMFD   sp!, {lr}
  310.                                             ;  a2    a4    a3
  311.                                             ; 1234
  312.         MOV     a4, a2, LSR #24             ;       xxx1
  313.         BIC     a2, a2, #&FF000000          ; x234
  314.         MOV     a3, a2, LSR #16             ;             xxx2
  315.         ORR     a4, a4, a3, LSL #8          ;       xx21
  316.         AND     a3, a2, #&000000FF          ;             xxx4
  317.         AND     a2, a2, #&0000FF00          ; xx3x
  318.         ORR     a4, a4, a2, LSL #8          ;       x321
  319.         ORR     a2, a4, a3, LSL #24         ; 4321
  320.  
  321.         BL      File_Write32                ; Write the word
  322.         LDMFD   sp!, {pc}^
  323. ;
  324. ; ---------------------------------------------------------------------------
  325. ;
  326.         END
  327.