FileIO-2.def 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. DEFINITION MODULE FileIO;
  2. (* This module attempts to provide several potentially non-portable
  3. facilities for Coco/R.
  4. (a) A general file input/output module, with all routines required for
  5. Coco/R itself, as well as several other that would be useful in
  6. Coco-generated applications.
  7. (b) Definition of the "LONGINT" type needed by Coco.
  8. (c) Some conversion functions to handle this long type.
  9. (d) Some "long" and other constant literals that may be problematic
  10. on some implementations.
  11. (e) Some string handling primitives needed to interface to a variety
  12. of known implementations.
  13. The intention is that the rest of the code of Coco and its generated
  14. parsers should be as portable as possible. Provided the definition
  15. module given, and the associated implementation, satisfy the
  16. specification given here, this should be almost 100% possible (with
  17. the exception of a few constants, avoid changing anything in this
  18. specification).
  19. FileIO is based on code by MB 1990/11/25; heavily modified and extended
  20. by PDT and others between 1992/1/6 and the present day. *)
  21. IMPORT SYSTEM;
  22. TYPE
  23. File; (* Preferably opaque *)
  24. INT32 = LONGINT; (* This may require a special import; on 32 bit
  25. systems INT32 = INTEGER may even suffice. *)
  26. CONST
  27. EOF = 0C; (* FileIO.Read returns EOF when eof is reached. *)
  28. EOL = 36C; (* FileIO.Read maps line marks onto EOL
  29. FileIO.Write maps EOL onto cr, lf, or cr/lf
  30. as appropriate for filing system. *)
  31. ESC = 33C; (* Standard ASCII escape. *)
  32. CR = 15C; (* Standard ASCII carriage return. *)
  33. LF = 12C; (* Standard ASCII line feed. *)
  34. BS = 10C; (* Standard ASCII backspace. *)
  35. DEL = 177C; (* Standard ASCII DEL (rub-out). *)
  36. BitSetSize = 16; (* number of bits actually used in BITSET type *)
  37. Long0 = VAL(INT32, 0); (* Some systems allow 0 or require 0L. *)
  38. Long1 = VAL(INT32, 1); (* Some systems allow 1 or require 1L. *)
  39. Long2 = VAL(INT32, 2); (* Some systems allow 2 or require 2L. *)
  40. FrmExt = ".frm"; (* supplied frame files have this extension. *)
  41. TxtExt = ".txt"; (* generated text files may have this extension. *)
  42. ErrExt = ".err"; (* generated error files may have this extension. *)
  43. DefExt = ".def"; (* generated definition modules have this extension. *)
  44. PasExt = ".pas"; (* generated Pascal units have this extension. *)
  45. ModExt = ".mod"; (* generated implementation/program modules have this
  46. extension. *)
  47. PathSep = ":"; (* separate components in path environment variables
  48. DOS = ";" UNIX = ":" *)
  49. DirSep = "/"; (* separate directory element of file specifiers
  50. DOS = "\" UNIX = "/" *)
  51. VAR
  52. Okay: BOOLEAN; (* Status of last I/O operation. *)
  53. con, err: File; (* Standard terminal and error channels. *)
  54. StdIn, StdOut: File; (* standard input/output - redirectable *)
  55. EOFChar: CHAR; (* Signal EOF interactively *)
  56. (* The following routines provide access to command line parameters and
  57. the environment. *)
  58. PROCEDURE NextParameter (VAR s: ARRAY OF CHAR);
  59. (* Extracts next parameter from command line.
  60. Returns empty string (s[0] = 0C) if no further parameter can be found. *)
  61. PROCEDURE GetEnv (envVar: ARRAY OF CHAR; VAR s: ARRAY OF CHAR);
  62. (* Returns s as the value of environment variable envVar, or empty string
  63. if that variable is not defined. *)
  64. (* The following routines provide a minimal set of file opening routines
  65. and closing routines. *)
  66. PROCEDURE Open (VAR f: File; fileName: ARRAY OF CHAR; newFile: BOOLEAN);
  67. (* Opens file f whose full name is specified by fileName.
  68. Opening mode is specified by newFile:
  69. TRUE: the specified file is opened for output only. Any existing
  70. file with the same name is deleted.
  71. FALSE: the specified file is opened for input only.
  72. FileIO.Okay indicates whether the file f has been opened successfully. *)
  73. PROCEDURE SearchFile (VAR f: File; envVar, fileName: ARRAY OF CHAR;
  74. newFile: BOOLEAN);
  75. (* As for Open, but tries to open file of given fileName by searching each
  76. directory specified by the environment variable named by envVar. *)
  77. PROCEDURE Close (VAR f: File);
  78. (* Closes file f. f becomes NIL.
  79. If possible, Close should be called automatically for all files that
  80. remain open when the application terminates. This will be possible on
  81. implementations that provide some sort of termination or at-exit
  82. facility. *)
  83. PROCEDURE CloseAll;
  84. (* Closes all files opened by Open or SearchFile.
  85. On systems that allow this, CloseAll should be automatically installed
  86. as the termination (at-exit) procedure *)
  87. (* The following utility procedure is not used by Coco, but may be useful.
  88. However, some operating systems may not allow for its implementation. *)
  89. PROCEDURE Delete (VAR f: File);
  90. (* Deletes file f. f becomes NIL. *)
  91. (* The following routines provide a minimal set of file name manipulation
  92. routines. These are modelled after MS-DOS conventions, where a file
  93. specifier is of a form exemplified by D:\DIR\SUBDIR\PRIMARY.EXT
  94. Other conventions may be introduced; these routines are used by Coco to
  95. derive names for the generated modules from the grammar name and the
  96. directory in which the grammar specification is located. *)
  97. PROCEDURE ExtractDirectory (fullName: ARRAY OF CHAR;
  98. VAR directory: ARRAY OF CHAR);
  99. (* Extracts D:\DIRECTORY\ portion of fullName. *)
  100. PROCEDURE ExtractFileName (fullName: ARRAY OF CHAR;
  101. VAR fileName: ARRAY OF CHAR);
  102. (* Extracts PRIMARY.EXT portion of fullName. *)
  103. PROCEDURE AppendExtension (oldName, ext: ARRAY OF CHAR;
  104. VAR newName: ARRAY OF CHAR);
  105. (* Constructs newName as complete file name by appending ext to oldName
  106. if it doesn't end with "." Examples: (assume ext = "EXT")
  107. old.any ==> OLD.EXT
  108. old. ==> OLD.
  109. old ==> OLD.EXT
  110. This is not a file renaming facility, merely a string manipulation
  111. routine. *)
  112. PROCEDURE ChangeExtension (oldName, ext: ARRAY OF CHAR;
  113. VAR newName: ARRAY OF CHAR);
  114. (* Constructs newName as a complete file name by changing extension of
  115. oldName to ext. Examples: (assume ext = "EXT")
  116. old.any ==> OLD.EXT
  117. old. ==> OLD.EXT
  118. old ==> OLD.EXT
  119. This is not a file renaming facility, merely a string manipulation
  120. routine. *)
  121. (* The following routines provide a minimal set of file positioning routines.
  122. Others may be introduced, but at least these should be implemented.
  123. Success of each operation is recorded in FileIO.Okay. *)
  124. PROCEDURE Length (f: File): INT32;
  125. (* Returns length of file f. *)
  126. PROCEDURE GetPos (f: File): INT32;
  127. (* Returns the current read/write position in f. *)
  128. PROCEDURE SetPos (f: File; pos: INT32);
  129. (* Sets the current position for f to pos. *)
  130. (* The following routines provide a minimal set of file rewinding routines.
  131. These two are not currently used by Coco itself.
  132. Success of each operation is recorded in FileIO.Okay *)
  133. PROCEDURE Reset (f: File);
  134. (* Sets the read/write position to the start of the file *)
  135. PROCEDURE Rewrite (f: File);
  136. (* Truncates the file, leaving open for writing *)
  137. (* The following routines provide a minimal set of input routines.
  138. Others may be introduced, but at least these should be implemented.
  139. Success of each operation is recorded in FileIO.Okay. *)
  140. PROCEDURE EndOfLine (f: File): BOOLEAN;
  141. (* TRUE if f is currently at the end of a line, or at end of file. *)
  142. PROCEDURE EndOfFile (f: File): BOOLEAN;
  143. (* TRUE if f is currently at the end of file. *)
  144. PROCEDURE Read (f: File; VAR ch: CHAR);
  145. (* Reads a character ch from file f.
  146. Maps filing system line mark sequence to FileIO.EOL. *)
  147. PROCEDURE ReadAgain (f: File);
  148. (* Prepares to re-read the last character read from f.
  149. There is no buffer, so at most one character can be re-read. *)
  150. PROCEDURE ReadLn (f: File);
  151. (* Reads to start of next line on file f, or to end of file if no next
  152. line. Skips to, and consumes next line mark. *)
  153. PROCEDURE ReadString (f: File; VAR str: ARRAY OF CHAR);
  154. (* Reads a string of characters from file f.
  155. Leading blanks are skipped, and str is delimited by line mark.
  156. Line mark is not consumed. *)
  157. PROCEDURE ReadLine (f: File; VAR str: ARRAY OF CHAR);
  158. (* Reads a string of characters from file f.
  159. Leading blanks are not skipped, and str is terminated by line mark or
  160. control character, which is not consumed. *)
  161. PROCEDURE ReadToken (f: File; VAR str: ARRAY OF CHAR);
  162. (* Reads a string of characters from file f.
  163. Leading blanks and line feeds are skipped, and token is terminated by a
  164. character <= ' ', which is not consumed. *)
  165. PROCEDURE ReadInt (f: File; VAR i: INTEGER);
  166. (* Reads an integer value from file f. *)
  167. PROCEDURE ReadCard (f: File; VAR i: CARDINAL);
  168. (* Reads a cardinal value from file f. *)
  169. PROCEDURE ReadBytes (f: File; VAR buf: ARRAY OF SYSTEM.BYTE; VAR len: CARDINAL);
  170. (* Attempts to read len bytes from the current file position into buf.
  171. After the call, len contains the number of bytes actually read. *)
  172. (* The following routines provide a minimal set of output routines.
  173. Others may be introduced, but at least these should be implemented. *)
  174. PROCEDURE Write (f: File; ch: CHAR);
  175. (* Writes a character ch to file f.
  176. If ch = FileIO.EOL, writes line mark appropriate to filing system. *)
  177. PROCEDURE WriteLn (f: File);
  178. (* Skips to the start of the next line on file f.
  179. Writes line mark appropriate to filing system. *)
  180. PROCEDURE WriteString (f: File; str: ARRAY OF CHAR);
  181. (* Writes entire string str to file f. *)
  182. PROCEDURE WriteText (f: File; text: ARRAY OF CHAR; len: INTEGER);
  183. (* Writes text to file f.
  184. At most len characters are written. Trailing spaces are introduced
  185. if necessary (thus providing left justification). *)
  186. PROCEDURE WriteInt (f: File; int: INTEGER; wid: CARDINAL);
  187. (* Writes an INTEGER int into a field of wid characters width.
  188. If the number does not fit into wid characters, wid is expanded.
  189. If wid = 0, exactly one leading space is introduced. *)
  190. PROCEDURE WriteCard (f: File; card, wid: CARDINAL);
  191. (* Writes a CARDINAL card into a field of wid characters width.
  192. If the number does not fit into wid characters, wid is expanded.
  193. If wid = 0, exactly one leading space is introduced. *)
  194. PROCEDURE WriteBytes (f: File; VAR buf: ARRAY OF SYSTEM.BYTE; len: CARDINAL);
  195. (* Writes len bytes from buf to f at the current file position. *)
  196. (* The following procedures are not currently used by Coco, and may be
  197. safely omitted, or implemented as null procedures. They might be
  198. useful in measuring performance. *)
  199. PROCEDURE WriteDate (f: File);
  200. (* Write current date DD/MM/YYYY to file f. *)
  201. PROCEDURE WriteTime (f: File);
  202. (* Write time HH:MM:SS to file f. *)
  203. PROCEDURE WriteElapsedTime (f: File);
  204. (* Write elapsed time in seconds since last call of this procedure. *)
  205. PROCEDURE WriteExecutionTime (f: File);
  206. (* Write total execution time in seconds thus far to file f. *)
  207. (* The following procedures are a minimal set used within Coco for
  208. string manipulation. They almost follow the conventions of the ISO
  209. routines, and are provided here to interface onto whatever Strings
  210. library is available. On ISO compilers it should be possible to
  211. implement most of these with CONST declarations, and even replace
  212. SLENGTH with the pervasive function LENGTH at the points where it is
  213. called.
  214. CONST
  215. SLENGTH = Strings.Length;
  216. Assign = Strings.Assign;
  217. Extract = Strings.Extract;
  218. Concat = Strings.Concat;
  219. *)
  220. PROCEDURE SLENGTH (stringVal: ARRAY OF CHAR): CARDINAL;
  221. (* Returns number of characters in stringVal, not including nul *)
  222. PROCEDURE Assign (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR);
  223. (* Copies as much of source to destination as possible, truncating if too
  224. long, and nul terminating if shorter.
  225. Be careful - some libraries have the parameters reversed! *)
  226. PROCEDURE Extract (source: ARRAY OF CHAR;
  227. startIndex, numberToExtract: CARDINAL;
  228. VAR destination: ARRAY OF CHAR);
  229. (* Extracts at most numberToExtract characters from source[startIndex]
  230. to destination. If source is too short, fewer will be extracted, even
  231. zero perhaps *)
  232. PROCEDURE Concat (stringVal1, stringVal2: ARRAY OF CHAR;
  233. VAR destination: ARRAY OF CHAR);
  234. (* Concatenates stringVal1 and stringVal2 to form destination.
  235. Nul terminated if concatenation is short enough, truncated if it is
  236. too long *)
  237. PROCEDURE Compare (stringVal1, stringVal2: ARRAY OF CHAR): INTEGER;
  238. (* Returns -1, 0, 1 depending whether stringVal1 < = > stringVal2.
  239. This is not directly ISO compatible *)
  240. (* The following routines are for conversions to and from the INT32 type.
  241. Their names are modelled after the ISO pervasive routines that would
  242. achieve the same end. Where possible, replacing calls to these routines
  243. by the pervasives would improve performance markedly. As used in Coco,
  244. these routines should not give range problems. *)
  245. PROCEDURE ORDL (n: INT32): CARDINAL;
  246. (* Convert long integer n to corresponding (short) cardinal value.
  247. Potentially FileIO.ORDL(n) = VAL(CARDINAL, n) *)
  248. PROCEDURE INTL (n: INT32): INTEGER;
  249. (* Convert long integer n to corresponding short integer value.
  250. Potentially FileIO.INTL(n) = VAL(INTEGER, n) *)
  251. PROCEDURE INT (n: CARDINAL): INT32;
  252. (* Convert cardinal n to corresponding long integer value.
  253. Potentially FileIO.INT(n) = VAL(INT32, n) *)
  254. PROCEDURE QuitExecution;
  255. (* Close all files and halt execution.
  256. On some implementations QuitExecution will be simply implemented as HALT *)
  257. END FileIO.