home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / !DeskLib / h / File < prev    next >
Encoding:
Text File  |  1993-07-12  |  8.3 KB  |  241 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.h
  12.     Author:  Copyright © 1993 Jason Williams
  13.     Version: 1.00 (12 Jul 1993)
  14.     Purpose: Low level file operations, much faster than stdio equivalents,
  15.              and more convenient when interacting with other code that uses
  16.              RISC OS file handles to refer to files.
  17. */
  18.  
  19.  
  20. #ifndef __dl_file_h
  21. #define __dl_file_h
  22.  
  23. #ifndef __dl_core_h
  24. #include "Core.h"
  25. #endif
  26.  
  27.  
  28. extern os_error *file_lasterror;
  29.  
  30. /*  IMPORTANT NOTE
  31.  *  file_lasterror is used to provide a more convenient SWI veneer for certain
  32.  *  file operations (Open, reading, etc, return handles/values directly,
  33.  *  rather than you having to provide the address of a variable for the
  34.  *  returned value, leaving no convenient way of returning errors!)
  35.  *
  36.  *  Thus, file_lasterror is set by the file operations:
  37.  *    Open, Close, EOF, Read8/32/Bytes, Write8/32/Bytes, Seek, ReturnPos
  38.  *
  39.  *  However, note that it is just a pointer to the error returned by the SWI
  40.  *  call, so it MUST be used immediately after the call, or else it may be
  41.  *  invalid or possibly even dangerously corrupted
  42.  *  If for example, you wish to close the file before reporting the error,
  43.  *  you must first copy the error into your own os_error block.
  44.  *
  45.  *  If no error occurred on the last file op, file_lasterror will be NULL.
  46.  *  Most of the above functions also return values (filehandle = NULL etc)
  47.  *  that will indicate when an error may have occurred, at which time you
  48.  *  can interrogate file_lasterror to work out what the error was.
  49.  *
  50.  *  Note also that all of the above calls which do not have a specific
  51.  *  return value (Close, Write*, Seek) return the os_error pointer directly
  52.  *  as well as setting file_lasterror, for convenience.
  53.  */
  54.  
  55.  
  56.  /*  File_Delete
  57.   *  Attempts to delete the named file. Use carefully!
  58.   */
  59. extern os_error *File_Delete(char *filename);
  60.  
  61.  
  62.  /*  File_Size
  63.   *  Reads the size (in bytes) of the file. A return value of 0 indicates
  64.   *  either the file is 0 bytes long, or an error occurred (which usually
  65.   *  means that the file doesn't exist)
  66.   */
  67. extern int File_Size(char *filename);
  68.  
  69.  
  70. /*  File_Exists
  71.  *  This returns TRUE if the file exists.
  72.  *  (It attempts to open the file for reading - it returns TRUE if successful)
  73.  */
  74. extern BOOL File_Exists(char *filename);
  75.  
  76.  
  77.  
  78. typedef int file_position;
  79. typedef int file_handle;
  80.  
  81. #define file_READERROR (-1)
  82.  
  83.  
  84.  /*  file_access values
  85.   *  These are used when opening files. NOTE that they correspond directly
  86.   *  to OS_File opening flags (OS_File register 0 entry value)
  87.   */
  88.  
  89. typedef enum
  90. {
  91.   file_READ   = 0x40,          /* Open the file for reading                 */
  92.   file_WRITE  = 0x80,          /* CREATE/WIPE the file, open it for writing */
  93.   file_APPEND = 0xC0           /* Open for appending to existing file. NOTE
  94.                                 * that I don't have the PRMs so I can't tell
  95.                                 * you where this puts the initial file pointer
  96.                                 */
  97. } file_access;
  98.  
  99.  
  100.  /*  File_Open
  101.   *  Opens the given file for reading or writing (see file_access's above)
  102.   *  Returns the file handle, or NULL if it failed
  103.   *  (if NULL, you can check file_lasterror for the reason)
  104.   *
  105.   *  NOTE that the OS will return a NULL file handle if we try to open a
  106.   *  non existent file, but does NOT flag this as an error (i.e. File_Open
  107.   *  returns NULL, but file_lasterror is ALSO NULL). You must therefore
  108.   *  check file_lasterror before blindly trying to use it to report an error.
  109.   */
  110. extern file_handle File_Open(char *filename, file_access access);
  111.  
  112.  
  113.  /*  File_Close
  114.   *  Closes an open file stream, using the handle returned by File_Open
  115.   *  After a close, the file_handle is no longer valid, so do not try
  116.   *  to use it.
  117.   */
  118. extern os_error *File_Close(file_handle handle);
  119.  
  120.  
  121.  /*  File_EOF
  122.   *  Returns TRUE if the end of the file has been reached
  123.   */
  124. extern BOOL File_EOF(file_handle handle);
  125.  
  126.  
  127.  /*  File_Seek
  128.   *  Seeks the file pointer to the given position (an offset in terms of
  129.   *  bytes offset from the start of the file). Subsequent read/write
  130.   *  operations will read from or write to that part of the file.
  131.   */
  132. extern os_error *File_Seek(file_handle handle, file_position position);
  133.  
  134.  
  135.  /*  File_ReturnPos
  136.   *  Returns the current file pointer position.
  137.   *  This is a value which, when passed to File_Seek, will return you to the
  138.   *  same position you were at when you called File_ReturnPos
  139.   */
  140. extern file_position File_ReturnPos(file_handle handle);
  141.  
  142.  
  143.  /*  File_WriteBytes
  144.   *  This writes a chunk of data to the current position in a file opened
  145.   *  for writing. 'numbytes' bytes will be written from the given buffer.
  146.   *  It is important to check for errors from this call as Disc Full errors
  147.   *  are a common occurrence.
  148.   */
  149. extern os_error *File_WriteBytes(file_handle handle,
  150.                                  void *buffer, int numbytes);
  151.  
  152.  
  153.  /*  File_ReadBytes
  154.   *  Reads the given number of bytes of data from the given file, into the
  155.   *  given buffer. Returns the number of bytes NOT successfully read.
  156.   *  (i.e. if you hit EOF during the read, the return value will be non-zero)
  157.   */
  158. extern int File_ReadBytes(file_handle handle, void *buffer, int numbytes);
  159.  
  160.  
  161.  /*  File_Write8
  162.   *  Writes an 8-bit value (a byte/char) to the given output file.
  163.   */
  164. extern os_error *File_Write8(file_handle handle, int byte);
  165.  
  166. #define File_WriteByte File_Write8
  167. #define File_WriteChar File_Write8
  168.  
  169.  
  170.  /*  File_Read8
  171.   *  Reads an 8-bit value (a byte/char) from the given input file.
  172.   *  If an error (typically EOF) occurs during the read, file_READERROR
  173.   *  will be returned.
  174.   *
  175.   *  VERY IMPORTANT NOTE!
  176.   *  DO NOT use File_Read8() in a line like the following:
  177.   *    shortvalue = File_Read8(infile) | (File_Read8(infile) << 8);
  178.   *
  179.   *  The problem here is that the C compiler is allowed to evaluate the 2
  180.   *  functions in any order it sees fit, so may read the two bytes in the
  181.   *  incorrect order. You MUST therefore do such an operation with code
  182.   *  in the following form if you are to sure it will work:
  183.   *    shortvalue = File_Read8(infile);
  184.   *    shortvalue |= File_Read8(infile) << 8;  
  185.   */
  186. extern int File_Read8(file_handle handle);
  187.  
  188. #define File_ReadByte File_Read8
  189. #define File_ReadChar File_Read8
  190.  
  191.  
  192.  /*  File_Write32
  193.   *  Writes an 32-bit value (a word/int/long) to the given output file.
  194.   */
  195. extern os_error *File_Write32(file_handle handle, int word);
  196.  
  197. #define File_WriteWord File_Write32
  198. #define File_WriteInt  File_Write32
  199. #define File_WriteLong File_Write32
  200.  
  201.  
  202.  /*  File_Read32
  203.   *  Reads a 32-bit value (a word/int/long) from the given input file.
  204.   *  If an error (typically EOF) occurs during the read, file_READERROR
  205.   *  will be returned. (Note that this is a perfectly legal value to read
  206.   *  from the file, though, so you should check file_lasterror to see if
  207.   *  an error occurred).
  208.   */
  209. extern int File_Read32(file_handle handle);
  210.  
  211. #define File_ReadWord File_Read32
  212. #define File_ReadInt  File_Read32
  213. #define File_ReadLong File_Read32
  214.  
  215.  
  216.  /*  File_Read32R
  217.   *  Identical to File_Read32, but reverses the byte ordering as it reads
  218.   *  (converts from little endian to big endian format). This is very useful
  219.   *  for reading textual tags from files, as they can then be compared to
  220.   *  integer constants of the form 'aTag' rather than having to reverse the
  221.   *  byte order manually (i.e. using the tag 'gaTa' which is less human
  222.   *  readable)
  223.   */
  224. extern int File_Read32R(file_handle handle);
  225. #define File_ReadWordR File_Read32R
  226. #define File_ReadIntR  File_Read32R
  227. #define File_ReadLongR File_Read32R
  228.  
  229.  
  230.  /*  File_Write32R
  231.   *  Same as Write32, but reverses the byte order before writing. See
  232.   *  Read32r for more details about why you'd want to do this.
  233.   */
  234. extern os_error *File_Write32R(file_handle handle, int word);
  235.  
  236. #define File_WriteWordR File_Write32R
  237. #define File_WriteIntR  File_Write32R
  238. #define File_WriteLongR File_Write32R
  239.  
  240. #endif
  241.