www.delorie.com/gnu/docs/kawa/kawa_49.html   search  
 
Buy GNU books!


Kawa, the Java-based Scheme system

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.2 Ports

Function: current-error-port
Return the port to which errors and warnings should be sent (the standard error in Unix and C terminology).

Function: read-line [port [handle-newline]]
Reads a line of input from port. The handle-newline parameter determines what is done with terminating end-of-line delimiter. The default, 'trim, ignores the delimiter; 'peek leaves the delimiter in the input stream; 'concat appends the delimiter to the returned value; and 'split returns the delimiter as a second value. You can use the last three options to tell if the string was terminated by end-or-line or by end-of-file.

Function: open-input-string string
Takes a string and returns an input port that delivers characters from the string. The port can be closed by close-input-port, though its storage will be reclaimed by the garbage collector if it becomes inaccessible.

 
(define p
  (open-input-string "(a . (b c . ())) 34"))

(input-port? p)                 -->  #t
(read p)                        -->  (a b c)
(read p)                        -->  34
(eof-object? (peek-char p))     -->  #t

Function: open-output-string
Returns an output port that will accumulate characters for retrieval by get-output-string. The port can be closed by the procedure close-output-port, though its storage will be reclaimed by the garbage collector if it becomes inaccessible.
 
(let ((q (open-output-string))
  (x '(a b c)))
    (write (car x) q)
    (write (cdr x) q)
    (get-output-string q))        -->  "a(b c)"

Function: get-output-string output-port
Given an output port created by open-output-string, returns a string consisting of the characters that have been output to the port so far.

Function: call-with-input-string string proc
Create an input port that gets its data from string, call proc with that port as its one argument, and return the result from the call of proc

Function: call-with-output-string proc
Create an output port that writes its data to a string, and call proc with that port as its one argument. Return a string consisting of the data written to the port.

Function: force-output [port]
Forces any pending output on port to be delivered to the output device and returns an unspecified value. If the port argument is omitted it defaults to the value returned by (current-output-port).

An interactive input port has a prompt procedure associated with it. The prompt procedure is called before a new line is read. It is passed the port as an argument, and returns a string, which gets printed as a prompt.

Function: input-port-prompter port
Get the prompt procedure associated with port.

Function: set-input-port-prompter! port prompter
Set the prompt procedure associated with port to prompter, which must be a one-argument procedure taking an input port, and returning a string.

Function: default-prompter port
The default prompt procedure. It returns "#|kawa:L|# ", where L is the current line number of port. When reading a continuation line, the result is "#|C---:L|# ", where C is the character returned by (input-port-read-state port). The prompt has the form of a comment to make it easier to cut-and-paste.

Function: port-column input-port
Function: port-line input-port
Return the current column number or line number of input-port, using the current input port if none is specified. If the number is unknown, the result is #f. Otherwise, the result is a 0-origin integer - i.e. the first character of the first line is line 0, column 0. (However, when you display a file position, for example in an error message, we recommend you add 1 to get 1-origin integers. This is because lines and column numbers traditionally start with 1, and that is what non-programmers will find most natural.)

Function: set-port-line! port line
Set (0-origin) line number of the current line of port to num.

Function: input-port-line-number port
Get the line number of the current line of port, which must be a (non-binary) input port. The initial line is line 1. Deprecated; replaced by (+ 1 (port-line port)).

Function: set-input-port-line-number! port num
Set line number of the current line of port to num. Deprecated; replaced by (set-port-line! port (- num 1)).

Function: input-port-column-number port
Get the column number of the current line of port, which must be a (non-binary) input port. The initial column is column 1. Deprecated; replaced by (+ 1 (port-column port)).

Function: input-port-read-state port
Returns a character indicating the current read state of the port. Returns #\Return if not current doing a read, #\" if reading a string; #\| if reading a comment; #\( if inside a list; and #\Space when otherwise in a read. The result is intended for use by prompt prcedures, and is not necessarily correct except when reading a new-line.

Variable: symbol-read-case
A symbol that controls how read handles letters when reading a symbol. If the first letter is `U', then letters in symbols are upper-cased. If the first letter is `D' or `L', then letters in symbols are down-cased. If the first letter is `I', then the case of letters in symbols is inverted. Otherwise (the default), the letter is not changed. (Letters following a `\' are always unchanged.)

Variable: port-char-encoding
Controls how bytes in external files are converted to/from internal Unicode characters. Can be either a symbol or a boolean. If port-char-encoding is #f, the file is assumed to be a binary file and no conversion is done. Otherwise, the file is a text file. The default is #t, which uses a locale-dependent conversion. If port-char-encoding is a symbol, it must be the name of a character encoding known to Java. For all text files (that is if port-char-encoding is not #f), on input a #\Return character or a #\Return followed by #\Newline are converted into plain #\Newline.

This variable is checked when the file is opened; not when actually reading or writing. Here is an example of how you can safely change the encoding temporarily:
 
(define (open-binary-input-file name)
  (fluid-let ((port-char-encoding #f)) (open-input-file name)))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003