WIRTHISO 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. From Wirth to ISO - for those used to gpm's 'Wirth' I/O library (InOut, etc),
  2. an introduction to the ISO Standard Modula-2 I/O libraries.
  3. =============================================================================
  4. Simple I/O from stdin and to stdout
  5. -----------------------------------
  6. InOut is replaced by
  7. - STextIO for ReadChar, WriteChar, ReadString, WriteString and WriteLn;
  8. it adds SkipLine, ReadRestLine, ReadToken.
  9. - SWholeIO for ReadCard, ReadInt, WriteCard, WriteInt.
  10. - SIOResult for ReadResult.
  11. RealInOut is replaced by
  12. - SRealIO
  13. So the changes are:
  14. - minor name changes (the modules, and ReadChar/WriteChar)
  15. - no Done; instead check for SIOResult.ReadResult() = allRight;
  16. the full SIOResult.ReadResults enumeration is:
  17. notKnown, (* no read result is set *)
  18. allRight, (* data is as expected or as required *)
  19. outOfRange, (* data cannot be represented *)
  20. wrongFormat, (* data not in expected form *)
  21. endOfLine, (* end of line seen before expected data *)
  22. endOfInput (* end of input seen before expected data *)
  23. examples are:
  24. outOfRange: a string too long to fit the array supplied,
  25. negative value for cardinal, numberic value too largeo;
  26. wrongFormat: non-numeric input to ReadCard/Int;
  27. endOfLine: see below
  28. - no TermCh; thus Read procedures consume only what they were intended to, and
  29. the 'terminating character' is still to be read; it can be checked without
  30. consuming it by IOChan.Look, which returns a ReadResults and a CHAR
  31. - the endOfLine value of ReadResults highlights a significant (unfortunate?)
  32. change: Read procedures will not read through an end-of-line; in particular,
  33. ReadChar will return endOfLine, and an undefined ch parameter, and will
  34. continue to do so on successive calls; ReadCard/Int, though they skip through
  35. leading spaces (and tabs) will similarly fail repeatedly if they encounter
  36. an end-of-line before a valid number is found.
  37. To consume an end-of-line, you must use SkipLine, which discards all
  38. characters up to and including the next end-of-line (cf Pascal Readln()).
  39. This slightly complicates processing loops; for examples see USINGISO.
  40. It does mean that a common bug will be an infinite loop due to omitting the
  41. endOfLine handling, causing a Read loop to block at an end-of-line.
  42. I/O from/to files.
  43. -----------------
  44. The simplest way is to redirect stdin and stdout using command-line
  45. redirection (<, >); all the SxxxxIO facilities still apply.
  46. For access to multiple files:
  47. UxFiles is replaced by
  48. - StreamFile for Open and Close
  49. TextInOut is replaced by
  50. - TextIO, WholeIO, IOResult
  51. - RealIO
  52. Open takes a file name and an open mode flag set, and returns an opaque ChanId
  53. (cf UxFiles FILE) and an OpenResults enumeration value.
  54. Streamfile exports various singleton values of FlagSet, to allow for example,
  55. read+write:
  56. CONST
  57. read = FlagSet{readFlag}; (* input operations are requested/available *)
  58. write = FlagSet{writeFlag}; (* output operations are requested/available *)
  59. old = FlagSet{oldFlag}; (* a file may/must/did exist before the channel
  60. was opened *)
  61. text = FlagSet{textFlag}; (* text operations are requested/available *)
  62. raw = FlagSet{rawFlag}; (* raw operations are requested/available *)
  63. with some useful defaults: unless binary is set, text is implied, and if read
  64. is set, old is implied. Again, some examples such as those in USINGISO help.
  65. The OpenResults values are:
  66. TYPE
  67. OpenResults = (
  68. opened,
  69. wrongNameFormat,
  70. wrongFlags,
  71. tooManyOpen,
  72. outOfChans,
  73. wrongPermissions,
  74. noRoomOnDevice,
  75. noSuchFile,
  76. fileExists,
  77. wrongFileType,
  78. noTextOperations,
  79. noRawOperations,
  80. noMixedOperations,
  81. alreadyOpen,
  82. otherProblem
  83. );
  84. see the DEF file for details; usually just a check for "opened" suffices.
  85. The TextIO, WholeIO, IOResult and RealIO modules export exactly the same
  86. facilities as their "S" versions, but take an additional first ChanId
  87. parameter.
  88. StdError is replaced by TextIO/WholeIO/RealIO using the channel id
  89. StdChans.StdErrChan().
  90. Other data streams
  91. ------------------
  92. StreamFile makes files (and standard channels such as stdin, stdout, stderr,
  93. null and bad) accessible as "sequential data streams"; other libraries
  94. offering similar interfaces are:
  95. RndFile "random access files" - OpenOld, OpenClean, StartPos, CurrentPos,
  96. EndpOs, NewPos, SetPos, Close
  97. SeqFile "rewindable sequential files" - OpenWrite/OpenAppend/OpenRead, Reread,
  98. Rewrite, Close
  99. TermFile "the single terminal device" - echo flag - see below
  100. Terminal I/O
  101. ------------
  102. Terminal is replaced by
  103. - TermFile plus normal I/O modules
  104. The Standard treats terminal I/O by the TermFile device module; having opened
  105. the implied device it returns a ChanId for use in TextIO, WholeIO, etc calls.
  106. The Open call allows an extra flag: echo. If echo is set, "single character
  107. mode" is used for all calls - input is not buffered, and echoing is controlled
  108. by the user: a Read will echo the character as it is read, but a Look, Skip
  109. pair will remove it without echo.
  110. If echo is not set, "line mode" is used - input is buffered and echoed, just
  111. as for StreamFile input.
  112. Thus, the functionality of Terminal.GetKeyStroke is provided by opening the
  113. terminal in single-sharacter mode and using Look/Skip.
  114. Since the current implementation buffers StreamFile and TermFile line mode
  115. input, care should be exercised when mixing these with single character mode.
  116. Recommendations: if single character mode is to be used, use only TermFile;
  117. multiple channels can be opened with TermFile at one time, allowing mixed
  118. line and single character input; however the user should ensure that an entire
  119. line is read in line mode (ie the buffer is empty) before using single
  120. character mode.
  121. There is no equivalent of Terminal.WasKeyPressed - we are currently looking at
  122. implementing it as an extension in TermDevice - see below.
  123. Additions/Omissions
  124. -------------------
  125. The Standard includes no provision for deleting a file (Open procedures will
  126. create a file if it does not exist and old is not required).
  127. We have provided Delete in StdDevice - one of the implementation-dependant
  128. modules which also provide the low-level implementation of the facilities of
  129. StreamFile, TextIO, etc; apart from Delete, users should have no need for
  130. StdDevice procedures. Users should stick to the StreamFile/TextIO/... level,
  131. with the occasional need to use the IOChan level.
  132. Raw (binary) I/O
  133. ----------------
  134. to come