~ chicken-core (master) /manual/Module (chicken port)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken port)
  5
  6This module contains various extended port definitions.
  7
  8All errors related to failing port operations will signal a condition
  9of kind {{exn}}.
 10
 11* New in CHICKEN 5.4.0: Errors caused by underlying C calls that
 12  change errno will produce a condition object with an {{errno}}
 13  property, which can be accessed with
 14  {{(get-condition-property <the-condition-object> 'exn 'errno)}}.
 15
 16=== Port attributes
 17
 18==== port-encoding
 19
 20<procedure>(port-encoding PORT)</procedure>
 21
 22Returns the encoding used for reading and writing data from and to
 23the given port. Encoding is currently one of the symbols {{utf-8}},
 24{{latin-1}} or {{binary}}. Note that the encoding can be changed by using
 25the (SRFI-17) setter procedure for this operation:
 26
 27{{(set! (port-encoding PORT) ENCODING)}}
 28
 29==== port-name
 30
 31<procedure>(port-name [PORT])</procedure>
 32
 33Fetch filename from {{PORT}}. This returns the filename that was used to open
 34this file. Returns a special tag string, enclosed into parentheses for
 35non-file ports. {{PORT}} defaults to the value of {{(current-input-port)}}.
 36
 37Note that the encoding can be changed by using
 38the (SRFI-17) setter procedure for this operation:
 39
 40{{(set! (port-name PORT) STRING)}}
 41
 42
 43==== port-position
 44
 45<procedure>(port-position [PORT])</procedure>
 46
 47Returns the current position of {{PORT}} as two values: row and column number.
 48If the port does not support such an operation an error is signaled. This
 49procedure is currently only available for input ports. {{PORT}} defaults to the
 50value of {{(current-input-port)}}.
 51
 52
 53=== Setting the file buffering mode
 54
 55==== set-buffering-mode!
 56
 57<procedure>(set-buffering-mode! PORT MODE [BUFSIZE])</procedure>
 58
 59Sets the buffering-mode for the file associated with {{PORT}} to
 60{{MODE}}, which should be one of the keywords {{#:full}},
 61{{#:line}} or {{#:none}}. If {{BUFSIZE}} is specified it
 62determines the size of the buffer to be used (if any).
 63
 64This procedure doesn't work on custom ports, such as those created with {{make-input-port}} or {{make-output-port}}.
 65
 66=== Terminal ports
 67
 68==== terminal-name
 69
 70<procedure>(terminal-name PORT)</procedure>
 71
 72Returns the name of the terminal that is connected to {{PORT}}.
 73
 74On Windows, this procedure always raises an exception.
 75
 76==== terminal-port?
 77
 78<procedure>(terminal-port? PORT)</procedure>
 79
 80Returns {{#t}} if {{PORT}} is connected to a terminal and
 81{{#f}} otherwise.
 82
 83
 84==== terminal-size
 85
 86<procedure>(terminal-size PORT)</procedure>
 87
 88Returns two values, the number of rows and columns of the terminal
 89that is connected to {{PORT}} or {{0}}, {{0}} if the terminal size can
 90not be obtained.
 91
 92On Windows, this procedure always raises an exception.
 93
 94
 95=== Input/output port extensions
 96
 97==== with-output-to-port
 98
 99<procedure>(with-output-to-port PORT THUNK)</procedure>
100
101Call procedure {{THUNK}} with the current output-port temporarily
102bound to {{PORT}}.
103
104==== make-input-port
105==== make-binary-input-port
106
107<procedure>(make-input-port READ-CHAR CHAR-READY? CLOSE #!key peek-char read-bytevector read-line)</procedure>
108<procedure>(make-binary-input-port READ-U8 U8-READY? CLOSE #!key peek-u8 read-bytevector)</procedure>
109
110Returns a custom input port. Common operations on this port are
111handled by the given parameters, which should be procedures of no
112arguments.  The following arguments are all different kinds of reader
113procedures:
114
115* {{READ-CHAR}} is the most fundamental reader, and must always be
116present.  It is a thunk which is called when the next character is
117to be read and it should return a character or {{#!eof}}.
118* {{READ-U8}} similar to {{READ-CHAR}} but reads and returns a byte.
119* {{CHAR-READY?}}/{{U8-READY?}} are thunks which are called when {{char-ready?}}
120or {{u8-ready?}} are called on this port and should return {{#t}} or {{#f}}.
121* {{CLOSE}} is a thunk which is called when the port is closed.
122* {{peek-char}}/{{peek-u8}} are thunks which are called when {{peek-char}} 
123or {{peek-u8}} are
124called on this port and should return a character/byte or {{#!eof}}. If
125not provided or {{#f}}, {{READ-CHAR}}/{{READ-U8}} will be used instead and the
126created port object handles peeking automatically (by calling the reader procedure
127and buffering the character).
128* {{read-bytevector}} is called when {{read-bytevector}} or {{read-string!}} is called (or the
129higher-level non-mutating {{read-string}} and {{read-bytevector}}).  It will be invoked with 4
130arguments: the port created by {{make-input-port}}, the number of
131bytes to read, a bytevector to read into (which may be
132assumed to be big enough to hold the data) and the offset into the
133buffer at which to put the data to read.  It should return the number
134of bytes that have successfully been read, which should always be
135equal to the requested bytes unless EOF was hit, in which case it can
136be less.  If this procedure is not provided or {{#f}}, the buffer will
137be filled by repeated reads to {{READ-CHAR}}.
138* {{READ-LINE}} is called when {{read-line}} is called.  It will be
139invoked with two arguments: the port created by {{make-input-port}}
140and the maximum number of characters to read (or {{#f}}).  If this
141procedure is not provided or {{#f}}, the buffer will be filled by
142repeated reads to {{READ-CHAR}}.
143
144All the optional procedures except for {{PEEK-CHAR}} are responsible
145for updating the port's position, which currently can only be done via
146low-level slot accessors like {{##sys#setslot}}; slot 4 is the row
147number (ie, the line) and slot 5 is the column number (ie, the
148character on the line).  If the port's positions are not updated,
149{{port-position}} won't work.
150
151Note that reading binary input from a custom non-binary input port is only possible
152when the {{read-bytevector}} operation is given, as byte-input can
153currently not ben synthesized from character-input operations.
154
155
156==== make-output-port
157==== make-binary-output-port
158
159<procedure>(make-output-port WRITE CLOSE #!key force-output)</procedure>
160<procedure>(make-binary-output-port WRITE CLOSE #!key force-output write-bytevector)</procedure>
161
162Returns a custom output port. Common operations on this port are handled
163by the given parameters, which should be procedures.  {{WRITE}} is
164called when output is sent to the port and receives a single argument,
165a string (or bytevector for binary ports).  {{CLOSE}} is called when the port is closed and should
166be a procedure of no arguments. {{force-output}} (if provided) is called
167for flushing the output port. The optional {{write-bytevector}} allows
168more optimized writing of partial buffers and takes 3 arguments: a
169bytevector, a starting position and an endong position.
170
171
172==== with-error-output-to-port
173
174<procedure>(with-error-output-to-port PORT THUNK)</procedure>
175
176Call procedure {{THUNK}} with the current error output-port
177temporarily bound to {{PORT}}.
178
179
180==== with-input-from-port
181
182<procedure>(with-input-from-port PORT THUNK)</procedure>
183
184Call procedure {{THUNK}} with the current input-port temporarily
185bound to {{PORT}}.
186
187
188=== String-port extensions
189
190==== call-with-input-string
191
192<procedure>(call-with-input-string STRING PROC)</procedure>
193
194Calls the procedure {{PROC}} with a single argument that is a
195string-input-port with the contents of {{STRING}}.
196
197
198==== call-with-output-string
199
200<procedure>(call-with-output-string PROC)</procedure>
201
202Calls the procedure {{PROC}} with a single argument that is a
203string-output-port.  Returns the accumulated output-string.
204
205
206==== with-input-from-string
207
208<procedure>(with-input-from-string STRING THUNK)</procedure>
209
210Call procedure {{THUNK}} with the current input-port temporarily
211bound to an input-string-port with the contents of {{STRING}}.
212
213
214==== with-output-to-string
215
216<procedure>(with-output-to-string THUNK)</procedure>
217
218Call procedure {{THUNK}} with the current output-port temporarily
219bound to a string-output-port and return the accumulated output string.
220
221==== with-error-output-to-string
222
223<procedure>(with-error-output-to-string THUNK)</procedure>
224
225Call procedure {{THUNK}} with the current error output-port
226temporarily bound to a string-output-port and return the accumulated
227output string.
228
229
230=== Port iterators
231
232==== port-for-each
233
234<procedure>(port-for-each FN THUNK)</procedure>
235
236Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, discarding the results.
237
238==== port-map
239
240<procedure>(port-map FN THUNK)</procedure>
241
242Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}} (typically {{read}}) until it returns {{#!eof}}, returning a list of the collected results.
243
244==== port-fold
245
246<procedure>(port-fold FN ACC THUNK)</procedure>
247
248Apply {{FN}} to successive results of calling the zero argument procedure {{THUNK}}, (typically {{read}}) passing the {{ACC}} value as the second argument. The {{FN}} result becomes the new {{ACC}} value. When {{THUNK}} returns {{#!eof}}, the last {{FN}} result is returned.
249
250==== copy-port
251
252<procedure>(copy-port FROM TO [READ [WRITE]])</procedure>
253
254Reads all remaining data from port {{FROM}} using the reader procedure
255{{READ}} and writes it to port {{TO}} using the writer procedure
256{{WRITE}}. {{READ}} defaults to {{read-char}} and {{WRITE}} to
257{{write-char}}. Note that this procedure does not check {{FROM}} and
258{{TO}} for being ports, so the reader and writer procedures may
259perform arbitrary operations as long as they can be invoked
260as {{(READ FROM)}} and {{(WRITE X TO)}}, respectively.
261{{copy-port}} returns an undefined value.
262
263{{copy-port}} was introduced in CHICKEN 4.6.0.
264
265=== Funky ports
266
267==== make-bidirectional-port
268
269<procedure>(make-bidirectional-port INPUT-PORT OUTPUT-PORT)</procedure>
270
271Returns a joint input/output port that proxies port operations to the
272given {{INPUT-PORT}} and {{OUTPUT-PORT}}, respectively. This port
273satisfies both {{input-port?}} and {{output-port?}}, and its two
274directions may be closed independently.
275
276==== make-broadcast-port
277
278<procedure>(make-broadcast-port PORT ...)</procedure>
279
280Returns a custom output port that emits everything written into it to
281the ports given as {{PORT ...}}. Closing the broadcast port does not close
282any of the argument ports.
283
284==== make-concatenated-port
285
286<procedure>(make-concatenated-port PORT1 PORT2 ...)</procedure>
287
288Returns a custom input port that reads its input from {{PORT1}}, until it
289is empty, then from {{PORT2}} and so on. Closing the concatenated port
290does not close any of the argument ports.
291
292
293---
294Previous: [[Module (chicken plist)]]
295
296Next: [[Module (chicken pretty-print)]]
Trap