1 USING: help.markup help.syntax quotations hashtables kernel
2 classes strings continuations destructors math ;
6 { $values { "stream" "an input stream" } { "str/f" "a string or " { $link f } } }
7 { $contract "Reads a line of input from the stream. Outputs " { $link f } " on stream exhaustion." }
8 { $notes "Most code only works on one stream at a time and should instead use " { $link readln } "; see " { $link "stdio" } "." }
12 { $values { "stream" "an input stream" } { "ch/f" "a character or " { $link f } } }
13 { $contract "Reads a character of input from the stream. Outputs " { $link f } " on stream exhaustion." }
14 { $notes "Most code only works on one stream at a time and should instead use " { $link read1 } "; see " { $link "stdio" } "." }
18 { $values { "n" "a non-negative integer" } { "stream" "an input stream" } { "str/f" "a string or " { $link f } } }
19 { $contract "Reads " { $snippet "n" } " characters of input from the stream. Outputs a truncated string or " { $link f } " on stream exhaustion." }
20 { $notes "Most code only works on one stream at a time and should instead use " { $link read } "; see " { $link "stdio" } "." }
23 HELP: stream-read-until
24 { $values { "seps" string } { "stream" "an input stream" } { "str/f" "a string or " { $link f } } { "sep/f" "a character or " { $link f } } }
25 { $contract "Reads characters from the stream, until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator character 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 } "." }
26 { $notes "Most code only works on one stream at a time and should instead use " { $link read-until } "; see " { $link "stdio" } "." }
29 HELP: stream-read-partial
31 { "n" integer } { "stream" "an input stream" }
32 { "str/f" "a string or " { $link f } } }
33 { $description "Reads at most " { $snippet "n" } " characters from a stream and returns up to that many characters without blocking. If no characters are available, blocks until some are and returns them." } ;
36 { $values { "ch" "a character" } { "stream" "an output stream" } }
37 { $contract "Writes a character of output to the stream. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
38 { $notes "Most code only works on one stream at a time and should instead use " { $link write1 } "; see " { $link "stdio" } "." }
42 { $values { "str" string } { "stream" "an output stream" } }
43 { $contract "Writes a string of output to the stream. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
44 { $notes "Most code only works on one stream at a time and should instead use " { $link write } "; see " { $link "stdio" } "." }
48 { $values { "stream" "an output stream" } }
49 { $contract "Waits for any pending output to complete." }
50 { $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." }
51 { $notes "Most code only works on one stream at a time and should instead use " { $link flush } "; see " { $link "stdio" } "." }
55 { $values { "stream" "an output stream" } }
56 { $contract "Writes a line terminator. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
57 { $notes "Most code only works on one stream at a time and should instead use " { $link nl } "; see " { $link "stdio" } "." }
62 { $values { "str" string } { "stream" "an output stream" } }
63 { $description "Writes a newline-terminated string." }
64 { $notes "Most code only works on one stream at a time and should instead use " { $link print } "; see " { $link "stdio" } "." }
68 { $values { "in" "an input stream" } { "out" "an output stream" } }
69 { $description "Copies the contents of one stream into another, closing both streams when done." }
73 { $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
76 { $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
79 { $var-description "Holds an error stream." } ;
82 { $values { "str/f" "a string or " { $link f } } }
83 { $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
87 { $values { "ch/f" "a character or " { $link f } } }
88 { $description "Reads a character of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
92 { $values { "n" "a non-negative integer" } { "str/f" "a string or " { $link f } } }
93 { $description "Reads " { $snippet "n" } " characters of input from " { $link input-stream } ". Outputs a truncated string or " { $link f } " on stream exhaustion." }
97 { $values { "seps" string } { "str/f" "a string or " { $link f } } { "sep/f" "a character or " { $link f } } }
98 { $contract "Reads characters from " { $link input-stream } ". until the first occurrence of a separator character, or stream exhaustion. In the former case, the separator character 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 } "." }
105 { $description "Reads at most " { $snippet "n" } " characters from " { $link input-stream } " and returns up to that many characters without blocking. If no characters are available, blocks until some are and returns them." } ;
108 { $values { "ch" "a character" } }
109 { $contract "Writes a character of output to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
113 { $values { "str" string } }
114 { $description "Writes a string of output to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
118 { $description "Waits for any pending output on " { $link output-stream } " to complete." }
122 { $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." }
126 { $values { "string" string } }
127 { $description "Writes a newline-terminated string to " { $link output-stream } "." }
130 HELP: with-input-stream
131 { $values { "stream" "an input stream" } { "quot" quotation } }
132 { $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." } ;
134 HELP: with-output-stream
135 { $values { "stream" "an output stream" } { "quot" quotation } }
136 { $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." } ;
139 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
140 { $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 stream is closed if the quotation returns or throws an error." } ;
143 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
144 { $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" } "." }
145 { $notes "This word does not close the stream. Compare with " { $link with-streams } "." } ;
147 { with-input-stream with-input-stream* } related-words
149 { with-output-stream with-output-stream* } related-words
151 HELP: with-input-stream*
152 { $values { "stream" "an input stream" } { "quot" quotation } }
153 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to " { $snippet "stream" } "." }
154 { $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
156 HELP: with-output-stream*
157 { $values { "stream" "an output stream" } { "quot" quotation } }
158 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to " { $snippet "stream" } "." }
159 { $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
162 { $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
166 { $values { "stream" "an input stream" } { "seq" "a sequence of strings" } }
167 { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
170 { $values { "quot" { $quotation "( str -- )" } } }
171 { $description "Calls the quotation with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
174 { $values { "stream" "an input stream" } { "str" string } }
175 { $description "Reads the entire contents of a stream into a string." }
178 ARTICLE: "stream-protocol" "Stream protocol"
179 "The stream protocol consists of a large number of generic words, many of which are optional."
181 "Stream protocol words are rarely called directly, since code which only works with one stream at a time should be written 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 } "."
183 "All streams must implement the " { $link dispose } " word in addition to the stream protocol."
185 "These words are required for input streams:"
186 { $subsection stream-read1 }
187 { $subsection stream-read }
188 { $subsection stream-read-until }
189 { $subsection stream-readln }
190 { $subsection stream-read-partial }
191 "These words are required for output streams:"
192 { $subsection stream-flush }
193 { $subsection stream-write1 }
194 { $subsection stream-write }
195 { $subsection stream-nl }
196 { $see-also "io.timeouts" } ;
198 ARTICLE: "stdio" "Default input and output streams"
199 "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:"
201 { "Code becomes simpler because there is no need to keep a stream around on the stack." }
202 { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
203 { "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." }
205 "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:"
207 "USING: continuations kernel io io.files math.parser splitting ;"
208 "\"data.txt\" utf8 <file-reader>"
209 "dup stream-readln number>string over stream-read 16 group"
212 "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:"
214 "USING: continuations kernel io io.files math.parser splitting ;"
215 "\"data.txt\" utf8 <file-reader> ["
216 " dup stream-readln number>string over stream-read"
220 "This code is robust however it is more complex than it needs to be since. This is where the default stream words come in; using them, the above can be rewritten as follows:"
222 "USING: continuations kernel io io.files math.parser splitting ;"
223 "\"data.txt\" utf8 <file-reader> ["
224 " readln number>string read 16 group"
225 "] with-input-stream"
227 "An even better implementation that takes advantage of a utility word:"
229 "USING: continuations kernel io io.files math.parser splitting ;"
230 "\"data.txt\" utf8 ["
231 " readln number>string read 16 group"
234 "The default input stream is stored in a dynamically-scoped variable:"
235 { $subsection input-stream }
236 "Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
238 "Words reading from the default input stream:"
239 { $subsection read1 }
241 { $subsection read-until }
242 { $subsection readln }
243 { $subsection read-partial }
244 "A pair of combinators for rebinding the " { $link input-stream } " variable:"
245 { $subsection with-input-stream }
246 { $subsection with-input-stream* }
247 "The default output stream is stored in a dynamically-scoped variable:"
248 { $subsection output-stream }
249 "Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
251 "Words writing to the default input stream:"
252 { $subsection flush }
253 { $subsection write1 }
254 { $subsection write }
255 { $subsection print }
258 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
259 { $subsection with-output-stream }
260 { $subsection with-output-stream* }
261 "A pair of combinators for rebinding both default streams at once:"
262 { $subsection with-streams }
263 { $subsection with-streams* } ;
265 ARTICLE: "stream-utils" "Stream utilities"
266 "There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
268 "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
269 { $subsection stream-print }
270 "Processing lines one by one:"
271 { $subsection each-line }
272 "Sluring an entire stream into memory all at once:"
273 { $subsection lines }
274 { $subsection contents }
275 "Copying the contents of one stream to another:"
276 { $subsection stream-copy } ;
278 ARTICLE: "streams" "Streams"
279 "Input and output centers on the concept of a " { $emphasis "stream" } ", which is a source or sink of characters. Streams also support formatted output, which may be used to present styled text in a manner independent of output medium."
281 "A stream can either be passed around on the stack or bound to a dynamic variable and used as an implicit " { $emphasis "default stream" } "."
282 { $subsection "stream-protocol" }
283 { $subsection "stdio" }
284 { $subsection "stream-utils" }
285 { $see-also "io.streams.string" "io.streams.plain" "io.streams.duplex" } ;