]> gitweb.factorcode.org Git - factor.git/blob - core/io/io-docs.factor
factor: fix some spacing
[factor.git] / core / io / io-docs.factor
1 USING: alien byte-arrays destructors help.markup help.syntax
2 kernel math quotations sequences specialized-arrays strings ;
3 IN: io
4
5 ARTICLE: "stream-types" "Binary and text streams"
6 "A word which outputs the stream element type:"
7 { $subsections stream-element-type }
8 "Stream element types:"
9 { $subsections +byte+ +character+ }
10 "The stream element type is the data type read and written by " { $link stream-read1 } " and " { $link stream-write1 } "."
11 $nl
12 "Binary streams have an element type of " { $link +byte+ } ". Elements are integers in the range " { $snippet "[0,255]" } ", representing bytes. Reading a sequence of elements produces a " { $link byte-array } ". Any object implementing the " { $link >c-ptr } " and " { $link byte-length } " generic words can be written to a binary stream."
13 $nl
14 "Character streams have an element type of " { $link +character+ } ". Elements are non-negative integers, representing Unicode code points. Only instances of the " { $link string } " class can be read or written on a character stream."
15 $nl
16 "Most external streams are binary streams, and can be wrapped in string streams once a suitable encoding has been provided; see " { $link "io.encodings" } "." ;
17
18 HELP: +byte+
19 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
20
21 HELP: +character+
22 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
23
24 HELP: stream-element-type
25 { $values { "stream" "a stream" } { "type" { $link +byte+ } " or " { $link +character+ } } }
26 { $contract "Outputs one of " { $link +byte+ } " or " { $link +character+ } "." } ;
27
28 HELP: stream-readln
29 { $values { "stream" "an input stream" } { "str/f" { $maybe string } } }
30 { $contract "Reads a line of input from the stream. Outputs " { $link f } " on stream exhaustion." }
31 { $notes "Most code only works on one stream at a time and should instead use " { $link readln } "; see " { $link "stdio" } "." }
32 $io-error ;
33
34 HELP: stream-read1
35 { $values { "stream" "an input stream" } { "elt" { $maybe "an element" } } }
36 { $contract "Reads an element from the stream. Outputs " { $link f } " on stream exhaustion." }
37 { $notes "Most code only works on one stream at a time and should instead use " { $link read1 } "; see " { $link "stdio" } "." }
38 $io-error ;
39
40 HELP: stream-read
41 { $values { "n" "a non-negative integer" } { "stream" "an input stream" } { "seq/f" { $or byte-array string f } } }
42 { $contract "Reads " { $snippet "n" } " elements from the stream. Outputs a truncated string or " { $link f } " on stream exhaustion." }
43 { $notes "Most code only works on one stream at a time and should instead use " { $link read } "; see " { $link "stdio" } "." }
44 $io-error ;
45
46 HELP: stream-read-unsafe
47 { $values { "n" "a non-negative integer" } { "buf" { $or c-ptr string } } { "stream" "an input stream" } { "count" integer } }
48 { $contract "Reads up to " { $snippet "n" } " elements from the stream. The data is stored directly into the buffer provided by " { $snippet "buf" } ", which must be a string (in the case of a character stream) or a byte array, specialized array, or other pointer to memory (in the case of a byte stream). There must be space in the buffer for at least " { $snippet "n" } " elements. Returns the number of elements read from the stream, which will be equal to " { $snippet "n" } " unless the end of the stream is reached. If the stream is exhausted, returns zero." }
49 { $warning "This word does not perform bounds checking on " { $snippet "buf" } ". Most code should use " { $link stream-read } " or " { $link stream-read-into } " instead." }
50 $io-error ;
51
52 HELP: read-into
53 { $values { "buf" { $or byte-array specialized-array string } } { "buf-slice" slice } { "more?" boolean } }
54 { $contract "Reads from the current " { $link input-stream } " into the sequence " { $snippet "buf" } ", until either the length of " { $snippet "buf" } " is reached or the stream is exhausted. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean value that will be " { $link f } " if the stream was exhausted." }
55 $io-error ;
56
57 HELP: stream-read-into
58 { $values { "buf" { $or byte-array specialized-array string } } { "stream" "an input stream" } { "buf-slice" slice } { "more?" boolean } }
59 { $contract "Reads from the stream into the sequence " { $snippet "buf" } ", until either the length of " { $snippet "buf" } " is reached or the stream is exhausted. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean value that will be " { $link f } " if the stream was exhausted." }
60 { $notes "Most code only works on one stream at a time and should instead use " { $link read-into } "; see " { $link "stdio" } "." }
61 $io-error ;
62
63 HELP: stream-read-until
64 { $values { "seps" string } { "stream" "an input stream" } { "seq" { $or byte-array string f } } { "sep/f" { $maybe "a character" } } }
65 { $contract "Reads elements from the stream, until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator is pushed on the stack, and is not part of the output string. In the latter case, the entire stream contents are output, along with " { $link f } "." }
66 { $notes "Most code only works on one stream at a time and should instead use " { $link read-until } "; see " { $link "stdio" } "." }
67 $io-error ;
68
69 HELP: stream-read-partial
70 { $values
71     { "n" "a non-negative integer" } { "stream" "an input stream" }
72     { "seq/f" { $or byte-array string f } } }
73 { $description "Reads at most " { $snippet "n" } " elements from a stream and returns up to that many characters without blocking. If no characters are available, blocks until some are and returns them." } ;
74
75 HELP: stream-read-partial-unsafe
76 { $values { "n" "a non-negative integer" } { "buf" { $or c-ptr string } } { "stream" "an input stream" } { "count" integer } }
77 { $contract "Reads up to " { $snippet "n" } " elements from the stream without blocking. If no data is available immediately on the stream, blocks until data is available. The data is stored directly into the buffer provided by " { $snippet "buf" } ", which must be a string (in the case of a character stream), or a byte array, specialized array, or other pointer to memory (in the case of a byte stream). There must be space in the buffer for at least " { $snippet "n" } " elements. Returns the number of elements read from the stream, or zero if the end of the stream was reached." }
78 { $warning "This word does not perform bounds checking on " { $snippet "buf" } ". Most code should use " { $link stream-read-partial } " or " { $link stream-read-partial-into } " instead." }
79 $io-error ;
80
81 HELP: read-partial-into
82 { $values { "buf" { $or byte-array specialized-array string } } { "buf-slice" slice } { "more?" boolean } }
83 { $contract "Reads available data from the current " { $link input-stream } " into the sequence " { $snippet "buf" } " without blocking until all immediately available data is read or the length of " { $snippet "buf" } " is reached. If no data is immediately available, blocks until data is available. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean that will be " { $link f } " if the stream was exhausted." }
84 $io-error ;
85
86 HELP: stream-read-partial-into
87 { $values { "buf" { $or byte-array specialized-array string } } { "stream" "an input stream" } { "buf-slice" slice } { "more?" boolean } }
88 { $contract "Reads available data from the stream into the sequence " { $snippet "buf" } " without blocking until all immediately available data is read or the length of " { $snippet "buf" } " is reached. If no data is immediately available, blocks until data is available. Returns a " { $link slice } " over the part of " { $snippet "buf" } " that was written to, and a boolean that will be " { $link f } " if the stream was exhausted." }
89 { $notes "Most code only works on one stream at a time and should instead use " { $link read-partial-into } "; see " { $link "stdio" } "." }
90 $io-error ;
91
92 HELP: stream-write1
93 { $values { "elt" "an element" } { "stream" "an output stream" } }
94 { $contract "Writes an element to the stream. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
95 { $notes "Most code only works on one stream at a time and should instead use " { $link write1 } "; see " { $link "stdio" } "." }
96 $io-error ;
97
98 HELP: stream-write
99 { $values { "data" "binary data or a string" } { "stream" "an output stream" } }
100 { $contract "Writes a piece of data to the stream. If the stream performs buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
101 { $notes "Most code only works on one stream at a time and should instead use " { $link write } "; see " { $link "stdio" } "." }
102 $io-error ;
103
104 HELP: stream-flush
105 { $values { "stream" "an output stream" } }
106 { $contract "Waits for any pending output to complete." }
107 { $notes "With many output streams, written output is buffered and not sent to the underlying resource until either the buffer is full, or this word is called." }
108 { $notes "Most code only works on one stream at a time and should instead use " { $link flush } "; see " { $link "stdio" } "." }
109 $io-error ;
110
111 HELP: stream-nl
112 { $values { "stream" "an output stream" } }
113 { $contract "Writes a line terminator. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
114 { $notes "Most code only works on one stream at a time and should instead use " { $link nl } "; see " { $link "stdio" } "." }
115 $io-error ;
116
117 HELP: stream-print
118 { $values { "str" string } { "stream" "an output stream" } }
119 { $description "Writes a newline-terminated string." }
120 { $notes "Most code only works on one stream at a time and should instead use " { $link print } "; see " { $link "stdio" } "." }
121 $io-error ;
122
123 HELP: stream-copy*
124 { $values { "in" "an input stream" } { "out" "an output stream" } }
125 { $description "Copies the contents of one stream into another. The streams are left open when the copy is completed. The " { $link stream-copy } " word can be used instead to close them on completion, if desired." }
126 $io-error ;
127
128 HELP: stream-copy
129 { $values { "in" "an input stream" } { "out" "an output stream" } }
130 { $description "Copies the contents of one stream into another, closing both streams when done. To copy without closing the streams, use " { $link stream-copy* } "." }
131 $io-error ;
132
133 HELP: stream-tell
134 { $values
135     { "stream" "a stream" } { "n" integer }
136 }
137 { $description "Returns the index of the stream pointer if the stream is seekable." }
138 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
139
140 HELP: stream-seek
141 { $values
142     { "n" integer } { "seek-type" "a seek singleton" } { "stream" "a stream" }
143 }
144 { $description "Moves the pointer associated with a stream's handle to an offset " { $snippet "n" } " bytes from the seek type so that further reading or writing happens at the new location. For output streams, the buffer is flushed before seeking. Seeking past the end of an output stream will pad the difference with zeros once the stream is written to again." $nl
145     "Three methods of seeking are supported:"
146     { $list { $link seek-absolute } { $link seek-relative } { $link seek-end } }
147 }
148 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
149
150 HELP: stream-seekable?
151 { $values
152     { "stream" "a stream" } { "?" boolean }
153 }
154 { $description "Returns true if " { $snippet "stream" } " is a seekable stream." }
155 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
156
157 HELP: stream-length
158 { $values
159     { "stream" "a stream" } { "n/f" { $maybe integer } }
160 }
161 { $description "Returns the length of the data supplied by " { $snippet "stream" } ", or " { $link f } " if the stream is not seekable or has unknown length." }
162 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
163
164 HELP: seek-absolute
165 { $values
166     { "value" "a seek singleton" }
167 }
168 { $description "Seeks to an offset from the beginning of the stream." } ;
169
170 HELP: seek-end
171 { $values
172     { "value" "a seek singleton" }
173 }
174 { $description "Seeks to an offset from the end of the stream. If the offset puts the stream pointer past the end of the data on an output stream, writing to it will pad the difference with zeros." } ;
175
176 HELP: seek-relative
177 { $values
178     { "value" "a seek singleton" }
179 }
180 { $description "Seeks to an offset from the current position of the stream pointer." } ;
181
182 { stream-seek stream-tell stream-seekable? stream-length } related-words
183 { seek-absolute seek-relative seek-end } related-words
184
185 HELP: seek-input
186 { $values
187     { "n" integer } { "seek-type" "a seek singleton" }
188 }
189 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link input-stream } "." } ;
190
191 HELP: seek-output
192 { $values
193     { "n" integer } { "seek-type" "a seek singleton" }
194 }
195 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link output-stream } "." } ;
196
197 HELP: input-stream
198 { $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
199
200 HELP: output-stream
201 { $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
202
203 HELP: error-stream
204 { $var-description "Holds an error stream." } ;
205
206 HELP: readln
207 { $values { "str/f" { $maybe string } } }
208 { $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
209 $io-error ;
210
211 HELP: read1
212 { $values { "elt" { $maybe "an element" } } }
213 { $description "Reads an element from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
214 $io-error ;
215
216 HELP: read
217 { $values { "n" "a non-negative integer" } { "seq" { $or byte-array string f } } }
218 { $description "Reads " { $snippet "n" } " elements from " { $link input-stream } ". If there is no input available, outputs " { $link f } ". If there are less than " { $snippet "n" } " elements available, outputs a sequence shorter than " { $snippet "n" } " in length." }
219 $io-error ;
220
221 HELP: read-until
222 { $values { "seps" string } { "seq" { $or byte-array string f } } { "sep/f" { $maybe "a character" } } }
223 { $contract "Reads elements from " { $link input-stream } " until the first occurrence of a separator, or stream exhaustion. In the former case, the separator character is pushed on the stack, and is not part of the output. In the latter case, the entire stream contents are output, along with " { $link f } "." }
224 $io-error ;
225
226 HELP: read-partial
227 { $values { "n" integer } { "seq" { $or byte-array string f } } }
228 { $description "Reads at most " { $snippet "n" } " elements from " { $link input-stream } " and returns them in a sequence. This word should be used instead of " { $link read } " when processing the entire element a chunk at a time, since on some stream implementations it may be slightly faster." } ;
229
230 HELP: write1
231 { $values { "elt" "an element" } }
232 { $contract "Writes an element to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
233 $io-error ;
234
235 HELP: write
236 { $values { "seq" { $or byte-array string f } } }
237 { $description "Writes a sequence of elements to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
238 $io-error ;
239
240 HELP: flush
241 { $description "Waits for any pending output on " { $link output-stream } " to complete." }
242 $io-error ;
243
244 HELP: nl
245 { $description "Writes a line terminator to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
246 $io-error ;
247
248 HELP: print
249 { $values { "str" string } }
250 { $description "Writes a newline-terminated string to " { $link output-stream } "." }
251 $io-error ;
252
253 HELP: with-input-stream
254 { $values { "stream" "an input stream" } { "quot" quotation } }
255 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
256
257 HELP: with-output-stream
258 { $values { "stream" "an output stream" } { "quot" quotation } }
259 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } ". The stream is closed if the quotation returns or throws an error." } ;
260
261 HELP: with-streams
262 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
263 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } ". The streams are closed if the quotation returns or throws an error." } ;
264
265 HELP: with-streams*
266 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
267 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "input" } " and " { $link output-stream } " rebound to " { $snippet "output" } "." }
268 { $notes "This word does not close the streams. Compare with " { $link with-streams } "." } ;
269
270 { with-input-stream with-input-stream* } related-words
271
272 { with-output-stream with-output-stream* } related-words
273
274 HELP: with-input-stream*
275 { $values { "stream" "an input stream" } { "quot" quotation } }
276 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } "." }
277 { $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
278
279 HELP: with-output-stream*
280 { $values { "stream" "an output stream" } { "quot" quotation } }
281 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } "." }
282 { $examples
283   { $unchecked-example
284     "USING: destructors io io.encodings.utf8 io.files prettyprint ;"
285     "\"/tmp/test.txt\" utf8 <file-writer> dup [ \"Hello!\" write ] with-output-stream* dispose"
286     "\"/tmp/test.txt\" utf8 file-contents ."
287     "\"Hello!\""
288   }
289 }
290 { $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
291
292 HELP: bl
293 { $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
294 $io-error ;
295
296 HELP: stream-lines
297 { $values { "stream" "an input stream" } { "seq" { $sequence string } } }
298 { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
299
300 HELP: read-lines
301 { $values { "seq" { $sequence string } } }
302 { $description "Reads lines of text until from the " { $link input-stream } " until it is exhausted, collecting them in a sequence of strings." } ;
303
304 HELP: each-line
305 { $values { "quot" { $quotation ( ... line -- ... ) } } }
306 { $description "Calls the quotation with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
307
308 HELP: each-block
309 { $values { "quot" { $quotation ( ... block -- ... ) } } }
310 { $description "Calls the quotation with successive blocks of data, until the current " { $link input-stream } " is exhausted." } ;
311
312 HELP: stream-contents
313 { $values { "stream" "an input stream" } { "seq" { $or string byte-array } } }
314 { $description "Reads all elements in the given stream until the stream is exhausted. The type of the sequence depends on the stream's element type. The stream is closed after completion." }
315 $io-error ;
316
317 HELP: read-contents
318 { $values { "seq" { $or string byte-array } } }
319 { $description "Reads all elements in the " { $link input-stream } " until the stream is exhausted. The type of the sequence depends on the stream's element type." }
320 $io-error ;
321
322 HELP: tell-input
323 { $values
324         { "n" integer }
325 }
326 { $description "Returns the index of the stream stored in " { $link input-stream } "." } ;
327
328 HELP: tell-output
329 { $values
330         { "n" integer }
331 }
332 { $description "Returns the index of the stream stored in " { $link output-stream } "." } ;
333
334 ARTICLE: "stream-protocol" "Stream protocol"
335 "The stream protocol consists of a large number of generic words, many of which are optional."
336 $nl
337 "Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written to use " { $link "stdio" } " instead, wrapping I/O operations such as " { $link read } " and " { $link write } " in " { $link with-input-stream } " and " { $link with-output-stream } "."
338 $nl
339 "All streams must implement the " { $link dispose } " word in addition to the stream protocol."
340 { $subsections "stream-types" }
341 "These words are required for binary and string input streams:"
342 { $subsections
343     stream-read1
344     stream-read-unsafe
345     stream-read-until
346     stream-read-partial-unsafe
347 }
348 "The " { $link stream-read-unsafe } " and " { $link stream-read-partial-unsafe } " words should be implemented by streams but not used by client code. The following safe words are provided for reading from input streams:"
349 { $subsections
350     stream-read
351     stream-read-into
352     stream-read-partial
353     stream-read-partial-into
354 }
355 "This word is only required for string input streams:"
356 { $subsections stream-readln }
357 "These words are required for binary and string output streams:"
358 { $subsections
359     stream-flush
360     stream-write1
361     stream-write
362 }
363 "This word is only required for string output streams:"
364 { $subsections stream-nl }
365 "These words are for seekable streams:"
366 { $subsections
367     stream-seekable?
368     stream-tell
369     stream-seek
370     tell-input
371     tell-output
372     stream-length
373 }
374 { $see-also "io.timeouts" } ;
375
376 ARTICLE: "stdio-motivation" "Motivation for default streams"
377 "Most I/O code only operates on one stream at a time. The " { $link input-stream } " and " { $link output-stream } " variables are implicit parameters used by many I/O words. Using this idiom improves code in three ways:"
378 { $list
379     { "Code becomes simpler because there is no need to keep a stream around on the stack." }
380     { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
381     { "Code becomes more reusable because it can be written to not worry about which stream is being used, and instead the caller can use " { $link with-input-stream } " or " { $link with-output-stream } " to specify the source or destination for I/O operations." }
382 }
383 "For example, here is a program which reads the first line of a file, converts it to an integer, then reads that many characters, and splits them into groups of 16:"
384 { $code
385     "USING: continuations kernel io io.files math.parser splitting ;"
386     "\"data.txt\" utf8 <file-reader>"
387     "dup stream-readln string>number over stream-read 16 group"
388     "swap dispose"
389 }
390 "This code has two problems: it has some unnecessary stack shuffling, and if either " { $link stream-readln } " or " { $link stream-read } " throws an I/O error, the stream is not closed because " { $link dispose } " is never reached. So we can add a call to " { $link with-disposal } " to ensure the stream is always closed:"
391 { $code
392     "USING: continuations kernel io io.files math.parser splitting ;"
393     "\"data.txt\" utf8 <file-reader> ["
394     "    dup stream-readln string>number over stream-read"
395     "    16 group"
396     "] with-disposal"
397 }
398 "This code is robust, however it is more complex than it needs to be. This is where the default stream words come in; using them, the above can be rewritten as follows:"
399 { $code
400     "USING: continuations kernel io io.files math.parser splitting ;"
401     "\"data.txt\" utf8 <file-reader> ["
402     "    readln string>number read 16 group"
403     "] with-input-stream"
404 }
405 "An even better implementation that takes advantage of a utility word:"
406 { $code
407     "USING: continuations kernel io io.files math.parser splitting ;"
408     "\"data.txt\" utf8 ["
409     "    readln string>number read 16 group"
410     "] with-file-reader"
411 } ;
412
413 ARTICLE: "stdio" "Default input and output streams"
414 { $subsections "stdio-motivation" }
415 "The default input stream is stored in a dynamically-scoped variable:"
416 { $subsections input-stream }
417 "Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
418 $nl
419 "Words reading from the default input stream:"
420 { $subsections
421     read1
422     read
423     read-into
424     read-until
425     read-partial
426     read-partial-into
427     readln
428 }
429 "If the default input stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be read:"
430 { $subsections readln }
431 "Seeking on the default input stream:"
432 { $subsections seek-input }
433 "A pair of combinators for rebinding the " { $link input-stream } " variable:"
434 { $subsections
435     with-input-stream
436     with-input-stream*
437 }
438 "The default output stream is stored in a dynamically-scoped variable:"
439 { $subsections output-stream }
440 "Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
441 $nl
442 "Words writing to the default output stream:"
443 { $subsections
444     flush
445     write1
446     write
447 }
448 "If the default output stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be written:"
449 { $subsections
450     print
451     nl
452     bl
453 }
454 "Seeking on the default output stream:"
455 { $subsections seek-output }
456 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
457 { $subsections
458     with-output-stream
459     with-output-stream*
460 }
461 "A pair of combinators for rebinding both default streams at once:"
462 { $subsections
463     with-streams
464     with-streams*
465 } ;
466
467 ARTICLE: "stream-utils" "Stream utilities"
468 "There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
469 $nl
470 "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
471 { $subsections stream-print }
472 "Processing lines one by one:"
473 { $subsections
474     stream-lines
475     read-lines
476     each-line
477 }
478 "Processing blocks of data:"
479 { $subsections
480     stream-contents
481     read-contents
482     each-block
483 }
484 "Copying the contents of one stream to another:"
485 { $subsections
486     stream-copy*
487     stream-copy
488 } ;
489
490 ARTICLE: "stream-examples" "Stream example"
491 "Ask the user for their age, and print it back:"
492 { $code
493     "USING: io math.parser ;"
494     ""
495     ": ask-age ( -- ) \"How old are you?\" print ;"
496     ""
497     ": read-age ( -- n ) readln string>number ;"
498     ""
499     ": print-age ( n -- )"
500     "    \"You are \" write"
501     "    number>string write"
502     "    \" years old.\" print ;"
503     ": example ( -- ) ask-age read-age print-age ;"
504     ""
505     "example"
506 } ;
507
508 ARTICLE: "streams" "Streams"
509 "Input and output centers on the concept of a " { $emphasis "stream" } ", which is a source or sink of " { $emphasis "elements" } "."
510 { $subsections "stream-examples" }
511 "A stream can either be passed around on the stack or bound to a dynamic variable and used as one of the two implicit " { $emphasis "default streams" } "."
512 { $subsections
513     "stream-protocol"
514     "stdio"
515     "stream-utils"
516 }
517 { $see-also "io.streams.string" "io.streams.plain" "io.streams.duplex" } ;
518
519 ABOUT: "streams"