Debian Codegraph @

src/doc/manual/standards/streams.txi

raw
1 @node Streams
2 @section Streams
3
4 @menu
5 * Streams - ANSI Streams::
6 * Streams - Dictionary::
7 * Streams - C Reference::
8 @end menu
9
10 @node Streams - ANSI Streams
11 @subsection ANSI Streams
12
13 @menu
14 * Streams - Supported types::
15 * Streams - Element types::
16 * Streams - External formats::
17 @end menu
18
19 @node Streams - Supported types
20 @subsubsection Supported types
21 ECL implements all stream types described in ANSI @bibcite{ANSI}. Additionally, when configured with option @code{--enable-clos-streams}, ECL includes a version of Gray streams where any object that implements the appropriate methods (@code{stream-input-p}, @code{stream-read-char}, etc) is a valid argument for the functions that expect streams, such as @code{read}, @code{print}, etc.
22
23 @node Streams - Element types
24 @subsubsection Element types
25 @lspindex write-char
26 @lspindex write-sequence
27 @lspindex read-char
28 ECL distinguishes between two kinds of streams: character streams and byte streams. @dfn{Character streams} only accept and produce characters, written or read one by one, with @code{write-char} or @code{read-char}, or in chunks, with @code{write-sequence} or any of the Lisp printer functions. Character operations are conditioned by the external format, as described in @ref{Streams - External formats}.
29
30 ANSI Common Lisp also supports binary streams. Here input and output is performed in chunks of bits. Binary streams are created with the function open passing as argument a subtype of integer and the implementation is free to round up that integer type to the closest size it supports. In particular ECL rounds up the size to a multiple of a byte. For example, the form @code{(open "foo.bin" :direction :output :element-type '(unsigned-byte 13))}, will open the file @code{foo.bin} for writing, using 16-bit words as the element type.
31
32
33 @node Streams - External formats
34 @subsubsection External formats
35 An @dfn{external format} is an encoding for characters that maps character codes to a sequence of bytes, in a one-to-one or one-to-many fashion. External formats are also known as "character encodings" in the programming world and are an essential ingredient to be able to read and write text in different languages and alphabets.
36
37 ECL has one of the most complete supports for @emph{external formats}, covering all of the usual codepages from the Windows and Unix world, up to the more recent UTF-8, UCS-2 and UCS-4 formats, all of them with big and small endian variants, and considering different encodings for the newline character.
38
39 However, the set of supported external formats depends on the size of the space of character codes. When ECL is built with Unicode support (the default option), it can represent all known characters from all known codepages, and thus all external formats are supported. However, when ECL is built with the restricted character set, it can only use one codepage (the one provided by the C library), with a few variants for the representation of end-of-line characters.
40
41 In ECL, an external format designator is defined recursively as either a symbol or a list of symbols. The grammar is as follows
42
43 @verbatim
44 external-format-designator :=
45 symbol |
46 ( {symbol}+ )
47 @end verbatim
48
49 and the table of known symbols is shown below. Note how some symbols (@code{:cr}, @code{:little-endian}, etc.) just modify other external formats.
50
51 @float Table, tab:stream-ext-formats
52 @caption{Stream external formats}
53 @multitable @columnfractions .4 .4 .2
54 @headitem Symbols @tab Codepage or encoding @tab Unicode required
55 @item @code{:cr} @tab @code{#\Newline} is @code{Carriage Return} @tab No
56 @item @code{:crlf} @tab @code{#\Newline} is @code{Carriage Return} followed by @code{Linefeed} @tab No
57 @item @code{:lf} @tab @code{#\Newline} is @code{Linefeed} @tab No
58 @item @code{:little-endian} @tab Modify UCS to use little-endian encoding. @tab No
59 @item @code{:big-endian} @tab Modify UCS to use big-endian encoding. @tab No
60 @item @code{:utf-8 :utf8} @tab Unicode UTF-8 @tab Yes
61 @item @code{:ucs-2 :ucs2 :utf-16 :utf16 :unicode} @tab UCS-2 encoding with BOM. Defaults to big-endian when writing or if no BOM is detected when reading. @tab Yes
62 @item @code{:ucs-2le :ucs2le :utf-16le} @tab UCS-2 with little-endian encoding @tab Yes
63 @item @code{:ucs-2be :ucs2be :utf-16be} @tab UCS-2 with big-endian encoding @tab Yes
64 @item @code{:ucs-4 :ucs4 :utf-32 :utf32} @tab UCS-4 encoding with BOM. Defaults to big-endian when writing or if no BOM is detected when reading. @tab Yes
65 @item @code{:ucs-4le :ucs4le :utf-32le} @tab UCS-4 with little-endian encoding @tab Yes
66 @item @code{:ucs-4be :ucs4be :utf-32be} @tab UCS-4 with big-endian encoding @tab Yes
67 @item @code{:iso-8859-1 :iso8859-1 :latin-1 :cp819 :ibm819} @tab Latin-1 encoding @tab Yes
68 @item @code{:iso-8859-2 :iso8859-2 :latin-2 :latin2} @tab Latin-2 encoding @tab Yes
69 @item @code{:iso-8859-3 :iso8859-3 :latin-3 :latin3} @tab Latin-3 encoding @tab Yes
70 @item @code{:iso-8859-4 :iso8859-4 :latin-4 :latin4} @tab Latin-4 encoding @tab Yes
71 @item @code{:iso-8859-5 :cyrillic} @tab Latin-5 encoding @tab Yes
72 @item @code{:iso-8859-6 :arabic :asmo-708 :ecma-114} @tab Latin-6 encoding @tab Yes
73 @item @code{:iso-8859-7 :greek8 :greek :ecma-118} @tab Greek encoding @tab Yes
74 @item @code{:iso-8859-8 :hebrew} @tab Hebrew encoding @tab Yes
75 @item @code{:iso-8859-9 :latin-5 :latin5} @tab Latin-5 encoding @tab Yes
76 @item @code{:iso-8859-10 :iso8859-10 :latin-6 :latin6} @tab Latin-6 encoding @tab Yes
77 @item @code{:iso-8859-13 :iso8859-13 :latin-7 :latin7} @tab Latin-7 encoding @tab Yes
78 @item @code{:iso-8859-14 :iso8859-14 :latin-8 :latin8} @tab Latin-8 encoding @tab Yes
79 @item @code{:iso-8859-15 :iso8859-15 :latin-9 :latin9} @tab Latin-7 encoding @tab Yes
80 @item @code{:dos-cp437 :ibm-437} @tab IBM CP 437 @tab Yes
81 @item @code{:dos-cp850 :ibm-850 :cp850} @tab Windows CP 850 @tab Yes
82 @item @code{:dos-cp852 :ibm-852} @tab IBM CP 852 @tab Yes
83 @item @code{:dos-cp855 :ibm-855} @tab IBM CP 855 @tab Yes
84 @item @code{:dos-cp860 :ibm-860} @tab IBM CP 860 @tab Yes
85 @item @code{:dos-cp861 :ibm-861} @tab IBM CP 861 @tab Yes
86 @item @code{:dos-cp862 :ibm-862 :cp862} @tab Windows CP 862 @tab Yes
87 @item @code{:dos-cp863 :ibm-863} @tab IBM CP 863 @tab Yes
88 @item @code{:dos-cp864 :ibm-864} @tab IBM CP 864 @tab Yes
89 @item @code{:dos-cp865 :ibm-865} @tab IBM CP 865 @tab Yes
90 @item @code{:dos-cp866 :ibm-866 :cp866} @tab Windows CP 866 @tab Yes
91 @item @code{:dos-cp869 :ibm-869} @tab IBM CP 869 @tab Yes
92 @item @code{:windows-cp932 :windows-932 :cp932} @tab Windows CP 932 @tab Yes
93 @item @code{:windows-cp936 :windows-936 :cp936} @tab Windows CP 936 @tab Yes
94 @item @code{:windows-cp949 :windows-949 :cp949} @tab Windows CP 949 @tab Yes
95 @item @code{:windows-cp950 :windows-950 :cp950} @tab Windows CP 950 @tab Yes
96 @item @code{:windows-cp1250 :windows-1250 :ms-ee} @tab Windows CP 1250 @tab Yes
97 @item @code{:windows-cp1251 :windows-1251 :ms-cyrl} @tab Windows CP 1251 @tab Yes
98 @item @code{:windows-cp1252 :windows-1252 :ms-ansi} @tab Windows CP 1252 @tab Yes
99 @item @code{:windows-cp1253 :windows-1253 :ms-greek} @tab Windows CP 1253 @tab Yes
100 @item @code{:windows-cp1254 :windows-1254 :ms-turk} @tab Windows CP 1254 @tab Yes
101 @item @code{:windows-cp1255 :windows-1255 :ms-hebr} @tab Windows CP 1255 @tab Yes
102 @item @code{:windows-cp1256 :windows-1256 :ms-arab} @tab Windows CP 1256 @tab Yes
103 @item @code{:windows-cp1257 :windows-1257 :winbaltrim} @tab Windows CP 1257 @tab Yes
104 @item @code{:windows-cp1258 :windows-1258} @tab Windows CP 1258 @tab Yes
105 @end multitable
106 @end float
107
108 @node Streams - Dictionary
109 @subsection Dictionary
110
111 @subsubsection File Stream Extensions
112
113 @lspdef open
114 @defun open filespec &key direction element-type if-exists if-does-not-exist external-format close-on-exec nonblock
115 Additional options for @clhs{f_open.htm,open} include:
116 @table @code
117 @item :close-on-exec
118 Child processes don't inherit a copy of this stream: new processes
119 created by @code{fork} and @code{exec} (for example by calling
120 @coderef{ext:run-program}) close the stream after calling @code{exec}.
121 Defaults to @code{t}.
122 @item :nonblock
123 Open fifos or device files in nonblocking mode. Defaults to @code{nil}.
124 @end table
125 These options are ignored on operating systems which do not support them.
126 @end defun
127
128 @lspdef ext:set-buffering-mode
129 @defun ext:set-buffering-mode stream mode
130 Control the buffering mode of a stream
131 @paragraph Synopsis
132 @table @var
133 @item stream
134 an ANSI stream
135 @item mode
136 one of @code{nil}, @code{:none}, @code{:line}, @code{:line-buffered}, @code{:full} or @code{:full-buffered}
137 @item returns
138 The supplied stream
139 @end table
140 @paragraph Description
141 If @var{mode} is @code{nil} or @code{:none}, @var{stream} will not be
142 buffered, if it is @code{:line} or @code{:line-buffered} resp.
143 @code{:full} or @code{:fully-buffered}, @var{stream} will be line resp.
144 fully buffered. If the stream does not support buffering, nothing will
145 happen.
146 @end defun
147
148 @lspdef ext:file-stream-fd
149 @defun ext:file-stream-fd file-stream
150 Return the POSIX file descriptor of @var{file-stream} as an integer
151 @end defun
152
153 @subsubsection Binary Stream Extensions
154
155 @lspdef ext:unread-byte
156 @defun ext:unread-byte stream byte
157 Similar to @code{unread-char} but for unreading the last element to the
158 input binary stream. See also @code{gray:stream-unread-byte}.
159 @end defun
160
161 @lspdef ext:peek-byte
162 @defun ext:peek-byte stream eof-value
163 Similar to @code{peek-char} but for peeking the next element from the
164 output binary stream. See also @code{gray:stream-peek-byte}.
165 @end defun
166
167 @subsubsection External Format Extensions
168
169 @lspdef ext:*default-external-format*
170 @defvar ext:*default-external-format*
171 Default external format to use for reading from streams, dealing with
172 filenames, etc. The default is to use utf-8 encoding if ECL is built
173 with Unicode support.
174 @end defvar
175
176 @lspdef ext:all-encodings
177 @defun ext:all-encodings
178 Return a list of all supported external formats
179 @end defun
180
181 @lspdef ext:character-coding-error
182 @deftp Condition ext:character-coding-error
183
184 Character coding error
185
186 @paragraph Class Precedence List
187 @coderef{ext:character-coding-error}, @code{error}, @code{serious-condition}, @code{condition}, @code{t}
188
189 @paragraph Methods
190 @lspdef ext:character-coding-error-external-format
191 @defun ext:character-coding-error-external-format condition
192 @table @var
193 @item returns
194 The external format of @var{condition}
195 @end table
196 @end defun
197
198 @paragraph Description
199 Superclass of @coderef{ext:character-encoding-error} and @coderef{ext:character-decoding-error}.
200 @end deftp
201
202 @lspdef ext:character-encoding-error
203 @deftp Condition ext:character-encoding-error
204
205 Character encoding error
206
207 @paragraph Class Precedence List
208 @coderef{ext:character-encoding-error}, @coderef{ext:character-coding-error}, @code{error}, @code{serious-condition}, @code{condition}, @code{t}
209
210 @paragraph Methods
211 @lspdef ext:character-encoding-error-code
212 @defun ext:character-encoding-error-code condition
213 @table @var
214 @item returns
215 The character code of the character, which can't be encoded
216 @end table
217 @end defun
218
219 @paragraph Description
220 Condition for characters, which can't be encoded with some external
221 format.
222 @end deftp
223
224
225 @lspdef ext:character-decoding-error
226 @deftp Condition ext:character-decoding-error
227
228 Character decoding error
229
230 @paragraph Class Precedence List
231 @coderef{ext:character-decoding-error}, @coderef{ext:character-coding-error}, @code{error}, @code{serious-condition}, @code{condition}, @code{t}
232
233 @paragraph Methods
234 @lspdef ext:character-decoding-error-octects
235 @defun ext:character-decoding-error-octects condition
236 @table @var
237 @item returns
238 A list of integers with the values of the @code{unsigned char}'s which
239 can't be decoded.
240 @end table
241 @end defun
242
243 @paragraph Description
244 Condition for characters, which can't be decoded with some external
245 format.
246 @end deftp
247
248 @lspdef ext:stream-encoding-error
249 @deftp Condition ext:stream-encoding-error
250
251 Stream encoding error
252
253 @paragraph Class Precedence List
254 @coderef{ext:stream-encoding-error}, @coderef{ext:character-encoding-error}, @coderef{ext:character-coding-error}, @code{stream-error}, @code{error}, @code{serious-condition}, @code{condition}, @code{t}
255
256 @paragraph Description
257 This condition is signaled when trying to write a character to a
258 stream, which can't be encoded with the streams external format.
259 @end deftp
260
261
262 @lspdef ext:stream-decoding-error
263 @deftp Condition ext:stream-decoding-error
264
265 Stream decoding error
266
267 @paragraph Class Precedence List
268 @coderef{ext:stream-decoding-error}, @coderef{ext:character-decoding-error}, @coderef{ext:character-coding-error}, @code{stream-error}, @code{error}, @code{serious-condition}, @code{condition}, @code{t}
269
270 @paragraph Description
271 This condition is signaled when trying to read a character from a
272 stream, which can't be decoded with the streams external format.
273 @end deftp
274
275 @lspdef ext:encoding-error
276 @defun ext:encoding-error stream external-format code
277 Signal a @coderef{ext:stream-encoding-error} with the given
278 @var{external-format} and @var{code}. Make a restart available so
279 that the error can be ignored or the character can be replaced with a
280 different one.
281 @end defun
282
283 @lspdef ext:decoding-error
284 @defun ext:decoding-error stream external-format octects
285 Signal a @coderef{ext:stream-decoding-error} with the given
286 @var{external-format} and @var{octets}. Make a restart available so
287 that the error can be ignored or the octets can be replaced with a
288 character.
289 @end defun
290
291 @subsubsection Sequence Streams
292
293 @lspdef ext:sequence-stream
294 @deftp {System Class} ext:sequence-stream
295
296 @paragraph Class Precedence List
297 @coderef{ext:sequence-stream}, @code{stream}, @code{t}
298
299 @paragraph Description
300 Sequence streams work similar to string streams for vectors. The
301 supplied vectors that the streams read from or write to must have a
302 byte sized element type, i.e. @code{(signed-byte 8)},
303 @code{(unsigned-byte 8)} or @code{base-char}.
304
305 The semantics depend on the vector element type and the external
306 format of the stream. If no external format is supplied and the
307 element type is an integer type, the stream is a binary stream and
308 accepts only integers of the same type as the element type of the
309 vector. Otherwise, the stream accepts both characters and integers and
310 converts them using the given external format. If the element type is
311 @code{base-char}, the elements of the vectors are treated as bytes.
312 This means that writing a character may use multiple elements of the
313 vector, whose @code{char-code}s will be equal to the values of the
314 bytes comprising the character in the given external format.
315 @end deftp
316
317 @lspdef ext:make-sequence-input-stream
318 @defun ext:make-sequence-input-stream vector &key (start 0) (end nil) (external-format nil)
319 Create a sequence input stream with the subsequence bounded by
320 @var{start} and @var{end} of the given vector.
321
322 When @var{end} is @code{nil}, then the end bound is the vector's length.
323 When the vector is adjusted the end bound is updated as well.
324 @end defun
325 @lspdef ext:make-sequence-output-stream
326 @defun ext:make-sequence-output-stream vector &key (external-format nil)
327 Create a sequence output stream.
328 @end defun
329
330 @exindex Using sequence streams
331 Example:
332
333 Using sequence streams to convert to a UTF8 encoded base string
334 @lisp
335 CL-USER> (defvar *output* (make-array 20 :element-type 'base-char :adjustable t :fill-pointer 0))
336 *OUTPUT*
337 CL-USER> (defvar *stream* (ext:make-sequence-output-stream *output* :external-format :utf-8))
338 *STREAM*
339 CL-USER> (write-string "Spätzle mit Soß'" *stream*)
340 "Spätzle mit Soß'"
341 CL-USER> *output*
342 "Spätzle mit SoÃ\237'"
343 @end lisp
344
345 @node Streams - C Reference
346 @subsection C Reference
347
348 @subsubsection ANSI dictionary
349 Common Lisp and C equivalence
350
351 @multitable @columnfractions .3 .7
352 @headitem Lisp symbol @tab C function
353 @item @clhs{f_broadc.htm,broadcast-stream-streams} @tab cl_object cl_broadcast_stream_streams(cl_object broadcast_stream)
354 @item @clhs{f_clear_.htm,clear-input} @tab cl_object cl_clear_input(cl_narg narg, ...)
355 @item @clhs{f_finish.htm,clear-output} @tab cl_object cl_clear_output(cl_narg narg, ...)
356 @item @clhs{f_close.htm,close} @tab cl_object cl_close(cl_narg narg, cl_object stream, ...)
357 @item @clhs{f_conc_1.htm,concatenated-stream-streams} @tab cl_object cl_concatenated_stream_streams(cl_object concatenated_stream)
358 @item @clhs{f_echo_s.htm,echo-stream-input-stream} @tab cl_object cl_echo_stream_input_stream(cl_object echo_stream)
359 @item @clhs{f_echo_s.htm,echo-stream-output-stream} @tab cl_object cl_echo_stream_output_stream(cl_object echo_stream)
360 @item @clhs{f_file_p.htm,file-length} @tab cl_object cl_file_position(cl_narg narg, cl_object file_stream, ...)
361 @item @clhs{f_file_s.htm,file-position} @tab cl_object cl_file_position(cl_object stream)
362 @item @clhs{f_finish.htm,file-string-length} @tab cl_object cl_file_string_length(cl_object stream, cl_object object)
363 @item @clhs{f_finish.htm,finish-output} @tab cl_object cl_finish_output(cl_narg narg, ...)
364 @item @clhs{f_finish.htm,force-output} @tab cl_object cl_force_output(cl_narg narg, ...)
365 @item @clhs{f_terpri.htm,fresh-line} @tab cl_object cl_fresh_line(cl_narg narg, ...)
366 @item @clhs{f_get_out.htm,get-output-stream-string} @tab cl_object cl_get_output_stream_string(cl_object string_output_stream)
367 @item @clhs{f_in_stm.htm,input-stream-p} @tab cl_object cl_input_stream_p(cl_object stream)
368 @item @clhs{f_intera.htm,interactive-stream-p} @tab cl_object cl_interactive_stream_p(cl_object stream)
369 @item @clhs{f_listen.htm,listen} @tab cl_object cl_listen(cl_narg narg, cl_object stream, ...)
370 @item @clhs{f_mk_bro.htm,make-broadcast-stream} @tab cl_object cl_make_broadcast_stream(cl_narg narg, ...)
371 @item @clhs{f_mk_con.htm,make-concatenated-stream} @tab cl_object cl_make_concatenated_stream(cl_narg narg, ....)
372 @item @clhs{f_mk_ech.htm,make-echo-stream} @tab cl_object cl_make_echo_stream(cl_object input, cl_object output)
373 @item @clhs{f_mk_s_1.htm,make-string-input-stream} @tab cl_object cl_make_string_input_stream(cl_narg narg, cl_object string, ...)
374 @item @clhs{f_mk_s_2.htm,make-string-output-stream} @tab cl_object cl_make_string_output_stream(cl_narg narg, ...)
375 @item @clhs{f_mk_two.htm,make-two-way-stream} @tab cl_object cl_make_two_way_stream(cl_object input, cl_object output)
376 @item @clhs{f_mk_syn.htm,make-synonym-stream} @tab cl_object cl_make_synonym_stream(cl_object symbol)
377 @item @clhs{f_open.htm,open} @tab cl_object cl_open(cl_narg narg, cl_object filespec, ...)
378 @item @clhs{f_open_s.htm,open-stream-p} @tab cl_object cl_open_stream_p(cl_object stream)
379 @item @clhs{f_in_stm.htm,output-stream-p} @tab cl_object cl_output_stream_p(cl_object stream)
380 @item @clhs{f_peek_c.htm,peek-char} @tab cl_object cl_peek_char(cl_narg narg, ...)
381 @item @clhs{f_rd_by.htm,read-byte} @tab cl_object cl_read_byte(cl_narg narg, cl_object stream, ...)
382 @item @clhs{f_rd_cha.htm,read-char} @tab cl_object cl_read_char(cl_narg narg, ...)
383 @item @clhs{f_rd_c_1.htm,read-char-no-hang} @tab cl_object cl_read_char_no_hang(cl_narg narg, ...)
384 @item @clhs{f_rd_lin.htm,read-line} @tab cl_object cl_read_line(cl_narg narg, ...)
385 @item @clhs{f_rd_seq.htm,read-sequence} @tab cl_object cl_read_sequence(cl_narg narg, cl_object sequence, cl_object stream, ...)
386 @item @clhs{f_stm_el.htm,stream-element-type} @tab cl_object cl_stream_element_type(cl_object stream)
387 @item @clhs{f_stm_er.htm,stream-error-stream} @tab [Only in Common Lisp]
388 @item @clhs{f_stm_ex.htm,stream-external-format} @tab cl_object cl_stream_external_format(cl_object stream)
389 @item @clhs{f_stm_ex.htm,(setf stream-external-format)} @tab cl_object si_stream_external_format_set(cl_object stream, cl_object format)
390 @item @clhs{f_stmp.htm,streamp} @tab cl_object cl_streamp(cl_object object)
391 @item @clhs{f_syn_st.htm,synonym-stream-symbol} @tab cl_object cl_synonym_stream_symbol(cl_object synonym_stream)
392 @item @clhs{f_terpri.htm,terpri} @tab cl_object cl_terpri(cl_narg narg, ...)
393 @item @clhs{f_two_wa.htm,two-way-stream-input-stream} @tab cl_object cl_two_way_stream_input_stream(cl_object two_way_stream)
394 @item @clhs{f_two_wa.htm,two-way-stream-output-stream} @tab cl_object cl_two_way_stream_output_stream(cl_object two_way_stream)
395 @item @clhs{f_unrd_c.htm,unread-char} @tab cl_object cl_unread_char(cl_narg narg, cl_object character, ...)
396 @item @clhs{f_wr_by.htm,write-byte} @tab cl_object cl_write_byte(cl_object byte, cl_object stream)
397 @item @clhs{f_wr_cha.htm,write-char} @tab cl_object cl_write_char(cl_narg narg, cl_object character, ...)
398 @item @clhs{f_wr_stg.htm,write-line} @tab cl_object cl_write_line(cl_narg narg, cl_object string, ...)
399 @item @clhs{f_wr_stg.htm,write-string} @tab cl_object cl_write_string(cl_narg narg, cl_object string, ...)
400 @item @clhs{f_wr_seq.htm,write-sequence} @tab cl_object cl_write_sequence(cl_narg narg, cl_object sequence, cl_object stream, ...)
401 @item @clhs{f_y_or_n.htm,y-or-n-p} @tab cl_object cl_y_or_n_p(cl_narg narg, ...)
402 @item @clhs{f_y_or_n.htm,yes-or-no-p} @tab cl_object cl_yes_or_no_p(cl_narg narg, ...)
403 @end multitable