FIO.def 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. (* FIO.def provides a simple buffered file input/output library.
  2. Copyright (C) 2001-2025 Free Software Foundation, Inc.
  3. Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
  4. This file is part of GNU Modula-2.
  5. GNU Modula-2 is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GNU Modula-2 is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. *)
  20. DEFINITION MODULE FIO ;
  21. (* Provides a simple buffered file input/output library. *)
  22. FROM SYSTEM IMPORT ADDRESS, BYTE ;
  23. EXPORT QUALIFIED (* types *)
  24. File,
  25. (* procedures *)
  26. OpenToRead, OpenToWrite, OpenForRandom, Close,
  27. EOF, EOLN, WasEOLN, IsNoError, Exists, IsActive,
  28. exists, openToRead, openToWrite, openForRandom,
  29. SetPositionFromBeginning,
  30. SetPositionFromEnd,
  31. FindPosition,
  32. ReadChar, ReadString,
  33. WriteChar, WriteString, WriteLine,
  34. WriteCardinal, ReadCardinal,
  35. UnReadChar,
  36. WriteNBytes, ReadNBytes,
  37. FlushBuffer,
  38. GetUnixFileDescriptor,
  39. GetFileName, getFileName, getFileNameLength,
  40. FlushOutErr,
  41. (* variables *)
  42. StdIn, StdOut, StdErr ;
  43. TYPE
  44. File = CARDINAL ;
  45. (* the following variables are initialized to their UNIX equivalents *)
  46. VAR
  47. StdIn, StdOut, StdErr: File ;
  48. (*
  49. IsNoError - returns a TRUE if no error has occured on file, f.
  50. *)
  51. PROCEDURE IsNoError (f: File) : BOOLEAN ;
  52. (*
  53. IsActive - returns TRUE if the file, f, is still active.
  54. *)
  55. PROCEDURE IsActive (f: File) : BOOLEAN ;
  56. (*
  57. Exists - returns TRUE if a file named, fname exists for reading.
  58. *)
  59. PROCEDURE Exists (fname: ARRAY OF CHAR) : BOOLEAN ;
  60. (*
  61. OpenToRead - attempts to open a file, fname, for reading and
  62. it returns this file.
  63. The success of this operation can be checked by
  64. calling IsNoError.
  65. *)
  66. PROCEDURE OpenToRead (fname: ARRAY OF CHAR) : File ;
  67. (*
  68. OpenToWrite - attempts to open a file, fname, for write and
  69. it returns this file.
  70. The success of this operation can be checked by
  71. calling IsNoError.
  72. *)
  73. PROCEDURE OpenToWrite (fname: ARRAY OF CHAR) : File ;
  74. (*
  75. OpenForRandom - attempts to open a file, fname, for random access
  76. read or write and it returns this file.
  77. The success of this operation can be checked by
  78. calling IsNoError.
  79. towrite, determines whether the file should be
  80. opened for writing or reading.
  81. newfile, determines whether a file should be
  82. created if towrite is TRUE or whether the
  83. previous file should be left alone,
  84. allowing this descriptor to seek
  85. and modify an existing file.
  86. *)
  87. PROCEDURE OpenForRandom (fname: ARRAY OF CHAR;
  88. towrite, newfile: BOOLEAN) : File ;
  89. (*
  90. Close - close a file which has been previously opened using:
  91. OpenToRead, OpenToWrite, OpenForRandom.
  92. It is correct to close a file which has an error status.
  93. *)
  94. PROCEDURE Close (f: File) ;
  95. (* the following functions are functionally equivalent to the above
  96. except they allow C style names.
  97. *)
  98. PROCEDURE exists (fname: ADDRESS; flength: CARDINAL) : BOOLEAN ;
  99. PROCEDURE openToRead (fname: ADDRESS; flength: CARDINAL) : File ;
  100. PROCEDURE openToWrite (fname: ADDRESS; flength: CARDINAL) : File ;
  101. PROCEDURE openForRandom (fname: ADDRESS; flength: CARDINAL;
  102. towrite, newfile: BOOLEAN) : File ;
  103. (*
  104. FlushBuffer - flush contents of the FIO file, f, to libc.
  105. *)
  106. PROCEDURE FlushBuffer (f: File) ;
  107. (*
  108. ReadNBytes - reads nBytes of a file into memory area, dest, returning
  109. the number of bytes actually read.
  110. This function will consume from the buffer and then
  111. perform direct libc reads. It is ideal for large reads.
  112. *)
  113. PROCEDURE ReadNBytes (f: File; nBytes: CARDINAL;
  114. dest: ADDRESS) : CARDINAL ;
  115. (*
  116. ReadAny - reads HIGH (a) + 1 bytes into, a. All input
  117. is fully buffered, unlike ReadNBytes and thus is more
  118. suited to small reads.
  119. *)
  120. PROCEDURE ReadAny (f: File; VAR a: ARRAY OF BYTE) ;
  121. (*
  122. WriteNBytes - writes nBytes from memory area src to a file
  123. returning the number of bytes actually written.
  124. This function will flush the buffer and then
  125. write the nBytes using a direct write from libc.
  126. It is ideal for large writes.
  127. *)
  128. PROCEDURE WriteNBytes (f: File; nBytes: CARDINAL;
  129. src: ADDRESS) : CARDINAL ;
  130. (*
  131. WriteAny - writes HIGH (a) + 1 bytes onto, file, f. All output
  132. is fully buffered, unlike WriteNBytes and thus is more
  133. suited to small writes.
  134. *)
  135. PROCEDURE WriteAny (f: File; VAR a: ARRAY OF BYTE) ;
  136. (*
  137. WriteChar - writes a single character to file, f.
  138. *)
  139. PROCEDURE WriteChar (f: File; ch: CHAR) ;
  140. (*
  141. EOF - tests to see whether a file, f, has reached end of file.
  142. *)
  143. PROCEDURE EOF (f: File) : BOOLEAN ;
  144. (*
  145. EOLN - tests to see whether a file, f, is about to read a newline.
  146. It does NOT consume the newline. It reads the next character
  147. and then immediately unreads the character.
  148. *)
  149. PROCEDURE EOLN (f: File) : BOOLEAN ;
  150. (*
  151. WasEOLN - tests to see whether a file, f, has just read a newline
  152. character.
  153. *)
  154. PROCEDURE WasEOLN (f: File) : BOOLEAN ;
  155. (*
  156. ReadChar - returns a character read from file, f.
  157. Sensible to check with IsNoError or EOF after calling
  158. this function.
  159. *)
  160. PROCEDURE ReadChar (f: File) : CHAR ;
  161. (*
  162. UnReadChar - replaces a character, ch, back into file, f.
  163. This character must have been read by ReadChar
  164. and it does not allow successive calls. It may
  165. only be called if the previous read was successful,
  166. end of file or end of line seen.
  167. *)
  168. PROCEDURE UnReadChar (f: File ; ch: CHAR) ;
  169. (*
  170. WriteLine - writes out a linefeed to file, f.
  171. *)
  172. PROCEDURE WriteLine (f: File) ;
  173. (*
  174. WriteString - writes a string to file, f.
  175. *)
  176. PROCEDURE WriteString (f: File; a: ARRAY OF CHAR) ;
  177. (*
  178. ReadString - reads a string from file, f, into string, a.
  179. It terminates the string if HIGH is reached or
  180. if a newline is seen or an error occurs.
  181. *)
  182. PROCEDURE ReadString (f: File; VAR a: ARRAY OF CHAR) ;
  183. (*
  184. WriteCardinal - writes a CARDINAL to file, f.
  185. It writes the binary image of the CARDINAL.
  186. to file, f.
  187. *)
  188. PROCEDURE WriteCardinal (f: File; c: CARDINAL) ;
  189. (*
  190. ReadCardinal - reads a CARDINAL from file, f.
  191. It reads a bit image of a CARDINAL
  192. from file, f.
  193. *)
  194. PROCEDURE ReadCardinal (f: File) : CARDINAL ;
  195. (*
  196. GetUnixFileDescriptor - returns the UNIX file descriptor of a file.
  197. Useful when combining FIO.mod with select
  198. (in Selective.def - but note the comments in
  199. Selective about using read/write primatives)
  200. *)
  201. PROCEDURE GetUnixFileDescriptor (f: File) : INTEGER ;
  202. (*
  203. SetPositionFromBeginning - sets the position from the beginning
  204. of the file.
  205. *)
  206. PROCEDURE SetPositionFromBeginning (f: File; pos: LONGINT) ;
  207. (*
  208. SetPositionFromEnd - sets the position from the end of the file.
  209. *)
  210. PROCEDURE SetPositionFromEnd (f: File; pos: LONGINT) ;
  211. (*
  212. FindPosition - returns the current absolute position in file, f.
  213. *)
  214. PROCEDURE FindPosition (f: File) : LONGINT ;
  215. (*
  216. GetFileName - assigns, a, with the filename associated with, f.
  217. *)
  218. PROCEDURE GetFileName (f: File; VAR a: ARRAY OF CHAR) ;
  219. (*
  220. getFileName - returns the address of the filename associated with, f.
  221. *)
  222. PROCEDURE getFileName (f: File) : ADDRESS ;
  223. (*
  224. getFileNameLength - returns the number of characters associated with
  225. filename, f.
  226. *)
  227. PROCEDURE getFileNameLength (f: File) : CARDINAL ;
  228. (*
  229. FlushOutErr - flushes, StdOut, and, StdErr.
  230. *)
  231. PROCEDURE FlushOutErr ;
  232. END FIO.