PascalIO.def 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. DEFINITION MODULE PascalIO;
  2. (*
  3. Module: Pascal-like Input/Output
  4. Author: Ceriel J.H. Jacobs
  5. Version: $Id$
  6. This module provides for I/O that is essentially equivalent to the I/O
  7. provided by Pascal with "text", or "file of char".
  8. Output buffers are automatically flushed at program termination.
  9. The CloseOutput routine is just there for compatibility with earlier
  10. versions of this module.
  11. *)
  12. CONST Eos = 0C; (* End of string character *)
  13. TYPE Text;
  14. VAR Input, Output: Text; (* standard input and standard output available
  15. immediately.
  16. Standard output is not buffered when
  17. connected to a terminal.
  18. *)
  19. VAR Notext: Text; (* Initialize your Text variables with this *)
  20. PROCEDURE Reset(VAR InputText: Text; Filename: ARRAY OF CHAR);
  21. (* When InputText indicates an open textfile, it is first flushed
  22. and closed. Then, the file indicated by "Filename" is opened for reading.
  23. If this fails, a runtime error results. Otherwise, InputText is
  24. associated with the new input file.
  25. *)
  26. PROCEDURE Rewrite(VAR OutputText: Text; Filename: ARRAY OF CHAR);
  27. (* When OutputText indicates an open textfile, it is first flushed
  28. and closed. Then, the file indicated by "Filename" is opened for writing.
  29. If this fails, a runtime error results. Otherwise, OutputText is
  30. associated with the new output file.
  31. *)
  32. PROCEDURE CloseOutput();
  33. (* To be called at the end of the program, to flush all output buffers. *)
  34. (***************************************************************************
  35. Input routines;
  36. All these routines result in a runtime error when not called with either
  37. "Input", or a "Text" value obtained by Reset.
  38. Also, the routines that actually advance the "read pointer", result in a
  39. runtime error when end of file is reached prematurely.
  40. ****************************************************************************)
  41. PROCEDURE NextChar(InputText: Text): CHAR;
  42. (* Returns the next character from the InputText, 0C on end of file.
  43. Does not advance the "read pointer", so behaves much like "input^"
  44. in Pascal. However, unlike Pascal, if Eoln(InputText) is TRUE, it
  45. returns the newline character, rather than a space.
  46. *)
  47. PROCEDURE Get(InputText: Text);
  48. (* Advances the "read pointer" by one character. *)
  49. PROCEDURE Eoln(InputText: Text): BOOLEAN;
  50. (* Returns TRUE if the next character from the InputText is a linefeed.
  51. Unlike Pascal however, it does not produce a runtime error when
  52. called when Eof(InputText) is TRUE.
  53. *)
  54. PROCEDURE Eof(InputText: Text): BOOLEAN;
  55. (* Returns TRUE if the end of the InputText is reached. *)
  56. PROCEDURE ReadChar(InputText: Text; VAR Char: CHAR);
  57. (* Read a character from the InputText, and leave the result in "Char".
  58. Unlike Pascal, if Eoln(InputText) is TRUE, the newline character
  59. is put in "Char".
  60. *)
  61. PROCEDURE ReadLn(InputText: Text);
  62. (* Skip the rest of the current line from the InputText,
  63. including the linefeed.
  64. *)
  65. PROCEDURE ReadInteger(InputText: Text; VAR Integer: INTEGER);
  66. (* Skip leading blanks, read an optionally signed integer from the
  67. InputText, and leave the result in "Integer".
  68. If no integer is read, or when overflow occurs, a runtime error results.
  69. Input stops at the character following the integer.
  70. *)
  71. PROCEDURE ReadCardinal(InputText: Text; VAR Cardinal: CARDINAL);
  72. (* Skip leading blanks, read a cardinal from the InputText, and leave the
  73. result in "Cardinal".
  74. If no cardinal is read, or when overflow occurs, a runtime error results.
  75. Input stops at the character following the cardinal.
  76. *)
  77. PROCEDURE ReadReal(InputText: Text; VAR Real: REAL);
  78. (* Skip leading blanks, read a real from the InputText, and leave the
  79. result in "Real".
  80. Syntax:
  81. real --> [(+|-)] digit {digit} [. digit {digit}]
  82. [ E [(+|-)] digit {digit} ]
  83. If no real is read, or when overflow/underflow occurs, a runtime error
  84. results.
  85. Input stops at the character following the real.
  86. *)
  87. PROCEDURE ReadLongReal(InputText: Text; VAR Real: LONGREAL);
  88. (* Like ReadReal, but for LONGREAL *)
  89. (***************************************************************************
  90. Output routines;
  91. All these routines result in a runtime error when not called with either
  92. "Output", or a "Text" value obtained by Rewrite.
  93. ****************************************************************************)
  94. PROCEDURE WriteChar(OutputText: Text; Char: CHAR);
  95. (* Writes the character "Char" to the OutputText. *)
  96. PROCEDURE WriteLn(OutputText: Text);
  97. (* Writes a linefeed to the OutputText. *)
  98. PROCEDURE Page(OutputText: Text);
  99. (* Writes a form-feed to the OutputText *)
  100. PROCEDURE WriteInteger(OutputText: Text; Integer: INTEGER; Width: CARDINAL);
  101. (* Write integer "Integer" to the OutputText, using at least "Width" places,
  102. blank-padding to the left if needed.
  103. *)
  104. PROCEDURE WriteCardinal(OutputText: Text; Cardinal, Width: CARDINAL);
  105. (* Write cardinal "Cardinal" to the OutputText, using at least
  106. "Width" places, blank-padding to the left if needed.
  107. *)
  108. PROCEDURE WriteBoolean(OutputText: Text; Boolean: BOOLEAN; Width: CARDINAL);
  109. (* Write boolean "Boolean" to the OutputText, using at least "Width" places,
  110. blank-padding to the left if needed.
  111. Equivalent to WriteString(OutputText, " TRUE", Width), or
  112. WriteString(OutputText, "FALSE", Width).
  113. *)
  114. PROCEDURE WriteString(OutputText: Text;
  115. String: ARRAY OF CHAR; Width: CARDINAL);
  116. (* Write string "String" to the OutputText, using at least "Width" places,
  117. blank-padding to the left if needed.
  118. The string is terminated either by the character Eos, or by the upperbound
  119. of the array "String".
  120. *)
  121. PROCEDURE WriteReal(OutputText: Text; Real: REAL; Width, Nfrac: CARDINAL);
  122. (* Write real "Real" to the OutputText. If "Nfrac" = 0, use scientific
  123. notation, otherwise use fixed-point notation with "Nfrac" digits behind
  124. the dot.
  125. Always use at least "Width" places, blank-padding to the left if needed.
  126. *)
  127. PROCEDURE WriteLongReal(OutputText: Text; Real: LONGREAL;
  128. Width, Nfrac: CARDINAL);
  129. (* Like WriteReal, but for LONGREAL *)
  130. END PascalIO.