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
|
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<!-- %% -->
<!-- %W streams.xml GAP documentation Frank Celler -->
<!-- %W Martin Schönert -->
<!-- %W & Steve Linton -->
<!-- %% -->
<!-- %% -->
<!-- %Y Copyright 1997, Lehrstuhl D für Mathematik, RWTH Aachen, Germany -->
<!-- %Y Copyright 2000, St Andrews -->
<!-- %% -->
<!-- %% This file contains the description of streams. -->
<!-- %% -->
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Chapter Label="Streams">
<Heading>Streams</Heading>
<#Include Label="[1]{streams}">
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Categories for Streams and the StreamsFamily">
<Heading>Categories for Streams and the StreamsFamily</Heading>
<#Include Label="IsStream">
<#Include Label="IsClosedStream">
<#Include Label="IsInputStream">
<#Include Label="IsInputTextStream">
<#Include Label="IsInputTextNone">
<#Include Label="IsOutputStream">
<#Include Label="IsOutputTextStream">
<#Include Label="IsOutputTextNone">
<#Include Label="StreamsFamily">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Operations applicable to All Streams">
<Heading>Operations applicable to All Streams</Heading>
<#Include Label="CloseStream">
<#Include Label="FileDescriptorOfStream">
<ManSection>
<Func Name="UNIXSelect"
Arg='inlist, outlist, exclist, timeoutsec, timeoutusec'/>
<Description>
makes the UNIX C-library function <C>select</C> accessible from &GAP;
for streams. The functionality is as described in the man page (see
UNIX file descriptors (integers) for streams. They can be obtained via
<Ref Oper="FileDescriptorOfStream"/> for streams
to local processes and to local files. The argument <A>timeoutsec</A> is a
timeout in seconds as in the <C>struct timeval</C> on the C level. The argument
<A>timeoutusec</A> 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 (<K>fail</K> is allowed for the timeout arguments).
<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 <K>fail</K> in this function. So
the lists are changed!
<P/>
This function is only available if your operating system has <C>select</C>,
which is detected during compilation of &GAP;.
</Description>
</ManSection>
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Operations for Input Streams">
<Heading>Operations for Input Streams</Heading>
Two operations normally used to read files: <Ref Oper="Read"/> and
<Ref Oper="ReadAsFunction"/> can also be used to read &GAP; input from a
stream. The input is immediately parsed and executed. When reading
from a stream <A>str</A>, the &GAP; kernel generates calls to
<C>ReadLine(<A>str</A>)</C> to supply text to the parser.
<P/>
Three further operations: <Ref Oper="ReadByte"/>, <Ref Oper="ReadLine"/>
and <Ref Oper="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;.
<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/>
<ManSection>
<Oper Name="Read" Arg='input-text-stream' Label="for streams"/>
<Description>
reads the input-text-stream as input until <C>end-of-stream</C> occurs. See
<Ref Sect="File Operations"/> for details.
</Description>
</ManSection>
<ManSection>
<Oper Name="ReadAsFunction" Arg='input-text-stream' Label="for streams"/>
<Description>
reads the input-text-stream as function and returns this function. See
<Ref Sect="File Operations"/> for details.
<Example><![CDATA[
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
gap> # reading it via `Read' does
gap> i := InputTextString( "a := 10;" );;
gap> Read(i);
gap> a;
10
]]></Example>
</Description>
</ManSection>
<#Include Label="ReadByte">
<#Include Label="ReadLine">
<#Include Label="ReadAll">
<#Include Label="IsEndOfStream">
<#Include Label="PositionStream">
<#Include Label="RewindStream">
<#Include Label="SeekPositionStream">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Operations for Output Streams">
<Heading>Operations for Output Streams</Heading>
<#Include Label="WriteByte">
<#Include Label="WriteLine">
<#Include Label="WriteAll">
<ManSection>
<Heading>PrintTo and AppendTo (for streams)</Heading>
<Func Name="PrintTo" Arg='output-stream, arg1, ...' Label="for streams"/>
<Func Name="AppendTo" Arg='output-stream, arg1, ...' Label="for streams"/>
<Description>
These functions work like <Ref Func="Print"/>, except that the output is
appended to the output stream <A>output-stream</A>.
<P/>
<Example><![CDATA[
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)
]]></Example>
</Description>
</ManSection>
<#Include Label="LogTo">
<#Include Label="InputLogTo">
<#Include Label="OutputLogTo">
<#Include Label="SetPrintFormattingStatus">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="File Streams">
<Heading>File Streams</Heading>
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 <K>fail</K> if an error occurred,
in this case <Ref Func="LastSystemError"/> can be used to get
information about the error.
<#Include Label="InputTextFile">
<#Include Label="OutputTextFile">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="User Streams">
<Heading>User Streams</Heading>
The commands described in this section create streams which accept characters
from, or deliver characters to, the user, via the keyboard or the &GAP; session
display.
<#Include Label="InputTextUser">
<#Include Label="OutputTextUser">
<#Include Label="InputFromUser">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="String Streams">
<Heading>String Streams</Heading>
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.
<#Include Label="InputTextString">
<#Include Label="OutputTextString">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Input-Output Streams">
<Heading>Input-Output Streams</Heading>
<#Include Label="[2]{streams}">
<#Include Label="IsInputOutputStream">
<#Include Label="InputOutputLocalProcess">
<#Include Label="ReadAllLine">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Dummy Streams">
<Heading>Dummy Streams</Heading>
The following two commands create dummy streams which will consume all
characters and never deliver one.
<#Include Label="InputTextNone">
<#Include Label="OutputTextNone">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Handling of Streams in the Background">
<Heading>Handling of Streams in the Background</Heading>
This section describes a feature of the &GAP; kernel that can be used
to handle pending streams somehow <Q>in the background</Q>. This is
only available on operating systems that have <C>select</C>.
<P/>
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.
<P/>
This feature is used in the following way. One can install handler
functions for reading or writing streams via
<Ref Func="InstallCharReadHookFunc"/>.
Handlers can be removed via
<Ref Func="UnInstallCharReadHookFunc"/>
<P/>
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.
<#Include Label="InstallCharReadHookFunc">
<#Include Label="UnInstallCharReadHookFunc">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Comma separated files">
<Heading>Comma separated files</Heading>
<Index>Spreadsheet</Index>
<Index>Excel</Index>
In some situations it can be desirable to process data given in the form of
a spreadsheet (such as Excel). &GAP; can do this using the CSV (comma
separated values) format, which spreadsheet programs can usually read in or
write out.
<P/>
The first line of the spreadsheet is used as labels of record components,
each subsequent line then corresponds to a record. Entries enclosed in
double quotes are considered as strings and are permitted to contain the
separation character (usually a comma).
<#Include Label="ReadCSV">
<#Include Label="PrintCSV">
</Section>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<Section Label="Opening files in the Operating System">
<Heading>Opening files in the Operating System</Heading>
In some situations it can be desirable to open a file outside of &GAP;,
for example HTML files, PDFs, or pictures.
<#Include Label="OpenExternal">
</Section>
</Chapter>
<!-- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -->
<!-- %% -->
<!-- %E -->
|