Streams.def 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. DEFINITION MODULE Streams;
  2. (*
  3. Module: Stream Input/Output
  4. Author: Ceriel J.H. Jacobs
  5. Version: $Id$
  6. * This module provides sequential IO through streams.
  7. * A stream is either a text stream or a binary stream, and is either in
  8. * reading, writing or appending mode.
  9. * By default, there are three open text streams, connected to standard input,
  10. * standard output, and standard error respectively.
  11. * These are text streams.
  12. * When connected to a terminal, the standard output and standard error
  13. * streams are linebuffered.
  14. * The user can create more streams with the OpenStream call, and
  15. * delete streams with the CloseStream call.
  16. * Streams are automatically closed at program termination.
  17. *)
  18. FROM SYSTEM IMPORT BYTE;
  19. TYPE StreamKind = (text, binary, none);
  20. StreamMode = (reading, writing, appending);
  21. StreamResult = (succeeded, illegaloperation,
  22. nomemory, openfailed, nostream, endoffile);
  23. StreamBuffering = (unbuffered, linebuffered, blockbuffered);
  24. TYPE Stream;
  25. VAR InputStream, OutputStream, ErrorStream: Stream;
  26. PROCEDURE OpenStream(VAR stream: Stream;
  27. filename: ARRAY OF CHAR;
  28. kind: StreamKind;
  29. mode: StreamMode;
  30. VAR result: StreamResult);
  31. (* Associates a stream with the file named filename.
  32. If kind = none, result is set to illegaloperation.
  33. mode has one of the follwing values:
  34. reading: the file is opened for reading
  35. writing: the file is truncated or created for writing
  36. appending: the file is opened for writing at end of file,
  37. or created for writing.
  38. On failure, result is set to openfailed.
  39. On success, result is set to succeeded.
  40. *)
  41. PROCEDURE SetStreamBuffering( stream: Stream;
  42. b: StreamBuffering;
  43. VAR result: StreamResult);
  44. (* This procedure is only allowed for output streams.
  45. The three types of buffering available are unbuffered, linebuffered,
  46. and blockbuffered. When an output stream is unbuffered, the output
  47. appears as soon as written; when it is blockbuffered, output is saved
  48. up and written as a block; When it is linebufferded (only possible for
  49. text output streams), output is saved up until a newline is encountered
  50. or input is read from standard input.
  51. *)
  52. PROCEDURE CloseStream(VAR stream: Stream; VAR result: StreamResult);
  53. (* Closes the stream.
  54. result is set to nostream if "stream" was not associated with a stream.
  55. *)
  56. PROCEDURE FlushStream(stream: Stream; VAR result: StreamResult);
  57. (* Flushes the stream.
  58. result is set to nostream if "stream" was not associated with a stream.
  59. It is set to illegaloperation if "stream" is not an output or
  60. appending stream.
  61. *)
  62. PROCEDURE EndOfStream(stream: Stream; VAR result: StreamResult): BOOLEAN;
  63. (* Returns true if the stream is an input stream, and the end of the file
  64. has been reached.
  65. result is set to nostream if "stream" was not associated with a stream.
  66. It is set to illegaloperation if the stream is not an input stream.
  67. *)
  68. PROCEDURE Read(stream: Stream; VAR ch: CHAR; VAR result: StreamResult);
  69. (* reads a character from the stream. Certain character translations may
  70. occur, such as the mapping of the end-of-line sequence to the
  71. character 12C.
  72. result is set to nostream if "stream" was not associated with a stream.
  73. It is set to endoffile if EndOfStream would have returned TRUE before the
  74. call to Read. In this case, "ch" is set to 0C.
  75. It is set to illegaloperation if the stream is not a text input stream.
  76. *)
  77. PROCEDURE ReadByte(stream: Stream; VAR byte: BYTE; VAR result: StreamResult);
  78. (* reads a byte from the stream. No character translations occur.
  79. result is set to nostream if "stream" was not associated with a stream.
  80. It is set to endoffile if EndOfStream would have returned TRUE before the
  81. call to ReadByte. In this case, "byte" is set to 0C.
  82. It is set to illegaloperation if the stream is not a binary input stream.
  83. *)
  84. PROCEDURE ReadBytes(stream: Stream;
  85. VAR bytes: ARRAY OF BYTE;
  86. VAR result: StreamResult);
  87. (* reads bytes from the stream. No character translations occur.
  88. The number of bytes is determined by the size of the parameter.
  89. result is set to nostream if "stream" was not associated with a stream.
  90. It is set to endoffile if there are not enough bytes left on the stream.
  91. In this case, the rest of the bytes are set to 0C.
  92. It is set to illegaloperation if the stream is not a binary input stream.
  93. *)
  94. PROCEDURE Write(stream: Stream; ch: CHAR; VAR result: StreamResult);
  95. (* writes a character to the stream. Certain character translations may
  96. occur, such as the mapping of a line-feed or carriage return (12C or 15C)
  97. to the end-of-line sequence of the system.
  98. result is set to nostream if "stream" was not associated with a stream.
  99. It is set to illegaloperation if the stream is not a text output stream.
  100. *)
  101. PROCEDURE WriteByte(stream: Stream; byte: BYTE; VAR result: StreamResult);
  102. (* writes a byte to the stream. No character translations occur.
  103. result is set to nostream if "stream" was not associated with a stream.
  104. It is set to illegaloperation if the stream is not a binary output stream.
  105. *)
  106. PROCEDURE WriteBytes(stream: Stream;
  107. bytes: ARRAY OF BYTE;
  108. VAR result: StreamResult);
  109. (* writes bytes to the stream. No character translations occur.
  110. The number of bytes written is equal to the size of the parameter.
  111. result is set to nostream if "stream" was not associated with a stream.
  112. It is set to illegaloperation if the stream is not a binary output stream.
  113. *)
  114. PROCEDURE GetPosition(stream: Stream;
  115. VAR position: LONGINT;
  116. VAR result: StreamResult);
  117. (* gives the actual read/write position in "position".
  118. "result" is set to illegaloperation if "stream" is not a stream.
  119. *)
  120. PROCEDURE SetPosition(stream: Stream;
  121. position: LONGINT;
  122. VAR result: StreamResult);
  123. (* sets the actual read/write position to "position".
  124. "result" is set to nostream if "stream" is not a stream.
  125. It is set to illegaloperation if the stream was opened for appending and
  126. the position is in front of the current position, or it failed for some
  127. other reason (f.i. when the stream is connected to a terminal).
  128. *)
  129. PROCEDURE isatty(stream: Stream; VAR result: StreamResult): BOOLEAN;
  130. (* return TRUE if the stream is connected to a terminal.
  131. "result" is set to nostream if "stream" is not a stream, and
  132. set to illegaloperation if the operation failed for some other reason.
  133. *)
  134. END Streams.