Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap10.txt
Views: 415153
1
2
10 Streams
3
4
Streams provide flexible access to GAP's input and output processing. An
5
input stream takes characters from some source and delivers them to GAP
6
which reads them from the stream. When an input stream has delivered all
7
characters it is at end-of-stream. An output stream receives characters from
8
GAP which writes them to the stream, and delivers them to some destination.
9
10
A major use of streams is to provide efficient and flexible access to files.
11
Files can be read and written using Read (9.7-1) and AppendTo (9.7-3),
12
however the former only allows a complete file to be read as GAP input and
13
the latter imposes a high time penalty if many small pieces of output are
14
written to a large file. Streams allow input files in other formats to be
15
read and processed, and files to be built up efficiently from small pieces
16
of output. Streams may also be used for other purposes, for example to read
17
from and print to GAP strings, or to read input directly from the user.
18
19
Any stream is either a text stream, which translates the end-of-line
20
character (\n) to or from the system's representation of end-of-line (e.g.,
21
new-line under UNIX and carriage-return-new-line under DOS), or a binary
22
stream, which does not translate the end-of-line character. The processing
23
of other unprintable characters by text streams is undefined. Binary streams
24
pass them unchanged.
25
26
Whereas it is cheap to append to a stream, streams do consume system
27
resources, and only a limited number can be open at any time, therefore it
28
is necessary to close a stream as soon as possible using CloseStream
29
(10.2-1). If creating a stream failed then LastSystemError (9.1-1) can be
30
used to get information about the failure.
31
32
33
10.1 Categories for Streams and the StreamsFamily
34
35
10.1-1 IsStream
36
37
IsStream( obj )  Category
38
39
Streams are GAP objects and all open streams, input, output, text and
40
binary, lie in this category.
41
42
10.1-2 IsClosedStream
43
44
IsClosedStream( obj )  Category
45
46
When a stream is closed, its type changes to lie in IsClosedStream. This
47
category is used to install methods that trap accesses to closed streams.
48
49
10.1-3 IsInputStream
50
51
IsInputStream( obj )  Category
52
53
All input streams lie in this category, and support input operations such as
54
ReadByte (10.3-3) (see 10.3)
55
56
10.1-4 IsInputTextStream
57
58
IsInputTextStream( obj )  Category
59
60
All text input streams lie in this category. They translate new-line
61
characters read.
62
63
10.1-5 IsInputTextNone
64
65
IsInputTextNone( obj )  Category
66
67
It is convenient to use a category to distinguish dummy streams (see 10.9)
68
from others. Other distinctions are usually made using representations
69
70
10.1-6 IsOutputStream
71
72
IsOutputStream( obj )  Category
73
74
All output streams lie in this category and support basic operations such as
75
WriteByte (10.4-1) (see Section 10.4).
76
77
10.1-7 IsOutputTextStream
78
79
IsOutputTextStream( obj )  Category
80
81
All text output streams lie in this category and translate new-line
82
characters on output.
83
84
10.1-8 IsOutputTextNone
85
86
IsOutputTextNone( obj )  Category
87
88
It is convenient to use a category to distinguish dummy streams (see 10.9)
89
from others. Other distinctions are usually made using representations
90
91
10.1-9 StreamsFamily
92
93
StreamsFamily family
94
95
All streams lie in the StreamsFamily.
96
97
98
10.2 Operations applicable to All Streams
99
100
10.2-1 CloseStream
101
102
CloseStream( stream )  operation
103
104
In order to preserve system resources and to flush output streams every
105
stream should be closed as soon as it is no longer used using CloseStream.
106
107
It is an error to try to read characters from or write characters to a
108
closed stream. Closing a stream tells the GAP kernel and/or the operating
109
system kernel that the file is no longer needed. This may be necessary
110
because the GAP kernel and/or the operating system may impose a limit on how
111
many streams may be open simultaneously.
112
113
10.2-2 FileDescriptorOfStream
114
115
FileDescriptorOfStream( stream )  operation
116
117
returns the UNIX file descriptor of the underlying file. This is mainly
118
useful for the UNIXSelect (10.2-3) function call. This is as of now only
119
available on UNIX-like operating systems and only for streams to local
120
processes and local files.
121
122
10.2-3 UNIXSelect
123
124
UNIXSelect( inlist, outlist, exclist, timeoutsec, timeoutusec )  function
125
126
makes the UNIX C-library function select accessible from GAP for streams.
127
The functionality is as described in the man page (see UNIX file descriptors
128
(integers) for streams. They can be obtained via FileDescriptorOfStream
129
(10.2-2) for streams to local processes and to local files. The argument
130
timeoutsec is a timeout in seconds as in the struct timeval on the C level.
131
The argument timeoutusec is analogously in microseconds. The total timeout
132
is the sum of both. If one of those timeout arguments is not a small integer
133
then no timeout is applicable (fail is allowed for the timeout arguments).
134
135
The return value is the number of streams that are ready, this may be 0 if a
136
timeout was specified. All file descriptors in the three lists that are not
137
yet ready are replaced by fail in this function. So the lists are changed!
138
139
This function is not available on the Macintosh architecture and is only
140
available if your operating system has select, which is detected during
141
compilation of GAP.
142
143
144
10.3 Operations for Input Streams
145
146
Two operations normally used to read files: Read (9.7-1) and ReadAsFunction
147
(9.7-2) can also be used to read GAP input from a stream. The input is
148
immediately parsed and executed. When reading from a stream str, the GAP
149
kernel generates calls to ReadLine(str) to supply text to the parser.
150
151
Three further operations: ReadByte (10.3-3), ReadLine (10.3-4) and ReadAll
152
(10.3-5), support reading characters from an input stream without parsing
153
them. This can be used to read data in any format and process it in GAP.
154
155
Additional operations for input streams support detection of end of stream,
156
and (for those streams for which it is appropriate) random access to the
157
data.
158
159
10.3-1 Read
160
161
Read( input-text-stream )  operation
162
163
reads the input-text-stream as input until end-of-stream occurs. See 9.7 for
164
details.
165
166
10.3-2 ReadAsFunction
167
168
ReadAsFunction( input-text-stream )  operation
169
170
reads the input-text-stream as function and returns this function. See 9.7
171
for details.
172
173
 Example 
174
gap> # a function with local `a' does not change the global one
175
gap> a := 1;;
176
gap> i := InputTextString( "local a; a := 10; return a*10;" );;
177
gap> ReadAsFunction(i)();
178
100
179
gap> a;
180
1
181
gap> # reading it via `Read' does
182
gap> i := InputTextString( "a := 10;" );;
183
gap> Read(i);
184
gap> a;
185
10
186

187
188
10.3-3 ReadByte
189
190
ReadByte( input-stream )  operation
191
192
ReadByte returns one character (returned as integer) from the input stream
193
input-stream. ReadByte returns fail if there is no character available, in
194
particular if it is at the end of a file.
195
196
If input-stream is the input stream of a input/output process, ReadByte may
197
also return fail if no byte is currently available.
198
199
ReadByte is the basic operation for input streams. If a ReadByte method is
200
installed for a user-defined type of stream which does not block, then all
201
the other input stream operations will work (although possibly not at peak
202
efficiency).
203
204
ReadByte will wait (block) until a byte is available. For instance if the
205
stream is a connection to another process, it will wait for the process to
206
output a byte.
207
208
10.3-4 ReadLine
209
210
ReadLine( input-stream )  operation
211
212
ReadLine returns one line (returned as string with the newline) from the
213
input stream input-stream. ReadLine reads in the input until a newline is
214
read or the end-of-stream is encountered.
215
216
If input-stream is the input stream of a input/output process, ReadLine may
217
also return fail or return an incomplete line if the other process has not
218
yet written any more. It will always wait (block) for at least one byte to
219
be available, but will then return as much input as is available, up to a
220
limit of one line
221
222
A default method is supplied for ReadLine which simply calls ReadByte
223
(10.3-3) repeatedly. This is only safe for streams that cannot block. The
224
kernel uses calls to ReadLine to supply input to the parser when reading
225
from a stream.
226
227
10.3-5 ReadAll
228
229
ReadAll( input-stream[, limit] )  operation
230
231
ReadAll returns all characters as string from the input stream stream-in. It
232
waits (blocks) until at least one character is available from the stream, or
233
until there is evidence that no characters will ever be available again.
234
This last indicates that the stream is at end-of-stream. Otherwise, it reads
235
as much input as it can from the stream without blocking further and returns
236
it to the user. If the stream is already at end of file, so that no bytes
237
are available, fail is returned. In the case of a file stream connected to a
238
normal file (not a pseudo-tty or named pipe or similar), all the bytes
239
should be immediately available and this function will read the remainder of
240
the file.
241
242
With a second argument, at most limit bytes will be returned. Depending on
243
the stream a bounded number of additional bytes may have been read into an
244
internal buffer.
245
246
A default method is supplied for ReadAll which simply calls ReadLine
247
(10.3-4) repeatedly. This is only really safe for streams which cannot
248
block. Other streams should install a method for ReadAll
249
250
 Example 
251
gap> i := InputTextString( "1Hallo\nYou\n1" );;
252
gap> ReadByte(i);
253
49
254
gap> CHAR_INT(last);
255
'1'
256
gap> ReadLine(i);
257
"Hallo\n"
258
gap> ReadLine(i);
259
"You\n"
260
gap> ReadLine(i);
261
"1"
262
gap> ReadLine(i);
263
fail
264
gap> ReadAll(i);
265
""
266
gap> RewindStream(i);;
267
gap> ReadAll(i);
268
"1Hallo\nYou\n1"
269

270
271
10.3-6 IsEndOfStream
272
273
IsEndOfStream( input-stream )  operation
274
275
IsEndOfStream returns true if the input stream is at end-of-stream, and
276
false otherwise. Note that IsEndOfStream might return false even if the next
277
ReadByte (10.3-3) fails.
278
279
10.3-7 PositionStream
280
281
PositionStream( input-stream )  operation
282
283
Some input streams, such as string streams and file streams attached to disk
284
files, support a form of random access by way of the operations
285
PositionStream, SeekPositionStream (10.3-9) and RewindStream (10.3-8).
286
PositionStream returns a non-negative integer denoting the current position
287
in the stream (usually the number of characters before the next one to be
288
read.
289
290
If this is not possible, for example for an input stream attached to
291
standard input (normally the keyboard), then fail is returned
292
293
10.3-8 RewindStream
294
295
RewindStream( input-stream )  operation
296
297
RewindStream attempts to return an input stream to its starting condition,
298
so that all the same characters can be read again. It returns true if the
299
rewind succeeds and fail otherwise
300
301
A default method implements RewindStream using SeekPositionStream (10.3-9).
302
303
10.3-9 SeekPositionStream
304
305
SeekPositionStream( input-stream, pos )  operation
306
307
SeekPositionStream attempts to rewind or wind forward an input stream to the
308
specified position. This is not possible for all streams. It returns true if
309
the seek is successful and fail otherwise.
310
311
312
10.4 Operations for Output Streams
313
314
10.4-1 WriteByte
315
316
WriteByte( output-stream, byte )  operation
317
318
writes the next character (given as integer) to the output stream
319
output-stream. The function returns true if the write succeeds and fail
320
otherwise.
321
322
WriteByte is the basic operation for output streams. If a WriteByte method
323
is installed for a user-defined type of stream, then all the other output
324
stream operations will work (although possibly not at peak efficiency).
325
326
10.4-2 WriteLine
327
328
WriteLine( output-stream, string )  operation
329
330
appends string to output-stream. A final newline is written. The function
331
returns true if the write succeeds and fail otherwise.
332
333
A default method is installed which implements WriteLine by repeated calls
334
to WriteByte (10.4-1).
335
336
10.4-3 WriteAll
337
338
WriteAll( output-stream, string )  operation
339
340
appends string to output-stream. No final newline is written. The function
341
returns true if the write succeeds and fail otherwise. It will block as long
342
as necessary for the write operation to complete (for example for a child
343
process to clear its input buffer )
344
345
A default method is installed which implements WriteAll by repeated calls to
346
WriteByte (10.4-1).
347
348
When printing or appending to a stream (using PrintTo (9.7-3), or AppendTo
349
(9.7-3) or when logging to a stream), the kernel generates a call to
350
WriteAll for each line output.
351
352
 Example 
353
gap> str := "";; a := OutputTextString(str,true);;
354
gap> WriteByte(a,INT_CHAR('H'));
355
true
356
gap> WriteLine(a,"allo");
357
true
358
gap> WriteAll(a,"You\n");
359
true
360
gap> CloseStream(a);
361
gap> Print(str);
362
Hallo
363
You
364

365
366
367
10.4-4 PrintTo and AppendTo (for streams)
368
369
PrintTo( output-stream, arg1, ... )  function
370
AppendTo( output-stream, arg1, ... )  function
371
372
These functions work like Print (6.3-4), except that the output is appended
373
to the output stream output-stream.
374
375
 Example 
376
gap> str := "";; a := OutputTextString(str,true);;
377
gap> AppendTo( a, (1,2,3), ":", Z(3) );
378
gap> CloseStream(a);
379
gap> Print( str, "\n" );
380
(1,2,3):Z(3)
381

382
383
10.4-5 LogTo
384
385
LogTo( stream )  operation
386
387
causes the subsequent interaction to be logged to the output stream stream.
388
It works in precisely the same way as it does for files (see LogTo (9.7-4)).
389
390
10.4-6 InputLogTo
391
392
InputLogTo( stream )  operation
393
394
causes the subsequent input to be logged to the output stream stream. It
395
works just like it does for files (see InputLogTo (9.7-5)).
396
397
10.4-7 OutputLogTo
398
399
OutputLogTo( stream )  operation
400
401
causes the subsequent output to be logged to the output stream stream. It
402
works just like it does for files (see OutputLogTo (9.7-6)).
403
404
10.4-8 SetPrintFormattingStatus
405
406
SetPrintFormattingStatus( stream, newstatus )  operation
407
PrintFormattingStatus( stream )  operation
408
409
When text is being sent to an output text stream via PrintTo (9.7-3),
410
AppendTo (9.7-3), LogTo (10.4-5), etc., it is by default formatted just as
411
it would be were it being printed to the screen. Thus, it is broken into
412
lines of reasonable length at (where possible) sensible places, lines
413
containing elements of lists or records are indented, and so forth. This is
414
appropriate if the output is eventually to be viewed by a human, and
415
harmless if it to passed as input to GAP, but may be unhelpful if the output
416
is to be passed as input to another program. It is possible to turn off this
417
behaviour for a stream using the SetPrintFormattingStatus operation, and to
418
test whether it is on or off using PrintFormattingStatus.
419
420
SetPrintFormattingStatus sets whether output sent to the output stream
421
stream via PrintTo (9.7-3), AppendTo (9.7-3), etc. will be formatted with
422
line breaks and indentation. If the second argument newstatus is true then
423
output will be so formatted, and if false then it will not. If the stream is
424
not a text stream, only false is allowed.
425
426
PrintFormattingStatus returns true if output sent to the output text stream
427
stream via PrintTo (9.7-3), AppendTo (9.7-3), etc. will be formatted with
428
line breaks and indentation, and false otherwise. For non-text streams, it
429
returns false. If as argument stream the string "*stdout*" is given, these
430
functions refer to the formatting status of the standard output (so usually
431
the users terminal screen).
432
433
These functions do not influence the behaviour of the low level functions
434
WriteByte (10.4-1), WriteLine (10.4-2) or WriteAll (10.4-3) which always
435
write without formatting.
436
437
 Example 
438
gap> s := "";; str := OutputTextString(s,false);;
439
gap> PrintTo(str,Primes{[1..30]});
440
gap> s;
441
"[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,\
442
 \n 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ]"
443
gap> Print(s,"\n");
444
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 
445
 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ]
446
gap> SetPrintFormattingStatus(str, false);
447
gap> PrintTo(str,Primes{[1..30]});
448
gap> s;
449
"[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,\
450
 \n 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ][ 2, 3, 5, 7\
451
, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, \
452
79, 83, 89, 97, 101, 103, 107, 109, 113 ]"
453
gap> Print(s,"\n");
454
[ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 
455
 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113 ][ 2, 3, 5, 7, 1\
456
1, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,\
457
 83, 89, 97, 101, 103, 107, 109, 113 ]
458

459
460
461
10.5 File Streams
462
463
File streams are streams associated with files. An input file stream reads
464
the characters it delivers from a file, an output file stream prints the
465
characters it receives to a file. The following functions can be used to
466
create such streams. They return fail if an error occurred, in this case
467
LastSystemError (9.1-1) can be used to get information about the error.
468
469
10.5-1 InputTextFile
470
471
InputTextFile( filename )  operation
472
473
InputTextFile( filename ) returns an input stream in the category
474
IsInputTextStream (10.1-4) that delivers the characters from the file
475
filename.
476
477
10.5-2 OutputTextFile
478
479
OutputTextFile( filename, append )  operation
480
481
OutputTextFile( filename, append ) returns an output stream in the category
482
IsOutputTextFile that writes received characters to the file filename. If
483
append is false, then the file is emptied first, otherwise received
484
characters are added at the end of the file.
485
486
 Example 
487
gap> # use a temporary directory
488
gap> name := Filename( DirectoryTemporary(), "test" );;
489
gap> # create an output stream, append output, and close again
490
gap> output := OutputTextFile( name, true );;
491
gap> AppendTo( output, "Hallo\n", "You\n" );
492
gap> CloseStream(output);
493
gap> # create an input, print complete contents of file, and close
494
gap> input := InputTextFile(name);;
495
gap> Print( ReadAll(input) );
496
Hallo
497
You
498
gap> CloseStream(input);
499
gap> # append a single line
500
gap> output := OutputTextFile( name, true );;
501
gap> AppendTo( output, "AppendLine\n" );
502
gap> # close output stream to flush the output
503
gap> CloseStream(output);
504
gap> # create an input, print complete contents of file, and close
505
gap> input := InputTextFile(name);;
506
gap> Print( ReadAll(input) );
507
Hallo
508
You
509
AppendLine
510
gap> CloseStream(input);
511

512
513
514
10.6 User Streams
515
516
The commands described in this section create streams which accept
517
characters from, or deliver characters to, the user, via the keyboard or the
518
GAP session display.
519
520
10.6-1 InputTextUser
521
522
InputTextUser( )  function
523
524
returns an input text stream which delivers characters typed by the user (or
525
from the standard input device if it has been redirected). In normal
526
circumstances, characters are delivered one by one as they are typed,
527
without waiting until the end of a line. No prompts are printed.
528
529
10.6-2 OutputTextUser
530
531
OutputTextUser( )  function
532
533
returns an output stream which delivers characters to the user's display (or
534
the standard output device if it has been redirected). Each character is
535
delivered immediately it is written, without waiting for a full line of
536
output. Text written in this way is not written to the session log (see
537
LogTo (9.7-4)).
538
539
10.6-3 InputFromUser
540
541
InputFromUser( arg )  function
542
543
prints the arg as a prompt, then waits until a text is typed by the user (or
544
from the standard input device if it has been redirected). This text must be
545
a single expression, followed by one enter. This is evaluated (see
546
EvalString (27.9-3)) and the result is returned.
547
548
549
10.7 String Streams
550
551
String streams are streams associated with strings. An input string stream
552
reads the characters it delivers from a string, an output string stream
553
appends the characters it receives to a string. The following functions can
554
be used to create such streams.
555
556
10.7-1 InputTextString
557
558
InputTextString( string )  operation
559
560
InputTextString( string ) returns an input stream that delivers the
561
characters from the string string. The string is not changed when reading
562
characters from it and changing the string after the call to InputTextString
563
has no influence on the input stream.
564
565
10.7-2 OutputTextString
566
567
OutputTextString( list, append )  operation
568
569
returns an output stream that puts all received characters into the list
570
list. If append is false, then the list is emptied first, otherwise received
571
characters are added at the end of the list.
572
573
 Example 
574
gap> # read input from a string
575
gap> input := InputTextString( "Hallo\nYou\n" );;
576
gap> ReadLine(input);
577
"Hallo\n"
578
gap> ReadLine(input);
579
"You\n"
580
gap> # print to a string
581
gap> str := "";;
582
gap> out := OutputTextString( str, true );;
583
gap> PrintTo( out, 1, "\n", (1,2,3,4)(5,6), "\n" );
584
gap> CloseStream(out);
585
gap> Print( str );
586
1
587
(1,2,3,4)(5,6)
588

589
590
591
10.8 Input-Output Streams
592
593
Input-output streams capture bidirectional communications between GAP and
594
another process, either locally or (@as yet unimplemented@) remotely.
595
596
Such streams support the basic operations of both input and output streams.
597
They should provide some buffering, allowing output data to be written to
598
the stream, even when input data is waiting to be read, but the amount of
599
this buffering is operating system dependent, and the user should take care
600
not to get too far ahead in writing, or behind in reading, or deadlock may
601
occur.
602
603
At present the only type of Input-Output streams that are implemented
604
provide communication with a local child process, using a pseudo-tty.
605
606
Like other streams, write operations are blocking, read operations will
607
block to get the first character, but not thereafter.
608
609
As far as possible, no translation is done on characters written to, or read
610
from the stream, and no control characters have special effects, but the
611
details of particular pseudo-tty implementations may effect this.
612
613
10.8-1 IsInputOutputStream
614
615
IsInputOutputStream( obj )  Category
616
617
IsInputOutputStream is the Category of Input-Output Streams; it returns true
618
if the obj is an input-output stream and false otherwise.
619
620
10.8-2 InputOutputLocalProcess
621
622
InputOutputLocalProcess( dir, executable, args )  function
623
624
starts up a slave process, whose executable file is executable, with command
625
line arguments args in the directory dir. (Suitable choices for dir are
626
DirectoryCurrent() or DirectoryTemporary() (see Section 9.3);
627
DirectoryTemporary() may be a good choice when executable generates output
628
files that it doesn't itself remove afterwards.) InputOutputLocalProcess
629
returns an InputOutputStream object. Bytes written to this stream are
630
received by the slave process as if typed at a terminal on standard input.
631
Bytes written to standard output by the slave process can be read from the
632
stream.
633
634
When the stream is closed, the signal SIGTERM is delivered to the child
635
process, which is expected to exit.
636
637
 Example 
638
gap> d := DirectoryCurrent();
639
dir("./")
640
gap> f := Filename(DirectoriesSystemPrograms(), "rev");
641
"/usr/bin/rev"
642
gap> s := InputOutputLocalProcess(d,f,[]);
643
< input/output stream to rev >
644
gap> WriteLine(s,"The cat sat on the mat");
645
true
646
gap> Print(ReadLine(s));
647
tam eht no tas tac ehT
648
gap> x := ListWithIdenticalEntries(10000,'x');;
649
gap> ConvertToStringRep(x);
650
gap> WriteLine(s,x);
651
true
652
gap> WriteByte(s,INT_CHAR('\n'));
653
true
654
gap> y := ReadAll(s);;
655
gap> Length(y);
656
4095
657
gap> CloseStream(s);
658
gap> s;
659
< closed input/output stream to rev >
660

661
662
10.8-3 ReadAllLine
663
664
ReadAllLine( iostream[, nofail][, IsAllLine] )  operation
665
666
For an input/output stream iostream ReadAllLine reads until a newline
667
character if any input is found or returns fail if no input is found,
668
i.e. if any input is found ReadAllLine is non-blocking.
669
670
If the argument nofail (which must be false or true) is provided and it is
671
set to true then ReadAllLine will wait, if necessary, for input and never
672
return fail.
673
674
If the argument IsAllLine (which must be a function that takes a string
675
argument and returns either true or false) then it is used to determine what
676
constitutes a whole line. The default behaviour is equivalent to passing the
677
function
678
679
 Example 
680
line -> 0 < Length(line) and line[Length(line)] = '\n'
681

682
683
for the IsAllLine argument. The purpose of the IsAllLine argument is to
684
cater for the case where the input being read is from an external process
685
that writes a prompt for data that does not terminate with a newline.
686
687
If the first argument is an input stream but not an input/output stream then
688
ReadAllLine behaves as if ReadLine (10.3-4) was called with just the first
689
argument and any additional arguments are ignored.
690
691
692
10.9 Dummy Streams
693
694
The following two commands create dummy streams which will consume all
695
characters and never deliver one.
696
697
10.9-1 InputTextNone
698
699
InputTextNone( )  function
700
701
returns a dummy input text stream, which delivers no characters, i.e., it is
702
always at end of stream. Its main use is for calls to Process (11.1-1) when
703
the started program does not read anything.
704
705
10.9-2 OutputTextNone
706
707
OutputTextNone( )  function
708
709
returns a dummy output stream, which discards all received characters. Its
710
main use is for calls to Process (11.1-1) when the started program does not
711
write anything.
712
713
714
10.10 Handling of Streams in the Background
715
716
This section describes a feature of the GAP kernel that can be used to
717
handle pending streams somehow in the background. This is currently not
718
available on the Macintosh architecture and only on operating systems that
719
have select.
720
721
Right before GAP reads a keypress from the keyboard it calls a little
722
subroutine that can handle streams that are ready to be read or ready to be
723
written. This means that GAP can handle these streams during user input on
724
the command line. Note that this does not work when GAP is in the middle of
725
some calculation.
726
727
This feature is used in the following way. One can install handler functions
728
for reading or writing streams via InstallCharReadHookFunc (10.10-1).
729
Handlers can be removed via UnInstallCharReadHookFunc (10.10-2)
730
731
Note that handler functions must not return anything and get one integer
732
argument, which refers to an index in one of the following arrays (according
733
to whether the function was installed for input, output or exceptions on the
734
stream). Handler functions usually should not output anything on the
735
standard output because this ruins the command line during command line
736
editing.
737
738
10.10-1 InstallCharReadHookFunc
739
740
InstallCharReadHookFunc( stream, mode, func )  function
741
742
installs the function func as a handler function for the stream stream. The
743
argument mode decides, for what operations on the stream this function is
744
installed. mode must be a string, in which a letter r means read, w means
745
write and x means exception, according to the select function call in the
746
UNIX C-library (see man select and UNIXSelect (10.2-3)). More than one
747
letter is allowed in mode. As described above the function is called in a
748
situation when GAP is reading a character from the keyboard. Handler
749
functions should not use much time to complete.
750
751
This functionality does not work on the Macintosh architecture and only
752
works if the operating system has a select function.
753
754
10.10-2 UnInstallCharReadHookFunc
755
756
UnInstallCharReadHookFunc( stream, func )  function
757
758
uninstalls the function func as a handler function for the stream stream.
759
All instances are deinstalled, regardless of the mode of operation (read,
760
write, exception).
761
762
This functionality does not work on the Macintosh architecture and only
763
works if the operating system has a select function.
764
765
766
10.11 Comma separated files
767
768
In some situations it can be desirable to process data given in the form of
769
a spreadsheet (such as Excel). GAP can do this using the CSV (comma
770
separated values) format, which spreadsheet programs can usually read in or
771
write out.
772
773
The first line of the spreadsheet is used as labels of record components,
774
each subsequent line then corresponds to a record. Entries enclosed in
775
double quotes are considered as strings and are permitted to contain the
776
separation character (usually a comma).
777
778
10.11-1 ReadCSV
779
780
ReadCSV( filename[, nohead][, separator] )  function
781
782
This function reads in a spreadsheet, saved in CSV format (comma separated
783
values) and returns its entries as a list of records. The entries of the
784
first line of the spreadsheet are used to denote the names of the record
785
components. Blanks will be translated into underscore characters. If the
786
parameter nohead is given as true, instead the record components will be
787
called fieldn. Each subsequent line will create one record. If given,
788
separator is the character used to separate fields. Otherwise it defaults to
789
a comma.
790
791
10.11-2 PrintCSV
792
793
PrintCSV( filename, list[, fields] )  function
794
795
This function prints a list of records as a spreadsheet in CSV format (which
796
can be read in for example into Excel). The names of the record components
797
will be printed as entries in the first line. If the argument fields is
798
given only the record fields listed in this list will be printed and they
799
will be printed in the same arrangement as given in this list. If the option
800
noheader is set to true the line with the record field names will not be
801
printed.
802
803
804