7.2 Input/Output system extensions
Syntax:buffer-ref
buffer index element-type
buffer-ref
obtains the element with the specified index from a buffer defined in the buffered interface.
get-input-buffer
,get-input-buffer-no-hang
, orget-output-buffer
.
string-char
,character
, (unsigned-byte
n), or (signed-byte
n); generally, it is the same type that is specified to the Common Lisp functionopen
.
stream-element-type
on the stream.
get-input-buffer
, get-input-buffer-no-hang
, get-output-buffer
;stream-element-type
(in CLtL2)
Syntax:close-all-files
close-all-files
closes all open files.
extract-stream-handles Function
Syntax:extract-stream-handle
common-lisp-stream&optional
direction
Syntax:extract-stream-handles
common-lisp-stream
extract-stream-handle
returns the file handle that the specified Common Lisp stream uses for the specified input or output direction. The functionextract-stream-handles
returns the file handles that the given Common Lisp stream uses for input and output. A file handle is a UNIX file descriptor. For LCL/HP and LCL/RS6000, the file handle is an operating system stream identifier, which is equivalent to a UNIX file descriptor.
underlying-stream
to retrieve the stream whose file handle or handles are returned.
extract-stream-handle
, the direction specified when the stream was opened is used. It is an error to specify a direction that common-lisp-stream is not open for.
extract-stream-handles
always returns two values, the file handles used for input and output. For unidirectional streams, one of the returned values isnil
, which indicates that there is no associated file handle for that direction.
with make-lisp-stream
must have at least one file handle, so that callingextract-stream-handles
on such a stream will return at least one non-nil
value. Callingextract-stream-handles
on a stream created in some other way that does not have any file handles at all will cause an error.
;; First, define a function that invokes the UNIX OPEN library call. > (def-foreign-function (unix-open (:name "_open")) path flags mode) UNIX-OPEN> (load-foreign-libraries nil) NIL ;; Get a UNIX file descriptor on the terminal for output. > (setq fd (unix-open "/dev/tty" 1 0)) 3
;; Now create a Lisp stream which does output to this file handle. > (setq str (make-lisp-stream :output-handle fd)) #<Stream OSI-BUFFERED-STREAM E6976B>
;; Get the handles from the stream. There is no input handle, and ;; the output handle is the value of the variable FD. > (extract-stream-handles str) NIL 3
;; You can get the output handle this way as well. > (extract-stream-handle str :output) 3
;; Write some text to the stream. > (format str "text~%") NIL
;; Force the output to actually be printed. > (force-output str) text NIL
;; Close the stream when done. > (close str) NIL
make-lisp-stream
, underlying-stream
Syntax:fast-read-char
stream eof-error-p eof-value
Syntax:fast-unread-char
char stream
Syntax:fast-write-char
char stream
Syntax:fast-write-string
string stream&key :start :end
Syntax:fast-read-byte
stream type eof-error-p eof-value
Syntax:fast-write-byte
byte stream type
read-char
,unread-char
,write-char
,write-string
, read-byte
,write-byte
).
underlying-stream
returns the lowest level stream of a composite stream.
:element-type
option for the Common Lisp functionopen
is acceptable. You do not need to quote this argument.
*terminal-io*
is a composite stream with separate input and output directions. Thus, it does not meet the conditions for fast I/O.
;; Write the numbers from 0 to 2 to an 8-bit binary file. > (with-open-file (f "temp.out" :direction :output :element-type '(unsigned-byte 8)) (dotimes (i 3) ;; Note element-type here. (fast-write-byte i f (unsigned-byte 8)))) NILget-input-buffer Function;; Read a file, and type out the characters until EOF is reached. > (with-open-file (f "temp.out") (loop (let ((char (fast-read-char f nil))) (unless char (return nil)) (write-char char)))) NIL
get-input-buffer-no-hang Function
get-input-buffer &optional
stream eof-error-p eof-value
get-input-buffer-no-hang &optional
stream eof-error-p eof-value
get-output-buffer &optional
stream
get-input-buffer
,get-input-buffer-no-hang
, andget-output-buffer
give you access to an I/O buffer that is used by a stream so that you can read or modify the buffer directly. They return the following values:get-input-buffer-no-hang
returnsnil
and no other values.*standard-input*
is used for the functions get-input-buffer
andget-input-buffer-no-hang
; the stream that is the value of*standard-output*
is used forget-output-buffer
. If you specify the valuet
for the stream argument, the stream that is the value of*terminal-io*
is used.
nil
value; this value is the default. If an end-of-file occurs and the value of eof-error-p isnil
, no error is signaled and eof-value is returned. The default value of eof-value isnil
.
buffer-ref
. To add elements to the buffer, use the Common Lisp macrosetf
with buffer-ref
.
return-input-buffer
orreturn-output-buffer
to restore the buffer to the stream in most instances.
get-input-buffer
if you want to be informed of an end-of-file condition.get-output-buffer
if you want to write into an empty buffer when an end-of-file condition occurs.return-output-buffer
if you want to write the data you have modified in the buffer into a file.return-input-buffer
if you have not modified the buffer. However, do not usereturn-input-buffer
to abort modifications you have made to the buffer.close
on the stream and specify a non-nil
value for the:abort
keyword argument.
get-output-buffer
returns a buffer that contains only data read from the stream or uninitialized data that will be appended to the end of the stream. You cannot extend a bidirectional stream by adding data to the end of the buffer; you must return the buffer and obtain an empty buffer to add data to the stream.
buffer-ref
, return-input-buffer
, return-output-buffer
Syntax:*ignore-extra-right-parens*
*ignore-extra-right-parens*
controls the action of the reader when excess right parentheses are encountered in the input stream. The action taken depends on the value of the variable as follows:t
, excess right parentheses in the input stream are ignored.:just-warn
, a warning message is generated.nil
, a continuable error is signaled.*ignore-extra-right-parens*
is:just-warn
.
> *ignore-extra-right-parens* :JUST-WARN > (read-from-string ")1") ;;; Warning: Ignoring an unmatched right parenthesis. 1 2make-lisp-stream Function> (let((*ignore-extra-right-parens* t)) (declare (special *ignore-extra-right-parens*)) (read-from-string ")1")) 1 2
make-lisp-stream
&key :input-handle :output-handle :io-handle :element-type :auto-force :positionable :desired-buffer-size
make-lisp-stream
constructs a Common Lisp stream from file handles. A file handle is a UNIX file descriptor. For LCL/HP and LCL/RS6000, the file handle is an operating system stream identifier, which is equivalent to a UNIX file descriptor.
:input-handle
:output-handle
:io-handle
:direction :io
.
:element-type
:element-type
option for the Common Lisp functionopen
is acceptable. The default value is'string-char
.
:auto-force
nil
value, the stream forces output after each output operation. The default value isnil
.
:positionable
nil
value, you can use the Common Lisp functionfile-position
to change the pointer position in the stream; in this case, the Common Lisp functionsclear-input
andclear-output
have no effect on the stream that is created. Disk files are generally positionable, but terminal and network connections generally are not.
nil
, you cannot set the position of the file pointer withfile-position
. If invoked, clear-input
andclear-output
attempt to clear input and output on the stream respectively. The default value isnil
.
:desired-buffer-size
:input-handle
and:output-handle
options. You should use this method for creating bidirectional streams if the file handles are associated with a socket or a similar device. If you have a device such as a terminal or network connection that has a single handle for both input and output, specify that handle as the value of both the:input-handle
and:output-handle
keyword arguments.
;; First, define a function that invokes the UNIX OPEN library ;; call. > (def-foreign-function (unix-open (:name "_open")) path flags mode) UNIX-OPEN;; Get a UNIX file descriptor on the terminal for output. > (setq fd (unix-open "/dev/tty" 1 0)) 3
;; Now create a Lisp stream that does output to this file handle. > (setq str (make-lisp-stream :output-handle fd)) #<Stream OSI-BUFFERED-STREAM E6976B>extract-stream-handle
extract-stream-handle
, extract-stream-handles
,with-buffered-terminal-output
Syntax:*print-structure*
*print-structure*
controls the printing of structures.
*print-structure*
is non-nil
, structures are printed in detail, using the#S
syntax. If*print-structure*
isnil
, they are printed with the abbreviated#<...>
syntax. The initial value of*print-structure*
is t
.
> *print-structure* Tread-array Function> (defstruct family mom dad brother sister dog) FAMILY
> (setq jones (make-family :mom 'simone :dad 'sam :brother 'basket-ball :sister 'sally :dog 'bowser)) #S(FAMILY MOM SIMONE DAD SAM BROTHER BASKET-BALL SISTER SALLY DOG BOWSER)
> (let ((*print-structure* nil)) (print jones) (values)) #<Structure FAMILY 428F3B>
Syntax:read-array
stream array&optional
start end eof-error-p eof-value
Syntax:write-array
stream array&optional
start end
read-array
andwrite-array
read and write respectively the contents of the specified one-dimensional array into the specified stream.
:element-type
argument to the Common Lisp functionopen
.
read-array
before an element can be read, an error is signaled if eof-error-p has a non-nil
value; this value is the default. If an end-of-file occurs and the value of eof-error-p isnil
, no error is signaled and eof-value is returned. The default value of eof-value isnil
.
Syntax:return-input-buffer
&optional
stream limit-reached
Syntax:return-output-buffer
&optional
stream limit-reached
return-input-buffer
andreturn-output-buffer
return buffers to the specified stream.
get-input-buffer
,get-input-buffer-no-hang
, orget-output-buffer
.
*standard-input*
is used for the function return-input-buffer
; the stream that is the value of*standard-output*
is used forreturn-output-buffer
. If you specify the valuet
for the stream argument, the stream that is the value of*terminal-io*
is used.
get-input-buffer
if you want to be informed of an end-of-file condition.get-output-buffer
if you want to write into an empty buffer when an end-of-file condition occurs.return-output-buffer
if you wish to write the data you have modified in the buffer into a file.return-input-buffer
if you have not modified the buffer. However, do not usereturn-input-buffer
to abort modifications you have made to the buffer.get-input-buffer
, get-input-buffer-no-hang
, get-input-buffer
, get-output-buffer
underlying-stream
stream&optional
direction recurse exact-same-effect
underlying-stream
returns the lowest level stream from which the specified composite stream performs I/O operations in the specified direction.
underlying-stream
returns that stream.
:input
,:output
, or:io
. If you do not specify the direction argument or if you specify the valuenil
, the lowest level stream that receives all I/O operations is returned.
nil
, there will be only one level of indirection. Having only one level of indirection is useful if, for example, you need to get the stream from which a two-way stream is made, even if that component stream is composite. The default value of recurse ist
.
nil
value, you can perform I/O operations in the given direction to the stream that is returned and be assured of getting the exact same effect as if you had performed the operations to the higher level stream. For example, if stream is an echo stream and direction is:input
, setting exact-same-effect returns the echo stream because performing an input operation from the input stream directly would not produce echoing.
underlying-stream
depends on the stream type:echo
,two-way
:input
and exact-same-effect isnil
, or direction is:output
.underlying-stream
follows the stream in that direction and returns the lowest level stream.
:input
; exact-same-effect ist
.underlying-stream
behaves as if the argument is a simple stream.
synonym
broadcast
,concatenated
make-broadcast-stream
,make-concatenated-stream
,make-echo-stream
,make-synonym-stream
,make-two-way-stream
(in CLtL2)
Syntax:with-buffered-terminal-output
stream {form}*
with-buffered-terminal-output
suppresses automatic forcing of output after each output operation. The output to the stream is sent to the terminal when the last output operation is complete.
make-lisp-stream
using the:auto-force
keyword option.
print
andformat
.
make-lisp-stream
Generated with Harlequin WebMaker