home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / pasdox / file.txt < prev    next >
Text File  |  1985-11-18  |  11KB  |  282 lines

  1. { -----------------------------------------------------------------------------
  2.  
  3.                                  NOTICE:
  4.  
  5.       THESE MATERIALS are UNSUPPORTED by OSS!  If you do not understand how to
  6.       use them do not contact OSS for help!  We will not teach you how to 
  7.       program in Pascal.  If you find an error in these materials, feel free
  8.       to SEND US A LETTER explaining the error, and how to fix it.
  9.  
  10.       THE BOTTOM LINE:
  11.  
  12.          Use it, enjoy it, but you are on your own when using these materials!
  13.  
  14.  
  15.                                DISCLAIMER:
  16.  
  17.       OSS makes no representations or warranties with respect to the contents
  18.       hereof and specifically disclaim all warranties of merchantability or
  19.       fitness for any particular purpose.   This document is subject to change
  20.       without notice.
  21.       
  22.       OSS provides these materials for use with Personal Pascal.  Use them in
  23.       any way you wish.
  24.  
  25.    -------------------------------------------------------------------------- }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. Disk File Functions                                                     Page 1
  32.  
  33.                              Disk File Operations
  34.  
  35. There  may  be  times when you want to perform operations not supported by the
  36. standard Personal Pascal I/O library.  Until such time as equivalent  routines
  37. are  added  to the Personal Pascal library, you can use direct GEMDOS calls to
  38. achieve the results you want.  For all calls described in this section,  we're
  39. going to assume the following TYPE declarations are present in your program:
  40.  
  41.   TYPE
  42.     Path_Chars = PACKED ARRAY [ 1..80 ] OF Char ;
  43.  
  44. Notice  that  the  maximum  length of a GEMDOS pathname is 80 characters.  The
  45. reason we are using a packed array and not a Pascal STRING type is that GEMDOS
  46. is expecting path and file names in the "C language" string format.  In  other
  47. words,  GEMDOS  normally  wants  a string in which the first byte is the first
  48. data character, and it expects the string to be  terminated  by  a  zero  byte
  49. (Chr(  0  ),  in  Pascal).  You can't pass a Pascal STRING to GEMDOS directly,
  50. since the first character in a Pascal string (s[0]) is the length byte for the
  51. string, and the string is not null-terminated.  In  order  to  pass  a  Pascal
  52. string  to  GEMDOS,  you  have  to copy it into a "C-type" string by calling a
  53. procedure like the following:
  54.  
  55.   PROCEDURE Make_Path( VAR ps : Str255 ; VAR cs : Path_Chars ) ;
  56.  
  57.     VAR
  58.       i : Integer ;
  59.  
  60.     BEGIN
  61.       FOR i := 1 TO Length( ps ) DO
  62.         cs[i] := ps[i] ;
  63.       cs[ length(ps)+1 ] := Chr(0) ;
  64.     END ;
  65.  
  66. Now that we know what type of names to pass to GEMDOS,  we  can  get  on  with
  67. presenting the calls you can use:
  68.  
  69.  
  70. Create and open a file.
  71.  
  72. Sometimes,  you  may  want  open  a  file  with special properties that Pascal
  73. doesn't support.  For this purpose, you can use the following routine:
  74.  
  75. FUNCTION f_create( VAR name : Path_Chars ; attributes : Integer ) : Integer ;
  76.   GEMDOS( $3c ) ;
  77.  
  78. This call creates a new  file  with  the  specified  name  and  the  specified
  79. attributes.    The  bits  in  the  attributes  parameter  have  the  following
  80. assignments:
  81.  
  82. bit  meaning
  83. ---  -------
  84. $01  file is read-only
  85. $02  file is hidden from directory search
  86. $04  file is a system file, hidden from directory search
  87. $08  file contains a volume label in the first 8 data bytes
  88.  
  89. The return value is a valid GEMDOS file handle, if greater than  or  equal  to
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. Disk File Functions                                                     Page 2
  98.  
  99. zero,  or  an  error  number, if negative.  You should use this call to open a
  100. file for output, if you want to open a new file, or if you want to first erase
  101. the previous contents.  If you want to write  to  an  existing  file,  without
  102. erasing the contents, use the f_open call, below.
  103.  
  104.  
  105. Open a file.
  106.  
  107. You  might  also  want  to  open  an  existing  file  (or one you created with
  108. f_create) without using the built-in procedure Reset.  You can use this GEMDOS
  109. call:
  110.  
  111. FUNCTION f_open( VAR name : Path_Chars ; mode : Integer ) : Integer ;
  112.   GEMDOS( $3d ) ;
  113.  
  114. Use this call to open a file for reading, writing, or updating.  If  you  want
  115. to open a file for writing, but you want to first erase the previous contents,
  116. use the f_create call, instead.  The valid values for mode are:
  117.  
  118. 0  open for reading only
  119. 1  open for writing only
  120. 2  open for reading or writing
  121.  
  122. The  return  value is a GEMDOS handle, if greater than or equal to zero, or an
  123. error number, if negative.  Notice that this call does not have a parameter to
  124. specify the  attributes  of  the  file.   Those  attributes  are  set  by  the
  125. f_create  call  and  are  not changed by this call.  If you want to change the
  126. attributes of a file, you can use the f_attrib call, below.
  127.  
  128.  
  129. Close an open file.
  130.  
  131. If you used f_create or f_open to ready a file for access, you should use  the
  132. following  call  to  close  it  when you're finished reading or writing to the
  133. file:
  134.  
  135. FUNCTION f_close( handle : Integer ) : Integer ;
  136.   GEMDOS( $3e ) ;
  137.  
  138. The parameter handle should be the same as that returned  by  the  appropriate
  139. open  call.   Zero  is  returned,  if  the  file was closed successfully, or a
  140. negative error number, otherwise.
  141.  
  142.  
  143. Read bytes from a file.
  144.  
  145. Pascal supports reading from and writing to files one item at  a  time,  where
  146. the  size  of the item is the size of the file pointer variable.  Occasionally
  147. you may want to read or write in larger chunks, especially if your  item  size
  148. is  small,  since  GEMDOS  isn't  very  fast  for  single-byte transfers.  The
  149. following call allows you to read a block of characters into memory:
  150.  
  151. FUNCTION f_read( handle : Integer ; count : Long_Integer ; VAR buf :  Buf_Type
  152. ) : Long_Integer ;
  153.   GEMDOS( $3f ) ;
  154.  
  155. This  call  reads  an  arbitrary  number  of  bytes from a file into a desired
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. Disk File Functions                                                     Page 3
  164.  
  165. buffer.  The number of bytes actually read is returned, if  the  function  was
  166. successful,  or  a  negative error number, if something went wrong.  Note that
  167. the number of bytes actually read may be shorter  than  the  number  of  bytes
  168. requested,  if  the  end-of-file position was reached.  The Buf_Type mentioned
  169. above may be almost any type.  For example, to read 100 two-byte  values  into
  170. an array, you might use a program segment like this:
  171.   TYPE
  172.     Hundred_Integers = ARRAY [ 1..100 ] OF Integer ;
  173.  
  174.   VAR
  175.     a : Hundred_Integers ;
  176.     bytes_read : Long_Integer ;
  177.  
  178.   PROCEDURE  f_read(  handle  :  Integer  ;  count  : Long_Integer ; VAR buf :
  179. Hundred_Integers ) : Long_Integer ;
  180.   GEMDOS( $3f ) ;
  181.  
  182.   BEGIN
  183.     bytes_read := f_read( handle, 200, a ) ;
  184.   END ;
  185.  
  186. Note that 200 was passed as the number of bytes to read, since we  wanted  100
  187. two-byte values!
  188.  
  189. The  handle  parameter should be that value returned by either the f_create or
  190. f_open call.  If you want to use the f_read call to read from a file which was
  191. opened using the built-in procedure Reset, you can use the function Handle  to
  192. find  out the handle associated with the file.  If you are reading from a file
  193. just opened using Reset, you must be aware, however, that the first  item  has
  194. already been read from the file and put into the file buffer variable.
  195.  
  196. Write bytes to a file.
  197.  
  198. Similarly,  you may want to write an arbitrary number of bytes to a file.  The
  199. following call supports block writing:
  200.  
  201. FUNCTION f_write( handle : Integer ; count : Long_Integer ; VAR buf : Buf_Type
  202. ) : Long_Integer ;
  203.   GEMDOS( $40 ) ;
  204.  
  205. This call is the counterpart of the f_read function described above.  It takes
  206. an arbitrary number of bytes from a buffer and outputs them  to  a  previously
  207. opened  file.   The  handle  parameter  must  be  that which was returned by a
  208. previous f_open or f_create call.  You can also use the Handle function to get
  209. the handle of a file which was opened using the  Rewrite  built-in  procedure.
  210. The value returned by f_write is the number of bytes written, if the operation
  211. was  successful,  or  a  negative  error number.  In general, if the number of
  212. bytes returned does not equal the number requested, something went wrong!
  213.  
  214.  
  215. Delete a file.
  216.  
  217. There is no standard procedure in Pascal to remove a file from a disk,  so  if
  218. you want to erase files, you need the following call:
  219.  
  220. FUNCTION f_delete( VAR name : Path_Chars ) : Integer ;
  221.   GEMDOS( $41 ) ;
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. Disk File Functions                                                     Page 4
  230.  
  231.  
  232. Zero  is  returned,  if  the delete was successful, or a negative error value,
  233. otherwise.
  234.  
  235.  
  236. Seek within a file.
  237.  
  238. Personal Pascal supports random access to files using the built-in  procedures
  239. Get,  Put, and Seek.  If you want to use instead the underlying GEMDOS routine
  240. to position within a file, here it is:
  241.  
  242. FUNCTION  f_seek(  offset  :  Long_Integer  ;  handle,  mode  :  Integer  )  :
  243. Long_Integer ;
  244.   GEMDOS( $42 ) ;
  245.  
  246. Use  this  call  to  point  to  a particular byte position within a file.  The
  247. offset parameter specifies the desired byte position, and the  mode  parameter
  248. specifies which file position the offset parameter is relative to:
  249.  
  250. mode  relative to
  251. ----  -----------
  252.   0   the beginning of the file
  253.   1   the current location
  254.   2   the end of the file
  255.  
  256. The  offset  parameter  is  signed,  so  you could, for example, move 10 bytes
  257. backwards in the file by specifying offset and mode parameters of -10  and  1,
  258. respectively.
  259.  
  260.  
  261. Get/Set file attributes.
  262.  
  263. As  mentioned  above,  the  f_create  call  sets  a  file's attributes.  These
  264. attributes are never changed when the file is  subsequently  opened.   If  you
  265. ever  want  to  change  the attributes of a file, you should use the following
  266. call:
  267.  
  268. FUNCTION f_attrib( VAR name : Path_Chars ; mode,  attributes  :  Integer  )  :
  269. Integer ;
  270.   GEMDOS( $43 ) ;
  271.  
  272. The  mode  parameter specifies whether to get the file attributes, if 0, or to
  273. set the attributes, if 1.  The attributes parameter is specified in  the  same
  274. way as for the f_create call, above, with the following two additions:
  275.  
  276. bit  meaning
  277. ---  -------
  278. $10  file is a subdirectory
  279. $20  file is written and closed correctly.
  280.  
  281. These two attributes only refer to subdirectories.
  282. ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆