]> gitweb.factorcode.org Git - factor.git/blob - core/io/io-docs.factor
43f66657a7d3dc3ad0d61302b26bff5a005d404f
[factor.git] / core / io / io-docs.factor
1 USING: help.markup help.syntax quotations hashtables kernel
2 classes strings continuations destructors math ;
3 IN: io
4
5 HELP: stream-readln
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" } "." }
9 $io-error ;
10
11 HELP: stream-read1
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" } "." }
15 $io-error ;
16
17 HELP: stream-read
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" } "." }
21 $io-error ;
22
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" } "." }
27 $io-error ;
28
29 HELP: stream-read-partial
30 { $values
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." } ;
34
35 HELP: stream-write1
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" } "." }
39 $io-error ;
40
41 HELP: stream-write
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" } "." }
45 $io-error ;
46
47 HELP: stream-flush
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" } "." }
52 $io-error ;
53
54 HELP: stream-nl
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" } "." }
58 $io-error ;
59
60 HELP: stream-format
61 { $values { "str" string } { "style" "a hashtable" } { "stream" "an output stream" } }
62 { $contract "Writes formatted text to the stream. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output."
63 $nl
64 "The " { $snippet "style" } " hashtable holds character style information. See " { $link "character-styles" } "." }
65 { $notes "Most code only works on one stream at a time and should instead use " { $link format } "; see " { $link "stdio" } "." }
66 $io-error ;
67
68 HELP: make-block-stream
69 { $values { "style" "a hashtable" } { "stream" "an output stream" } { "stream'" "an output stream" } }
70 { $contract "Creates an output stream which wraps " { $snippet "stream" } " and adds " { $snippet "style" } " on calls to " { $link stream-write } " and " { $link stream-format } "."
71 $nl
72 "Unlike " { $link make-span-stream } ", this creates a new paragraph block in the output."
73 $nl
74 "The " { $snippet "style" } " hashtable holds paragraph style information. See " { $link "paragraph-styles" } "." }
75 { $notes "Most code only works on one stream at a time and should instead use " { $link with-nesting } "; see " { $link "stdio" } "." }
76 $io-error ;
77
78 HELP: stream-write-table
79 { $values { "table-cells" "a sequence of sequences of table cells" } { "style" "a hashtable" } { "stream" "an output stream" } }
80 { $contract "Prints a table of cells produced by " { $link with-cell } "."
81 $nl
82 "The " { $snippet "style" } " hashtable holds table style information. See " { $link "table-styles" } "." }
83 { $notes "Most code only works on one stream at a time and should instead use " { $link tabular-output } "; see " { $link "stdio" } "." }
84 $io-error ;
85
86 HELP: make-cell-stream
87 { $values { "style" hashtable } { "stream" "an output stream" } { "stream'" object } }
88 { $contract "Creates an output stream which writes to a table cell object." }
89 { $notes "Most code only works on one stream at a time and should instead use " { $link with-cell } "; see " { $link "stdio" } "." }
90 $io-error ;
91
92 HELP: make-span-stream
93 { $values { "style" "a hashtable" } { "stream" "an output stream" } { "stream'" "an output stream" } }
94 { $contract "Creates an output stream which wraps " { $snippet "stream" } " and adds " { $snippet "style" } " on calls to " { $link stream-write } " and " { $link stream-format } "."
95 $nl
96 "Unlike " { $link make-block-stream } ", the stream output is inline, and not nested in a paragraph block." }
97 { $notes "Most code only works on one stream at a time and should instead use " { $link with-style } "; see " { $link "stdio" } "." }
98 $io-error ;
99
100 HELP: stream-print
101 { $values { "str" string } { "stream" "an output stream" } }
102 { $description "Writes a newline-terminated string." }
103 { $notes "Most code only works on one stream at a time and should instead use " { $link print } "; see " { $link "stdio" } "." }
104 $io-error ;
105
106 HELP: stream-copy
107 { $values { "in" "an input stream" } { "out" "an output stream" } }
108 { $description "Copies the contents of one stream into another, closing both streams when done." } 
109 $io-error ;
110
111 HELP: input-stream
112 { $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
113
114 HELP: output-stream
115 { $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
116
117 HELP: readln
118 { $values { "str/f" "a string or " { $link f } } }
119 { $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
120 $io-error ;
121
122 HELP: read1
123 { $values { "ch/f" "a character or " { $link f } } }
124 { $description "Reads a character of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
125 $io-error ;
126
127 HELP: read
128 { $values { "n" "a non-negative integer" } { "str/f" "a string or " { $link f } } }
129 { $description "Reads " { $snippet "n" } " characters of input from " { $link input-stream } ". Outputs a truncated string or " { $link f } " on stream exhaustion." }
130 $io-error ;
131
132 HELP: read-until
133 { $values { "seps" string } { "str/f" "a string or " { $link f } } { "sep/f" "a character or " { $link f } } }
134 { $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 } "." }
135 $io-error ;
136
137 HELP: read-partial
138 { $values
139      { "n" null }
140      { "str/f" null } }
141 { $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." } ;
142
143 HELP: write1
144 { $values { "ch" "a character" } }
145 { $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." }
146 $io-error ;
147
148 HELP: write
149 { $values { "str" string } }
150 { $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." }
151 $io-error ;
152
153 HELP: flush
154 { $description "Waits for any pending output on " { $link output-stream } " to complete." }
155 $io-error ;
156
157 HELP: nl
158 { $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." }
159 $io-error ;
160
161 HELP: format
162 { $values { "str" string } { "style" "a hashtable" } }
163 { $description "Writes formatted text to " { $link output-stream } ". If the stream does buffering, output may not be performed immediately; use " { $link flush } " to force output." }
164 { $notes "Details are in the documentation for " { $link stream-format } "." }
165 $io-error ;
166
167 HELP: with-nesting
168 { $values { "style" "a hashtable" } { "quot" quotation } }
169 { $description "Calls the quotation in a new dynamic scope with " { $link output-stream } " rebound to a nested paragraph stream, with formatting information applied." }
170 { $notes "Details are in the documentation for " { $link make-block-stream } "." }
171 $io-error ;
172
173 HELP: tabular-output
174 { $values { "style" "a hashtable" } { "quot" quotation } }
175 { $description "Calls a quotation which emits a series of equal-length table rows using " { $link with-row } ". The results are laid out in a tabular fashion on " { $link output-stream } "."
176 $nl
177 "The " { $snippet "style" } " hashtable holds table style information. See " { $link "table-styles" } "." }
178 { $examples
179     { $code
180         "{ { 1 2 } { 3 4 } }"
181         "H{ { table-gap { 10 10 } } } ["
182         "    [ [ [ [ . ] with-cell ] each ] with-row ] each"
183         "] tabular-output"
184     }
185 }
186 $io-error ;
187
188 HELP: with-row
189 { $values { "quot" quotation } }
190 { $description "Calls a quotation which emits a series of table cells using " { $link with-cell } ". This word can only be called inside the quotation given to " { $link tabular-output } "." }
191 $io-error ;
192
193 HELP: with-cell
194 { $values { "quot" quotation } }
195 { $description "Calls a quotation in a new scope with " { $link output-stream } " rebound. Output performed by the quotation is displayed in a table cell. This word can only be called inside the quotation given to " { $link with-row } "." }
196 $io-error ;
197
198 HELP: write-cell
199 { $values { "str" string } }
200 { $description "Outputs a table cell containing a single string. This word can only be called inside the quotation given to " { $link with-row } "." }
201 $io-error ;
202
203 HELP: with-style
204 { $values { "style" "a hashtable" } { "quot" quotation } }
205 { $description "Calls the quotation in a new dynamic scope where calls to " { $link write } ", " { $link format } " and other stream output words automatically inherit style settings from " { $snippet "style" } "." }
206 { $notes "Details are in the documentation for " { $link make-span-stream } "." }
207 $io-error ;
208
209 HELP: print
210 { $values { "string" string } }
211 { $description "Writes a newline-terminated string to " { $link output-stream } "." }
212 $io-error ;
213
214 HELP: with-input-stream
215 { $values { "stream" "an input stream" } { "quot" quotation } }
216 { $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." } ;
217
218 HELP: with-output-stream
219 { $values { "stream" "an output stream" } { "quot" quotation } }
220 { $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." } ;
221
222 HELP: with-streams
223 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
224 { $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." } ;
225
226 HELP: with-streams*
227 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
228 { $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" } "." }
229 { $notes "This word does not close the stream. Compare with " { $link with-streams } "." } ;
230
231 { with-input-stream with-input-stream* } related-words
232
233 { with-output-stream with-output-stream* } related-words
234
235 HELP: with-input-stream*
236 { $values { "stream" "an input stream" } { "quot" quotation } }
237 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to  " { $snippet "stream" } "." }
238 { $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
239
240 HELP: with-output-stream*
241 { $values { "stream" "an output stream" } { "quot" quotation } }
242 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to  " { $snippet "stream" } "." }
243 { $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
244
245 HELP: bl
246 { $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
247 $io-error ;
248
249 HELP: lines
250 { $values { "stream" "an input stream" } { "seq" "a sequence of strings" } }
251 { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
252
253 HELP: contents
254 { $values { "stream" "an input stream" } { "str" string } }
255 { $description "Reads the entire contents of a stream into a string." }
256 $io-error ;
257
258 ARTICLE: "stream-protocol" "Stream protocol"
259 "The stream protocol consists of a large number of generic words, many of which are optional."
260 $nl
261 "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 } "."
262 $nl
263 "All streams must implement the " { $link dispose } " word in addition to the stream protocol."
264 $nl
265 "These words are required for input streams:"
266 { $subsection stream-read1 }
267 { $subsection stream-read }
268 { $subsection stream-read-until }
269 { $subsection stream-readln }
270 { $subsection stream-read-partial }
271 "These words are required for output streams:"
272 { $subsection stream-flush }
273 { $subsection stream-write1 }
274 { $subsection stream-write }
275 { $subsection stream-format }
276 { $subsection stream-nl }
277 { $subsection make-span-stream }
278 { $subsection make-block-stream }
279 { $subsection make-cell-stream }
280 { $subsection stream-write-table }
281 { $see-also "io.timeouts" } ;
282
283 ARTICLE: "stdio" "Default input and output streams"
284 "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:"
285 { $list
286     { "Code becomes simpler because there is no need to keep a stream around on the stack." }
287     { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
288     { "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." }
289 }
290 "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:"
291 { $code
292     "USING: continuations kernel io io.files math.parser splitting ;"
293     "\"data.txt\" utf8 <file-reader>"
294     "dup stream-readln number>string over stream-read 16 group"
295     "swap dispose"
296 }
297 "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:"
298 { $code
299     "USING: continuations kernel io io.files math.parser splitting ;"
300     "\"data.txt\" utf8 <file-reader> ["
301     "    dup stream-readln number>string over stream-read"
302     "    16 group"
303     "] with-disposal"
304 }
305 "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:"
306 { $code
307     "USING: continuations kernel io io.files math.parser splitting ;"
308     "\"data.txt\" utf8 <file-reader> ["
309     "    readln number>string read 16 group"
310     "] with-input-stream"
311 }
312 "An even better implementation that takes advantage of a utility word:"
313 { $code
314     "USING: continuations kernel io io.files math.parser splitting ;"
315     "\"data.txt\" utf8 ["
316     "    readln number>string read 16 group"
317     "] with-file-reader"
318 }
319 "The default input stream is stored in a dynamically-scoped variable:"
320 { $subsection input-stream }
321 "Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
322 $nl
323 "Words reading from the default input stream:"
324 { $subsection read1 }
325 { $subsection read }
326 { $subsection read-until }
327 { $subsection readln }
328 { $subsection read-partial }
329 "A pair of combinators for rebinding the " { $link input-stream } " variable:"
330 { $subsection with-input-stream }
331 { $subsection with-input-stream* }
332 "The default output stream is stored in a dynamically-scoped variable:"
333 { $subsection output-stream }
334 "Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
335 $nl
336 "Words writing to the default input stream:"
337 { $subsection flush }
338 { $subsection write1 }
339 { $subsection write }
340 { $subsection print }
341 { $subsection nl }
342 { $subsection bl }
343 "Formatted output:"
344 { $subsection format }
345 { $subsection with-style }
346 { $subsection with-nesting }
347 "Tabular output:"
348 { $subsection tabular-output }
349 { $subsection with-row }
350 { $subsection with-cell }
351 { $subsection write-cell }
352 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
353 { $subsection with-output-stream }
354 { $subsection with-output-stream* }
355 "A pair of combinators for rebinding both default streams at once:"
356 { $subsection with-streams }
357 { $subsection with-streams* } ;
358
359 ARTICLE: "stream-utils" "Stream utilities"
360 "There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
361 $nl
362 "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
363 { $subsection stream-print }
364 "Sluring an entire stream into memory all at once:"
365 { $subsection lines }
366 { $subsection contents }
367 "Copying the contents of one stream to another:"
368 { $subsection stream-copy } ;
369
370 ARTICLE: "streams" "Streams"
371 "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."
372 $nl
373 "A stream can either be passed around on the stack or bound to a dynamic variable and used as an implicit " { $emphasis "default stream" } "."
374 { $subsection "stream-protocol" }
375 { $subsection "stdio" }
376 { $subsection "stream-utils" }
377 { $see-also "io.streams.string" "io.streams.plain" "io.streams.duplex" } ;
378
379 ABOUT: "streams"