1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%W streams.msk GAP documentation Frank Celler
%W Martin Schoenert
%W & Steve Linton
%%
%H @(#)$Id: streams.msk,v 1.16.2.1 2007/09/14 13:37:45 gapchron Exp $
%%
%Y Copyright 1997, Lehrstuhl D fuer Mathematik, RWTH Aachen, Germany
%Y Copyright 2000, St Andrews
%%
%% This file contains the description of streams.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Chapter{Streams}
\FileHeader{streams}[1]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Categories for Streams and the StreamsFamily}
\Declaration{IsStream}
\Declaration{IsClosedStream}
\Declaration{IsInputStream}
\Declaration{IsInputTextStream}
\Declaration{IsInputTextNone}
\Declaration{IsOutputStream}
\Declaration{IsOutputTextStream}
\Declaration{IsOutputTextNone}
\Declaration{StreamsFamily}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Operations applicable to All Streams}
\Declaration{CloseStream}
\Declaration{FileDescriptorOfStream}
\>UNIXSelect( <inlist>, <outlist>, <exclist>, <timeoutsec>, <timeoutusec> ) F
makes the UNIX C-library function `select' accessible from {\GAP}
for streams. The functionality is as described in the man page (see
`man select'). The first three arguments must be lists containing
UNIX file descriptors (integers) for streams. They can be obtained via
`FileDescriptorOfStream' (see~"FileDescriptorOfStream") for streams
to local processes and to local files. The argument <timeoutsec> is a
timeout in seconds as in the `struct timeval' on the C level. The argument
<timeoutusec> 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 (`fail' is allowed for the timeout arguments).
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 `fail' in this function. So
the lists are changed!
This function is not available on the Macintosh architecture and is
only available if your operating system has `select', which is detected
during compilation of {\GAP}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Operations for Input Streams}
Three operations normally used to read files: `Read', `ReadAsFunction'
and `ReadTest' can also be used to read {\GAP} input from a
stream. The input is immediately parsed and executed. When reading
from a stream <str>, the {\GAP} kernel generates calls to
`ReadLine(<str>)' to supply text to the parser.
Three further operations: `ReadByte', `ReadLine' and `ReadAll', support
reading characters from an input stream without parsing them. This can be
used to read data in any format and process it in {\GAP}.
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.
\>Read( <input-text-stream> )!{for streams} O
reads the input-text-stream as input until `end-of-stream' occurs. See
"File Operations" for details.
\>ReadAsFunction( <input-text-stream> )!{for streams} O
reads the input-text-stream as function and returns this function. See
"File Operations" for details.
\>ReadTest( <input-text-stream> )!{for streams} O
reads the input-text-stream as test input until `end-of-stream' occurs.
See "File Operations" for details.
*Example*
\beginexample
gap> # a function with local `a' does not change the global one
gap> a := 1;;
gap> i := InputTextString( "local a; a := 10; return a*10;" );;
gap> ReadAsFunction(i)();
100
gap> a;
1
\endexample
\beginexample
gap> # reading it via `Read' does
gap> i := InputTextString( "a := 10;" );;
gap> Read(i);
gap> a;
10
\endexample
\Declaration{ReadByte}
\Declaration{ReadLine}
\Declaration{ReadAll}
*Example*
\beginexample
gap> i := InputTextString( "1Hallo\nYou\n1" );;
gap> ReadByte(i);
49
gap> CHAR_INT(last);
'1'
gap> ReadLine(i);
"Hallo\n"
gap> ReadLine(i);
"You\n"
gap> ReadLine(i);
"1"
gap> ReadLine(i);
fail
gap> ReadAll(i);
""
gap> RewindStream(i);;
gap> ReadAll(i);
"1Hallo\nYou\n1"
\endexample
\Declaration{IsEndOfStream}
\Declaration{PositionStream}
\Declaration{RewindStream}
\Declaration{SeekPositionStream}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Operations for Output Streams}
\Declaration{WriteByte}
\Declaration{WriteLine}
\Declaration{WriteAll}
*Example*
\beginexample
gap> str := "";; a := OutputTextString(str,true);;
gap> WriteByte(a,INT_CHAR('H'));
true
gap> WriteLine(a,"allo");
true
gap> WriteAll(a,"You\n");
true
gap> CloseStream(a);
gap> Print(str);
Hallo
You
\endexample
\>PrintTo( <output-stream>, <arg1>, ... )!{for streams} F
\>AppendTo( <output-stream>, <arg1>, ... )!{for streams} F
These functions work like `Print', except that the output is
appended to the output stream <output-stream>.
*Example*
\beginexample
gap> str := "";; a := OutputTextString(str,true);;
gap> AppendTo( a, (1,2,3), ":", Z(3) );
gap> CloseStream(a);
gap> Print( str, "\n" );
(1,2,3):Z(3)
\endexample
\Declaration{LogTo}!{for streams}
\Declaration{InputLogTo}!{for streams}
\Declaration{OutputLogTo}!{for streams}
When text is being sent to an output text stream via `PrintTo', `AppendTo',
`LogTo', 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 {\GAP}, 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
`SetPrintFormattingStatus' operation, and to test whether it is on or off using
`PrintFormattingStatus'.
\Declaration{SetPrintFormattingStatus}
\Declaration{PrintFormattingStatus}
*Example*
\beginexample
gap> s := "";; str := OutputTextString(s,false);;
gap> PrintTo(str,Primes{[1..30]});
gap> s;
"[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,\
\n 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ]"
gap> Print(s,"\n");
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ]
gap> SetPrintFormattingStatus(str, false);
gap> PrintTo(str,Primes{[1..30]});
gap> s;
"[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,\
\n 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ][ 2, 3, 5, 7, 11, 13, 17, 19\
, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103\
, 107, 109, 113 ]"
gap> Print(s,"\n");
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ][ 2, 3, 5, 7, 11, 13, 17, 19, 2\
3, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 1\
07, 109, 113 ]
\endexample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{File Streams}
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 `fail' if an error occurred,
in this case `LastSystemError' (see "LastSystemError") can be used to get
information about the error.
\Declaration{InputTextFile}
\Declaration{OutputTextFile}
*Example*
\beginexample
gap> # use a temporary directory
gap> name := Filename( DirectoryTemporary(), "test" );;
gap> # create an output stream, append output, and close again
gap> output := OutputTextFile( name, true );;
gap> AppendTo( output, "Hallo\n", "You\n" );
gap> CloseStream(output);
gap> # create an input, print complete contents of file, and close
gap> input := InputTextFile(name);;
gap> Print( ReadAll(input) );
Hallo
You
gap> CloseStream(input);
gap> # append a single line
gap> output := OutputTextFile( name, true );;
gap> AppendTo( output, "AppendLine\n" );
gap> # close output stream to flush the output
gap> CloseStream(output);
gap> # create an input, print complete contents of file, and close
gap> input := InputTextFile(name);;
gap> Print( ReadAll(input) );
Hallo
You
AppendLine
gap> CloseStream(input);
\endexample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{User Streams}
The following two commands create streams which accept characters
from, or deliver characters to, the user, via the keyboard or the {\GAP} session
display.
\Declaration{InputTextUser}
\Declaration{OutputTextUser}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{String Streams}
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.
\Declaration{InputTextString}
\Declaration{OutputTextString}
*Example*
\beginexample
gap> # read input from a string
gap> input := InputTextString( "Hallo\nYou\n" );;
gap> ReadLine(input);
"Hallo\n"
gap> ReadLine(input);
"You\n"
gap> # print to a string
gap> str := "";;
gap> out := OutputTextString( str, true );;
gap> PrintTo( out, 1, "\n", (1,2,3,4)(5,6), "\n" );
gap> CloseStream(out);
gap> Print( str );
1
(1,2,3,4)(5,6)
\endexample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Input-Output Streams}
\FileHeader{streams}[2]
\Declaration{IsInputOutputStream}
\FileHeader{streams}[3]
\Declaration{InputOutputLocalProcess}
\beginexample
gap> d := DirectoryCurrent();
dir("./")
gap> f := Filename(DirectoriesSystemPrograms(), "rev");
"/usr/bin/rev"
gap> s := InputOutputLocalProcess(d,f,[]);
< input/output stream to rev >
gap> WriteLine(s,"The cat sat on the mat");
true
gap> Print(ReadLine(s));
tam eht no tas tac ehT
gap> x := ListWithIdenticalEntries(10000,'x');;
gap> ConvertToStringRep(x);
gap> WriteLine(s,x);
true
gap> WriteByte(s,INT_CHAR('\n'));
true
gap> y := ReadAll(s);;
gap> Length(y);
4096
gap> CloseStream(s);
gap> s;
< closed input/output stream to rev >
\endexample
\Declaration{ReadAllLine}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Dummy Streams}
The following two commands create dummy streams which will consume all
characters and never deliver one.
\Declaration{InputTextNone}
\Declaration{OutputTextNone}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Handling of Streams in the Background}
This section describes a feature of the {\GAP} kernel that can be used
to handle pending streams somehow ``in the background''. This is currently
not available on the Macintosh architecture and only on operating
systems that have `select'.
Right before {\GAP} reads a keypress from the keyboard it calls a little
subroutine that can handle streams that are ready to be read or ready to
be written. This means that {\GAP} can handle these streams during
user input on the command line. Note that this does not work when {\GAP}
is in the middle of some calculation.
This feature is used in the following way. One can install handler
functions for reading or writing streams. This is done via:
\Declaration{InstallCharReadHookFunc}
Handlers can be removed via:
\Declaration{UnInstallCharReadHookFunc}
Note that handler functions must not return anything and get one integer
argument, which refers to an index in one of the following arrays
(according to whether the function was installed for input, output or
exceptions on the stream). Handler functions usually should not output
anything on the standard output because this ruins the command line
during command line editing.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%E streams.tex . . . . . . . . . . . . . . . . . . . . . . . . ends here
|