]> gitweb.factorcode.org Git - factor.git/blob - core/io/io-docs.factor
Specialized arrays, structs and other objects responding to the >c-ptr / byte-length...
[factor.git] / core / io / io-docs.factor
1 USING: help.markup help.syntax quotations hashtables kernel
2 classes strings continuations destructors math byte-arrays
3 alien ;
4 IN: io
5
6 ARTICLE: "stream-types" "Binary and text streams"
7 "A word which outputs the stream element type:"
8 { $subsections stream-element-type }
9 "Stream element types:"
10 { $subsections +byte+ +character+ }
11 "The stream element type is the data type read and written by " { $link stream-read1 } " and " { $link stream-write1 } "."
12 $nl
13 "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."
14 $nl
15 "Character streams have an element tye 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."
16 $nl
17 "Most external streams are binary streams, and can be wrapped in string streams once a suitable encoding has been provided; see " { $link "io.encodings" } "." ;
18
19 HELP: +byte+
20 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
21
22 HELP: +character+
23 { $description "A stream element type. See " { $link stream-element-type } " for explanation." } ;
24
25 HELP: stream-element-type
26 { $values { "stream" "a stream" } { "type" { $link +byte+ } " or " { $link +character+ } } }
27 { $contract "Outputs one of " { $link +byte+ } " or " { $link +character+ } "." } ;
28
29 HELP: stream-readln
30 { $values { "stream" "an input stream" } { "str/f" "a string or " { $link f } } }
31 { $contract "Reads a line of input from the stream. Outputs " { $link f } " on stream exhaustion." }
32 { $notes "Most code only works on one stream at a time and should instead use " { $link readln } "; see " { $link "stdio" } "." }
33 $io-error ;
34
35 HELP: stream-read1
36 { $values { "stream" "an input stream" } { "elt" "an element or " { $link f } } }
37 { $contract "Reads an element from the stream. Outputs " { $link f } " on stream exhaustion." }
38 { $notes "Most code only works on one stream at a time and should instead use " { $link read1 } "; see " { $link "stdio" } "." }
39 $io-error ;
40
41 HELP: stream-read
42 { $values { "n" "a non-negative integer" } { "stream" "an input stream" } { "seq" { $or byte-array string f } } }
43 { $contract "Reads " { $snippet "n" } " elements from the stream. Outputs a truncated string or " { $link f } " on stream exhaustion." }
44 { $notes "Most code only works on one stream at a time and should instead use " { $link read } "; see " { $link "stdio" } "." }
45 $io-error ;
46
47 HELP: stream-read-until
48 { $values { "seps" string } { "stream" "an input stream" } { "seq" { $or byte-array string f } } { "sep/f" "a character or " { $link f } } }
49 { $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 } "." }
50 { $notes "Most code only works on one stream at a time and should instead use " { $link read-until } "; see " { $link "stdio" } "." }
51 $io-error ;
52
53 HELP: stream-read-partial
54 { $values
55      { "n" "a non-negative integer" } { "stream" "an input stream" }
56      { "seq" { $or byte-array string f } } }
57 { $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." } ;
58
59 HELP: stream-write1
60 { $values { "elt" "an element" } { "stream" "an output stream" } }
61 { $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." }
62 { $notes "Most code only works on one stream at a time and should instead use " { $link write1 } "; see " { $link "stdio" } "." }
63 $io-error ;
64
65 HELP: stream-write
66 { $values { "data" "binary data or a string" } { "stream" "an output stream" } }
67 { $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." }
68 { $notes "Most code only works on one stream at a time and should instead use " { $link write } "; see " { $link "stdio" } "." }
69 $io-error ;
70
71 HELP: stream-flush
72 { $values { "stream" "an output stream" } }
73 { $contract "Waits for any pending output to complete." }
74 { $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." }
75 { $notes "Most code only works on one stream at a time and should instead use " { $link flush } "; see " { $link "stdio" } "." }
76 $io-error ;
77
78 HELP: stream-nl
79 { $values { "stream" "an output stream" } }
80 { $contract "Writes a line terminator. If the stream does buffering, output may not be performed immediately; use " { $link stream-flush } " to force output." }
81 { $notes "Most code only works on one stream at a time and should instead use " { $link nl } "; see " { $link "stdio" } "." }
82 $io-error ;
83
84 HELP: stream-print
85 { $values { "str" string } { "stream" "an output stream" } }
86 { $description "Writes a newline-terminated string." }
87 { $notes "Most code only works on one stream at a time and should instead use " { $link print } "; see " { $link "stdio" } "." }
88 $io-error ;
89
90 HELP: stream-copy
91 { $values { "in" "an input stream" } { "out" "an output stream" } }
92 { $description "Copies the contents of one stream into another, closing both streams when done." } 
93 $io-error ;
94
95 HELP: stream-tell
96 { $values
97      { "stream" "a stream" } { "n" integer }
98 }
99 { $description "Returns the index of the stream pointer if the stream is seekable." }
100 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
101
102
103 HELP: stream-seek
104 { $values
105      { "n" integer } { "seek-type" "a seek singleton" } { "stream" "a stream" }
106 }
107 { $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
108     "Three methods of seeking are supported:"
109     { $list { $link seek-absolute } { $link seek-relative } { $link seek-end } }
110 }
111 { $notes "Stream seeking is not supported on streams that do not have a known length, e.g. TCP/IP streams." } ;
112
113 HELP: seek-absolute
114 { $values
115     
116      { "value" "a seek singleton" }
117 }
118 { $description "Seeks to an offset from the beginning of the stream." } ;
119
120 HELP: seek-end
121 { $values
122     
123      { "value" "a seek singleton" }
124 }
125 { $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." } ;
126
127 HELP: seek-relative
128 { $values
129     
130      { "value" "a seek singleton" }
131 }
132 { $description "Seeks to an offset from the current position of the stream pointer." } ;
133
134 { seek-absolute seek-relative seek-end } related-words
135
136 HELP: seek-input
137 { $values
138      { "n" integer } { "seek-type" "a seek singleton" }
139 }
140 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link input-stream } "." } ;
141
142 HELP: seek-output
143 { $values
144      { "n" integer } { "seek-type" "a seek singleton" }
145 }
146 { $description "Calls " { $link stream-seek } " on the stream stored in " { $link output-stream } "." } ;
147
148 HELP: input-stream
149 { $var-description "Holds an input stream for various implicit stream operations. Rebound using " { $link with-input-stream } " and " { $link with-input-stream* } "." } ;
150
151 HELP: output-stream
152 { $var-description "Holds an output stream for various implicit stream operations. Rebound using " { $link with-output-stream } " and " { $link with-output-stream* } "." } ;
153
154 HELP: error-stream
155 { $var-description "Holds an error stream." } ;
156
157 HELP: readln
158 { $values { "str/f" "a string or " { $link f } } }
159 { $description "Reads a line of input from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
160 $io-error ;
161
162 HELP: read1
163 { $values { "elt" "an element or " { $link f } } }
164 { $description "Reads an element from " { $link input-stream } ". Outputs " { $link f } " on stream exhaustion." }
165 $io-error ;
166
167 HELP: read
168 { $values { "n" "a non-negative integer" } { "seq" { $or byte-array string f } } }
169 { $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." }
170 $io-error ;
171
172 HELP: read-until
173 { $values { "seps" string } { "seq" { $or byte-array string f } } { "sep/f" "a character or " { $link f } } }
174 { $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 } "." }
175 $io-error ;
176
177 HELP: read-partial
178 { $values { "n" integer } { "seq" { $or byte-array string f } } }
179 { $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." } ;
180
181 HELP: write1
182 { $values { "elt" "an element" } }
183 { $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." }
184 $io-error ;
185
186 HELP: write
187 { $values { "seq" { $or byte-array string f } } }
188 { $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." }
189 $io-error ;
190
191 HELP: flush
192 { $description "Waits for any pending output on " { $link output-stream } " to complete." }
193 $io-error ;
194
195 HELP: nl
196 { $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." }
197 $io-error ;
198
199 HELP: print
200 { $values { "str" string } }
201 { $description "Writes a newline-terminated string to " { $link output-stream } "." }
202 $io-error ;
203
204 HELP: with-input-stream
205 { $values { "stream" "an input stream" } { "quot" quotation } }
206 { $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." } ;
207
208 HELP: with-output-stream
209 { $values { "stream" "an output stream" } { "quot" quotation } }
210 { $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." } ;
211
212 HELP: with-streams
213 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
214 { $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." } ;
215
216 HELP: with-streams*
217 { $values { "input" "an input stream" } { "output" "an output stream" } { "quot" quotation } }
218 { $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" } "." }
219 { $notes "This word does not close the stream. Compare with " { $link with-streams } "." } ;
220
221 { with-input-stream with-input-stream* } related-words
222
223 { with-output-stream with-output-stream* } related-words
224
225 HELP: with-input-stream*
226 { $values { "stream" "an input stream" } { "quot" quotation } }
227 { $description "Calls the quotation in a new dynamic scope, with " { $link input-stream } " rebound to  " { $snippet "stream" } "." }
228 { $notes "This word does not close the stream. Compare with " { $link with-input-stream } "." } ;
229
230 HELP: with-output-stream*
231 { $values { "stream" "an output stream" } { "quot" quotation } }
232 { $description "Calls the quotation in a new dynamic scope, with " { $link output-stream } " rebound to  " { $snippet "stream" } "." }
233 { $notes "This word does not close the stream. Compare with " { $link with-output-stream } "." } ;
234
235 HELP: bl
236 { $description "Outputs a space character (" { $snippet "\" \"" } ") to " { $link output-stream } "." }
237 $io-error ;
238
239 HELP: stream-lines
240 { $values { "stream" "an input stream" } { "seq" "a sequence of strings" } }
241 { $description "Reads lines of text until the stream is exhausted, collecting them in a sequence of strings." } ;
242
243 HELP: lines
244 { $values { "seq" "a sequence of strings" } }
245 { $description "Reads lines of text until from the " { $link input-stream } " until it is exhausted, collecting them in a sequence of strings." } ;
246
247 HELP: each-line
248 { $values { "quot" { $quotation "( str -- )" } } }
249 { $description "Calls the quotation with successive lines of text, until the current " { $link input-stream } " is exhausted." } ;
250
251 HELP: each-block
252 { $values { "quot" { $quotation "( block -- )" } } }
253 { $description "Calls the quotation with successive blocks of data, until the current " { $link input-stream } " is exhausted." } ;
254
255 HELP: stream-contents
256 { $values { "stream" "an input stream" } { "seq" { $or string byte-array } } }
257 { $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." }
258 $io-error ;
259
260 HELP: contents
261 { $values { "seq" { $or string byte-array } } }
262 { $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." }
263 $io-error ;
264
265 ARTICLE: "stream-protocol" "Stream protocol"
266 "The stream protocol consists of a large number of generic words, many of which are optional."
267 $nl
268 "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 } "."
269 $nl
270 "All streams must implement the " { $link dispose } " word in addition to the stream protocol."
271 { $subsections "stream-types" }
272 "These words are required for binary and string input streams:"
273 { $subsections
274     stream-read1
275     stream-read
276     stream-read-until
277     stream-read-partial
278 }
279 "This word is only required for string input streams:"
280 { $subsections stream-readln }
281 "These words are required for binary and string output streams:"
282 { $subsections
283     stream-flush
284     stream-write1
285     stream-write
286 }
287 "This word is only required for string output streams:"
288 { $subsections stream-nl }
289 "These words are for seekable streams:"
290 { $subsections
291     stream-tell
292     stream-seek
293 }
294 { $see-also "io.timeouts" } ;
295
296 ARTICLE: "stdio-motivation" "Motivation for default streams"
297 "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:"
298 { $list
299     { "Code becomes simpler because there is no need to keep a stream around on the stack." }
300     { "Code becomes more robust because " { $link with-input-stream } " and " { $link with-output-stream } " automatically close the streams if there is an error." }
301     { "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." }
302 }
303 "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:"
304 { $code
305     "USING: continuations kernel io io.files math.parser splitting ;"
306     "\"data.txt\" utf8 <file-reader>"
307     "dup stream-readln string>number over stream-read 16 group"
308     "swap dispose"
309 }
310 "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:"
311 { $code
312     "USING: continuations kernel io io.files math.parser splitting ;"
313     "\"data.txt\" utf8 <file-reader> ["
314     "    dup stream-readln string>number over stream-read"
315     "    16 group"
316     "] with-disposal"
317 }
318 "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:"
319 { $code
320     "USING: continuations kernel io io.files math.parser splitting ;"
321     "\"data.txt\" utf8 <file-reader> ["
322     "    readln string>number read 16 group"
323     "] with-input-stream"
324 }
325 "An even better implementation that takes advantage of a utility word:"
326 { $code
327     "USING: continuations kernel io io.files math.parser splitting ;"
328     "\"data.txt\" utf8 ["
329     "    readln string>number read 16 group"
330     "] with-file-reader"
331 } ;
332
333 ARTICLE: "stdio" "Default input and output streams"
334 { $subsections "stdio-motivation" }
335 "The default input stream is stored in a dynamically-scoped variable:"
336 { $subsections input-stream }
337 "Unless rebound in a child namespace, this variable will be set to a console stream for reading input from the user."
338 $nl
339 "Words reading from the default input stream:"
340 { $subsections
341     read1
342     read
343     read-until
344     read-partial
345 }
346 "If the default input stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be read:"
347 { $subsections readln }
348 "Seeking on the default input stream:"
349 { $subsections seek-input }
350 "A pair of combinators for rebinding the " { $link input-stream } " variable:"
351 { $subsections
352     with-input-stream
353     with-input-stream*
354 }
355 "The default output stream is stored in a dynamically-scoped variable:"
356 { $subsections output-stream }
357 "Unless rebound in a child namespace, this variable will be set to a console stream for showing output to the user."
358 $nl
359 "Words writing to the default output stream:"
360 { $subsections
361     flush
362     write1
363     write
364 }
365 "If the default output stream is a character stream (" { $link stream-element-type } " outputs " { $link +character+ } "), lines of text can be written:"
366 { $subsections
367     print
368     nl
369     bl
370 }
371 "Seeking on the default output stream:"
372 { $subsections seek-output }
373 "Seeking descriptors:"
374 { $subsections
375     seek-absolute
376     seek-relative
377     seek-end
378 }
379 "A pair of combinators for rebinding the " { $link output-stream } " variable:"
380 { $subsections
381     with-output-stream
382     with-output-stream*
383 }
384 "A pair of combinators for rebinding both default streams at once:"
385 { $subsections
386     with-streams
387     with-streams*
388 } ;
389
390 ARTICLE: "stream-utils" "Stream utilities"
391 "There are a few useful stream-related words which are not generic, but merely built up from the stream protocol."
392 $nl
393 "First, a simple composition of " { $link stream-write } " and " { $link stream-nl } ":"
394 { $subsections stream-print }
395 "Processing lines one by one:"
396 { $subsections
397     stream-lines
398     lines
399     each-line
400 }
401 "Processing blocks of data:"
402 { $subsections
403     stream-contents
404     contents
405     each-block
406 }
407 "Copying the contents of one stream to another:"
408 { $subsections stream-copy } ;
409
410 ARTICLE: "stream-examples" "Stream example"
411 "Ask the user for their age, and print it back:"
412 { $code
413     "USING: io math.parser ;"
414     ""
415     ": ask-age ( -- ) \"How old are you?\" print ;"
416     ""
417     ": read-age ( -- n ) readln string>number ;"
418     ""
419     ": print-age ( n -- )"
420     "    \"You are \" write"
421     "    number>string write"
422     "    \" years old.\" print ;"
423     ": example ( -- ) ask-age read-age print-age ;"
424     ""
425     "example"
426 } ;
427
428 ARTICLE: "streams" "Streams"
429 "Input and output centers on the concept of a " { $emphasis "stream" } ", which is a source or sink of " { $emphasis "elements" } "."
430 { $subsections "stream-examples" }
431 "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" } "."
432 { $subsections
433     "stream-protocol"
434     "stdio"
435     "stream-utils"
436 }
437 { $see-also "io.streams.string" "io.streams.plain" "io.streams.duplex" } ;
438
439 ABOUT: "streams"