<p><em>Streams</em> provide flexible access to <strong class="pkg">GAP</strong>'s input and output processing. An input stream takes characters from some source and delivers them to GAP which reads them from the stream. When an input stream has delivered all characters it is at end-of-stream. An output stream receives characters from GAP which writes them to the stream, and delivers them to some destination.
<p>A major use of streams is to provide efficient and flexible access to files. Files can be read and written using <code class="func">Read</code> (<a href="chap9.html#X8373AC6B7D5F9167"><span class="RefLink">9.8-1</span></a>) and <code class="func">AppendTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), however the former only allows a complete file to be read as <strong class="pkg">GAP</strong> input and the latter imposes a high time penalty if many small pieces of output are written to a large file. Streams allow input files in other formats to be read and processed, and files to be built up efficiently from small pieces of output. Streams may also be used for other purposes, for example to read from and print to <strong class="pkg">GAP</strong> strings, or to read input directly from the user.</p>
<p>Any stream is either a <em>text stream</em>, which translates the <code class="code">end-of-line</code> character (<code class="code">\n</code>) to or from the system's representation of end-of-line (e.g., new-line under UNIX and carriage-return-new-line under DOS), or a binary stream, which does not translate the end-of-line character. The processing of other unprintable characters by text streams is undefined. Binary streams pass them unchanged.
<p>Whereas it is cheap to append to a stream, streams do consume system resources, and only a limited number can be open at any time, therefore it is necessary to close a stream as soon as possible using <code class="func">CloseStream</code> (<a href="chap10.html#X786E5520803FDE00"><span class="RefLink">10.2-1</span></a>). If creating a stream failed then <code class="func">LastSystemError</code> (<a href="chap9.html#X87D278437A916905"><span class="RefLink">9.1-1</span></a>) can be used to get information about the failure.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsStream</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p>Streams are <strong class="pkg">GAP</strong> objects and all open streams, input, output, text and binary, lie in this category.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsClosedStream</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p>When a stream is closed, its type changes to lie in <code class="func">IsClosedStream</code>. This category is used to install methods that trap accesses to closed streams.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsInputStream</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p>All input streams lie in this category, and support input operations such as <code class="func">ReadByte</code> (<a href="chap10.html#X79E1E6A57AE58BB8"><span class="RefLink">10.3-3</span></a>) (see <a href="chap10.html#X7D1D33A587BFD93D"><span class="RefLink">10.3</span></a>)</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsInputTextNone</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p>It is convenient to use a category to distinguish dummy streams (see <a href="chap10.html#X8724699C7D67BA47"><span class="RefLink">10.9</span></a>) from others. Other distinctions are usually made using representations</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsOutputTextNone</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p>It is convenient to use a category to distinguish dummy streams (see <a href="chap10.html#X8724699C7D67BA47"><span class="RefLink">10.9</span></a>) from others. Other distinctions are usually made using representations</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ CloseStream</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>In order to preserve system resources and to flush output streams every stream should be closed as soon as it is no longer used using <code class="func">CloseStream</code>.</p>
<p>It is an error to try to read characters from or write characters to a closed stream. Closing a stream tells the <strong class="pkg">GAP</strong> kernel and/or the operating system kernel that the file is no longer needed. This may be necessary because the <strong class="pkg">GAP</strong> kernel and/or the operating system may impose a limit on how many streams may be open simultaneously.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ FileDescriptorOfStream</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>returns the UNIX file descriptor of the underlying file. This is mainly useful for the <code class="func">UNIXSelect</code> (<a href="chap10.html#X87BC257A78F96828"><span class="RefLink">10.2-3</span></a>) function call. This is as of now only available on UNIX-like operating systems and only for streams to local processes and local files.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ UNIXSelect</code>( <var class="Arg">inlist</var>, <var class="Arg">outlist</var>, <var class="Arg">exclist</var>, <var class="Arg">timeoutsec</var>, <var class="Arg">timeoutusec</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>makes the UNIX C-library function <code class="code">select</code> accessible from <strong class="pkg">GAP</strong> for streams. The functionality is as described in the man page (see UNIX file descriptors (integers) for streams. They can be obtained via <code class="func">FileDescriptorOfStream</code> (<a href="chap10.html#X7F0459287E717456"><span class="RefLink">10.2-2</span></a>) for streams to local processes and to local files. The argument <var class="Arg">timeoutsec</var> is a timeout in seconds as in the <code class="code">struct timeval</code> on the C level. The argument <var class="Arg">timeoutusec</var> is analogously in microseconds. The total timeout is the sum of both. If one of those timeout arguments is not a small integer then no timeout is applicable (<code class="keyw">fail</code> is allowed for the timeout arguments).</p>
<p>The return value is the number of streams that are ready, this may be 0 if a timeout was specified. All file descriptors in the three lists that are not yet ready are replaced by <code class="keyw">fail</code> in this function. So the lists are changed!</p>
<p>This function is only available if your operating system has <code class="code">select</code>, which is detected during compilation of <strong class="pkg">GAP</strong>.</p>
<h4>10.3 <span class="Heading">Operations for Input Streams</span></h4>
<p>Two operations normally used to read files: <code class="func">Read</code> (<a href="chap9.html#X8373AC6B7D5F9167"><span class="RefLink">9.8-1</span></a>) and <code class="func">ReadAsFunction</code> (<a href="chap9.html#X7824CB7D7D4BAFBC"><span class="RefLink">9.8-2</span></a>) can also be used to read <strong class="pkg">GAP</strong> input from a stream. The input is immediately parsed and executed. When reading from a stream <var class="Arg">str</var>, the <strong class="pkg">GAP</strong> kernel generates calls to <code class="code">ReadLine(<var class="Arg">str</var>)</code> to supply text to the parser.</p>
<p>Three further operations: <code class="func">ReadByte</code> (<a href="chap10.html#X79E1E6A57AE58BB8"><span class="RefLink">10.3-3</span></a>), <code class="func">ReadLine</code> (<a href="chap10.html#X7D2CA44C7D110C4F"><span class="RefLink">10.3-4</span></a>) and <code class="func">ReadAll</code> (<a href="chap10.html#X85C603D7867430D0"><span class="RefLink">10.3-5</span></a>), support reading characters from an input stream without parsing them. This can be used to read data in any format and process it in <strong class="pkg">GAP</strong>.</p>
<p>Additional operations for input streams support detection of end of stream, and (for those streams for which it is appropriate) random access to the data.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ReadAsFunction</code>( <var class="Arg">input-text-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>reads the input-text-stream as function and returns this function. See <a href="chap9.html#X81A0A4FF842B039B"><span class="RefLink">9.8</span></a> for details.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput"># a function with local `a' does not change the global one
<span class="GAPprompt">gap></span> <span class="GAPinput">a := 1;;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">i := InputTextString( "local a; a := 10; return a*10;" );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">ReadAsFunction(i)();</span>
100
<span class="GAPprompt">gap></span> <span class="GAPinput">a;</span>
1
<span class="GAPprompt">gap></span> <span class="GAPinput"># reading it via `Read' does
<span class="GAPprompt">gap></span> <span class="GAPinput">i := InputTextString( "a := 10;" );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Read(i);</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">a;</span>
10
</pre></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ReadByte</code>( <var class="Arg">input-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">ReadByte</code> returns one character (returned as integer) from the inputstream <var class="Arg">input-stream</var>. <code class="func">ReadByte</code> returns <code class="keyw">fail</code> if there is no character available, in particular if it is at the end of a file.</p>
<p>If <var class="Arg">input-stream</var> is the input stream of a input/output process, <code class="func">ReadByte</code> may also return <code class="keyw">fail</code> if no byte is currently available.</p>
<p><code class="func">ReadByte</code> is the basic operation for input streams. If a <code class="func">ReadByte</code> method is installed for a user-defined type of stream which does not block, then all the other input stream operations will work (although possibly not at peak efficiency).</p>
<p><code class="func">ReadByte</code> will wait (block) until a byte is available. For instance if the stream is a connection to another process, it will wait for the process to output a byte.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ReadLine</code>( <var class="Arg">input-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">ReadLine</code> returns one line (returned as string <em>with</em> the newline) from the input stream <var class="Arg">input-stream</var>. <code class="func">ReadLine</code> reads in the input until a newline is read or the end-of-stream is encountered.</p>
<p>If <var class="Arg">input-stream</var> is the input stream of a input/output process, <code class="func">ReadLine</code> may also return <code class="keyw">fail</code> or return an incomplete line if the other process has not yet written any more. It will always wait (block) for at least one byte to be available, but will then return as much input as is available, up to a limit of one line.</p>
<p>A default method is supplied for <code class="func">ReadLine</code> which simply calls <code class="func">ReadByte</code> (<a href="chap10.html#X79E1E6A57AE58BB8"><span class="RefLink">10.3-3</span></a>) repeatedly. This is only safe for streams that cannot block. The kernel uses calls to <code class="func">ReadLine</code> to supply input to the parser when reading from a stream.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ReadAll</code>( <var class="Arg">input-stream</var>[, <var class="Arg">limit</var>] )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">ReadAll</code> returns all characters as string from the input stream <var class="Arg">stream-in</var>. It waits (blocks) until at least one character is available from the stream, or until there is evidence that no characters will ever be available again. This last indicates that the stream is at end-of-stream. Otherwise, it reads as much input as it can from the stream without blocking further and returns it to the user. If the stream is already at end of file, so that no bytes are available, <code class="keyw">fail</code> is returned. In the case of a file stream connected to a normal file (not a pseudo-tty or named pipe or similar), all the bytes should be immediately available and this function will read the remainder of the file.</p>
<p>With a second argument, at most <var class="Arg">limit</var> bytes will be returned. Depending on the stream a bounded number of additional bytes may have been read into an internal buffer.</p>
<p>A default method is supplied for <code class="func">ReadAll</code> which simply calls <code class="func">ReadLine</code> (<a href="chap10.html#X7D2CA44C7D110C4F"><span class="RefLink">10.3-4</span></a>) repeatedly. This is only really safe for streams which cannot block. Other streams should install a method for <code class="func">ReadAll</code>.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsEndOfStream</code>( <var class="Arg">input-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">IsEndOfStream</code> returns <code class="keyw">true</code> if the input stream is at <em>end-of-stream</em>, and <code class="keyw">false</code> otherwise. Note that <code class="func">IsEndOfStream</code> might return <code class="keyw">false</code> even if the next <code class="func">ReadByte</code> (<a href="chap10.html#X79E1E6A57AE58BB8"><span class="RefLink">10.3-3</span></a>) fails.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ PositionStream</code>( <var class="Arg">input-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>Some input streams, such as string streams and file streams attached to disk files, support a form of random access by way of the operations <code class="func">PositionStream</code>, <code class="func">SeekPositionStream</code> (<a href="chap10.html#X7A60AD8C7E0D0507"><span class="RefLink">10.3-9</span></a>) and <code class="func">RewindStream</code> (<a href="chap10.html#X7A777E1186EB330B"><span class="RefLink">10.3-8</span></a>). <code class="func">PositionStream</code> returns a non-negative integer denoting the current position in the stream (usually the number of characters <em>before</em> the next one to be read.</p>
<p>If this is not possible, for example for an input stream attached to standard input (normally the keyboard), then <code class="keyw">fail</code> is returned</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ RewindStream</code>( <var class="Arg">input-stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">RewindStream</code> attempts to return an input stream to its starting condition, so that all the same characters can be read again. It returns <code class="keyw">true</code> if the rewind succeeds and <code class="keyw">fail</code> otherwise</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ SeekPositionStream</code>( <var class="Arg">input-stream</var>, <var class="Arg">pos</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="func">SeekPositionStream</code> attempts to rewind or wind forward an input stream to the specified position. This is not possible for all streams. It returns <code class="keyw">true</code> if the seek is successful and <code class="keyw">fail</code> otherwise.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ WriteByte</code>( <var class="Arg">output-stream</var>, <var class="Arg">byte</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>writes the next character (given as <em>integer</em>) to the output stream <var class="Arg">output-stream</var>. The function returns <code class="keyw">true</code> if the write succeeds and <codeclass="keyw">fail</code> otherwise.</p>
<p><code class="func">WriteByte</code> is the basic operation for output streams. If a <code class="func">WriteByte</code> method is installed for a user-defined type of stream, then all the other output stream operations will work (although possibly not at peak efficiency).</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ WriteLine</code>( <var class="Arg">output-stream</var>, <var class="Arg">string</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>appends <var class="Arg">string</var> to <var class="Arg">output-stream</var>. A final newline is written. The function returns <code class="keyw">true</code> if the write succeeds and <code class="keyw">fail</code> otherwise.</p>
<p>A default method is installed which implements <code class="func">WriteLine</code> by repeated calls to <code class="func">WriteByte</code> (<a href="chap10.html#X7D37C7A07E9C319C"><span class="RefLink">10.4-1</span></a>).</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ WriteAll</code>( <var class="Arg">output-stream</var>, <var class="Arg">string</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>appends <var class="Arg">string</var> to <var class="Arg">output-stream</var>. No final newline is written. The function returns <code class="keyw">true</code> if the write succeeds and <code class="keyw">fail</code> otherwise. It will block as long as necessary for the write operation to complete (for example for a child process to clear its input buffer )</p>
<p>A default method is installed which implements <code class="func">WriteAll</code> by repeated calls to <code class="func">WriteByte</code> (<a href="chap10.html#X7D37C7A07E9C319C"><span class="RefLink">10.4-1</span></a>).</p>
<p>When printing or appending to a stream (using <code class="func">PrintTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), or <code class="func">AppendTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>) or when logging to a stream), the kernel generates a call to <code class="func">WriteAll</code> for each line output.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ LogTo</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>causes the subsequent interaction to be logged to the output stream <var class="Arg">stream</var>. It works in precisely the same way as it does for files (see <code class="func">LogTo</code> (<a href="chap9.html#X79813A6686894960"><span class="RefLink">9.8-4</span></a>)).</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputLogTo</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>causes the subsequent input to be logged to the output stream <var class="Arg">stream</var>. It works just like it does for files (see <code class="func">InputLogTo</code> (<a href="chap9.html#X7CAB119378B075B7"><span class="RefLink">9.8-5</span></a>)).</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ OutputLogTo</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>causes the subsequent output to be logged to the output stream <var class="Arg">stream</var>. It works just like it does for files (see <code class="func">OutputLogTo</code> (<a href="chap9.html#X7A5591D87EAFA6CC"><span class="RefLink">9.8-6</span></a>)).</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ SetPrintFormattingStatus</code>( <var class="Arg">stream</var>, <var class="Arg">newstatus</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ PrintFormattingStatus</code>( <var class="Arg">stream</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>When text is being sent to an output text stream via <code class="func">PrintTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), <code class="func">AppendTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), <code class="func">LogTo</code> (<a href="chap10.html#X7BF4E44C7D51E085"><span class="RefLink">10.4-5</span></a>), etc., it is by default formatted just as it would be were it being printed to the screen. Thus, it is broken into lines of reasonable length at (where possible) sensible places, lines containing elements of lists or records are indented, and so forth. This is appropriate if the output is eventually to be viewed by a human, and harmless if it to passed as input to <strong class="pkg">GAP</strong>, but may be unhelpful if the output is to be passed as input to another program. It is possible to turn off this behaviour for a stream using the <code class="func">SetPrintFormattingStatus</code> operation, and to test whether it is on or off using <code class="func">PrintFormattingStatus</code>.</p>
<p><code class="func">SetPrintFormattingStatus</code> sets whether output sent to the output stream <var class="Arg">stream</var> via <code class="func">PrintTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), <code class="func">AppendTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), etc. will be formatted with line breaks and indentation. If the second argument <var class="Arg">newstatus</var> is <code class="keyw">true</code> then output will be so formatted, and if <code class="keyw">false</code> then it will not. If the stream is not a text stream, only <code class="keyw">false</code> is allowed.</p>
<p><code class="func">PrintFormattingStatus</code> returns <code class="keyw">true</code> if output sent to the output text stream <var class="Arg">stream</var> via <code class="func">PrintTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), <code class="func">AppendTo</code> (<a href="chap9.html#X86956C577FFEE1F9"><span class="RefLink">9.8-3</span></a>), etc. will be formatted with line breaks and indentation, and <code class="keyw">false</code> otherwise. For non-text streams, it returns <code class="keyw">false</code>. If as argument <var class="Arg">stream</var> the string <code class="code">"*stdout*"</code> is given, these functions refer to the formatting status of the standard output (so usually the user's terminal screen).
<p>Similarly, the string <code class="code">"*errout*"</code> refers to the formatting status of the standard error output, which influences how error messages are printed.</p>
<p>These functions do not influence the behaviour of the low level functions <code class="func">WriteByte</code> (<a href="chap10.html#X7D37C7A07E9C319C"><span class="RefLink">10.4-1</span></a>), <code class="func">WriteLine</code> (<a href="chap10.html#X79FA85498596CC99"><span class="RefLink">10.4-2</span></a>) or <code class="func">WriteAll</code> (<a href="chap10.html#X78C113917936058D"><span class="RefLink">10.4-3</span></a>) which always write without formatting.</p>
<p>File streams are streams associated with files. An input file stream reads the characters it delivers from a file, an output file stream prints the characters it receives to a file. The following functions can be used to create such streams. They return <code class="keyw">fail</code> if an error occurred, in this case <code class="func">LastSystemError</code> (<a href="chap9.html#X87D278437A916905"><span class="RefLink">9.1-1</span></a>) can be used to get information about the error.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputTextFile</code>( <var class="Arg">filename</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="code">InputTextFile( <var class="Arg">filename</var> )</code> returns an input stream in the category <code class="func">IsInputTextStream</code> (<a href="chap10.html#X7C8956BB7FE2A89C"><span class="RefLink">10.1-4</span></a>) that delivers the characters from the file <var class="Arg">filename</var>. If <var class="Arg">filename</var> ends in <code class="code">.gz</code> and the file is a valid gzipped file, then the file will be transparently uncompressed.</p>
<p><code class="code">InputTextFile</code> is designed for use with text files and automatically handles windows-style line endings. This means it should <em>not</em> be used for binary data. The <code class="func">IO_File</code> (<a href="../../pkg/io/doc/chap4.html#X7EC160D07E815DBB"><span class="RefLink">IO: IO_File mode</span></a>) function from the <strong class="pkg">IO</strong> package should be used to access binary data.</p>
<p>Note: At most 256 files may be open for reading or writing at the same time. Use <code class="func">CloseStream</code> (<a href="chap10.html#X786E5520803FDE00"><span class="RefLink">10.2-1</span></a>) to close the input stream once you have finished reading from it.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ OutputTextFile</code>( <var class="Arg">filename</var>, <var class="Arg">append</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ OutputGzipFile</code>( <var class="Arg">filename</var>, <var class="Arg">append</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="code">OutputTextFile( <var class="Arg">filename</var>, <var class="Arg">append</var> )</code> returns an output stream in the category <code class="code">IsOutputTextFile</code> that writes received characters to the file <var class="Arg">filename</var>. If <var class="Arg">append</var> is <code class="keyw">false</code>, then the file is emptied first, otherwise received characters are added at the end of the file. <code class="code">OutputGzipFile</code> acts identically to <code class="code">OutputTextFile</code>, except it compresses the output with gzip.</p>
<p>Note: At most 256 files may be open for reading or writing at the same time. Use <code class="func">CloseStream</code> (<a href="chap10.html#X786E5520803FDE00"><span class="RefLink">10.2-1</span></a>) to close the output stream once you have finished writing to it.</p>
<div class="example"><pre>
<span class="GAPprompt">gap></span> <span class="GAPinput"># use a temporary directory</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">name := Filename( DirectoryTemporary(), "test" );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"># create an output stream, append output, and close again</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">output := OutputTextFile( name, true );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">AppendTo( output, "Hallo\n", "You\n" );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">CloseStream(output);</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"># create an input, print complete contents of file, and close</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">input := InputTextFile(name);;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Print( ReadAll(input) );</span>
Hallo
You
<span class="GAPprompt">gap></span> <span class="GAPinput">CloseStream(input);</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"># append a single line</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">output := OutputTextFile( name, true );;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">AppendTo( output, "AppendLine\n" );</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"># close output stream to flush the output</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">CloseStream(output);</span>
<span class="GAPprompt">gap></span> <span class="GAPinput"># create an input, print complete contents of file, and close</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">input := InputTextFile(name);;</span>
<span class="GAPprompt">gap></span> <span class="GAPinput">Print( ReadAll(input) );</span>
Hallo
You
AppendLine
<span class="GAPprompt">gap></span> <span class="GAPinput">CloseStream(input);</span>
</pre></div>
<p>The commands described in this section create streams which accept characters from, or deliver characters to, the user, via the keyboard or the <strong class="pkg">GAP</strong> session display.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputTextUser</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns an input text stream which delivers characters typed by the user (or from the standard input device if it has been redirected). In normal circumstances, characters are delivered one by one as they are typed, without waiting until the end of a line. No prompts are printed.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ OutputTextUser</code>( )</td><td class="tdright">( function )</td></tr></table></div>
<p>returns an output stream which delivers characters to the user's display (or the standard output device if it has been redirected). Each character is delivered immediately it is written, without waiting for a full line of output. Text written in this way is not written to the session log (see LogTo (9.8-4)).
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputFromUser</code>( <var class="Arg">arg</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>prints the <var class="Arg">arg</var> as a prompt, then waits until a text is typed by the user (or from the standard input device if it has been redirected). This text must be a <em>single</em> expression, followed by one <em>enter</em>. This is evaluated (see <code class="func">EvalString</code> (<a href="chap27.html#X7DE4CCD285440659"><span class="RefLink">27.9-5</span></a>)) and the result is returned.</p>
<p>String streams are streams associated with strings. An input string stream reads the characters it delivers from a string, an output string stream appends the characters it receives to a string. The following functions can be used to create such streams.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputTextString</code>( <var class="Arg">string</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p><code class="code">InputTextString( <var class="Arg">string</var> )</code> returns an input stream that delivers the characters from the string <var class="Arg">string</var>. The <var class="Arg">string</var> is not changed when reading characters from it and changing the <var class="Arg">string</var> after the call to <code class="func">InputTextString</code> has no influence on the input stream.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ OutputTextString</code>( <var class="Arg">list</var>, <var class="Arg">append</var> )</td><td class="tdright">( operation )</td></tr></table></div>
<p>returns an output stream that puts all received characters into the list <var class="Arg">list</var>. If <var class="Arg">append</var> is <code class="keyw">false</code>, then the list is emptied first, otherwise received characters are added at the end of the list.</p>
<p>Input-output streams capture bidirectional communications between <strong class="pkg">GAP</strong> and another process, either locally or (@as yet unimplemented@) remotely.</p>
<p>Such streams support the basic operations of both input and output streams. They should provide some buffering, allowing output data to be written to the stream, even when input data is waiting to be read, but the amount of this buffering is operating system dependent, and the user should take care not to get too far ahead in writing, or behind in reading, or deadlock may occur.</p>
<p>At present the only type of Input-Output streams that are implemented provide communication with a local child process, using a pseudo-tty.</p>
<p>Like other streams, write operations are blocking, read operations will block to get the first character, but not thereafter.</p>
<p>As far as possible, no translation is done on characters written to, or read from the stream, and no control characters have special effects, but the details of particular pseudo-tty implementations may effect this.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ IsInputOutputStream</code>( <var class="Arg">obj</var> )</td><td class="tdright">( category )</td></tr></table></div>
<p><code class="func">IsInputOutputStream</code> is the Category of Input-Output Streams; it returns <code class="keyw">true</code> if the <var class="Arg">obj</var> is an input-output stream and <code class="keyw">false</code> otherwise.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ InputOutputLocalProcess</code>( <var class="Arg">dir</var>, <var class="Arg">executable</var>, <varclass="Arg">args</var> )</td><td class="tdright">( function )</td></tr></table></div>
<p>starts up a child process, whose executable file is <var class="Arg">executable</var>, with <q>command line</q> arguments <var class="Arg">args</var> in the directory <var class="Arg">dir</var>. (Suitable choices for <var class="Arg">dir</var> are <code class="code">DirectoryCurrent()</code> or <code class="code">DirectoryTemporary()</code> (see Section <a href="chap9.html#X85030B35865A1080"><span class="RefLink">9.4</span></a>); <code class="code">DirectoryTemporary()</code> may be a good choice when <var class="Arg">executable</var> generates output files that it doesn't itself remove afterwards.) InputOutputLocalProcess returns an InputOutputStream object. Bytes written to this stream are received by the child process as if typed at a terminal on standard input. Bytes written to standard output by the child process can be read from the stream.
<p>When the stream is closed, the signal SIGTERM is delivered to the child process, which is expected to exit.</p>
<div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">‣ ReadAllLine</code>( <var class="Arg">iostream</var>[, <var class="Arg">nofail</var>][, <var class="Arg">IsAllLine</var>] )</td><td class="tdright">( operation )</td></tr></table></div>
<p>For an input/output stream <var class="Arg">iostream</var> <code class="func">ReadAllLine</code> reads until a newline character if any input is found or returns <code class="keyw">fail</code> if no input is found, i.e. if any input is found <code class="func">ReadAllLine</code> is non-blocking.</p>
<p>If the argument <var class="Arg">nofail</var> (which must be <code class="keyw">false</code> or <code class="keyw">true</code>) is provided and it is set to <code class="keyw">true</code> then <code class="func">ReadAllLine</code> will wait, if necessary, for input and never return <code class="keyw">fail</code>.</p>
<p>If the argument <var class="Arg">IsAllLine</var> (which must be a function that takes a string argument and returns either <code class="keyw">true</code> or <code class="keyw">false</code>) then it is used to determine what constitutes a whole line. The default behaviour is equivalent to passing the function</p>
<div class="example"><pre>
line -> 0 < Length(line) and Last(line) = '\n'
</pre></div>
<p>for the <var class="Arg">IsAllLine</var> argument. The purpose of the <var class="Arg">IsAllLine</var> argument is to cater for the case where the input being read is from an external process that writes a <q>prompt</q> for data that does not terminate with a newline.</p>
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.