Warning: This is the manual of the legacy Guile 2.0 series. You may want to read the manual of the current stable series instead.

Next: , Previous: , Up: Input and Output   [Contents][Index]


6.14.6 Line Oriented and Delimited Text

The delimited-I/O module can be accessed with:

(use-modules (ice-9 rdelim))

It can be used to read or write lines of text, or read text delimited by a specified set of characters. It’s similar to the (scsh rdelim) module from guile-scsh, but does not use multiple values or character sets and has an extra procedure write-line.

Scheme Procedure: read-line [port] [handle-delim]

Return a line of text from port if specified, otherwise from the value returned by (current-input-port). Under Unix, a line of text is terminated by the first end-of-line character or by end-of-file.

If handle-delim is specified, it should be one of the following symbols:

trim

Discard the terminating delimiter. This is the default, but it will be impossible to tell whether the read terminated with a delimiter or end-of-file.

concat

Append the terminating delimiter (if any) to the returned string.

peek

Push the terminating delimiter (if any) back on to the port.

split

Return a pair containing the string read from the port and the terminating delimiter or end-of-file object.

Like read-char, this procedure can throw to decoding-error (see read-char).

Scheme Procedure: read-line! buf [port]

Read a line of text into the supplied string buf and return the number of characters added to buf. If buf is filled, then #f is returned. Read from port if specified, otherwise from the value returned by (current-input-port).

Scheme Procedure: read-delimited delims [port] [handle-delim]

Read text until one of the characters in the string delims is found or end-of-file is reached. Read from port if supplied, otherwise from the value returned by (current-input-port). handle-delim takes the same values as described for read-line.

Scheme Procedure: read-delimited! delims buf [port] [handle-delim] [start] [end]

Read text into the supplied string buf.

If a delimiter was found, return the number of characters written, except if handle-delim is split, in which case the return value is a pair, as noted above.

As a special case, if port was already at end-of-stream, the EOF object is returned. Also, if no characters were written because the buffer was full, #f is returned.

It’s something of a wacky interface, to be honest.

Scheme Procedure: write-line obj [port]
C Function: scm_write_line (obj, port)

Display obj and a newline character to port. If port is not specified, (current-output-port) is used. This function is equivalent to:

(display obj [port])
(newline [port])

In the past, Guile did not have a procedure that would just read out all of the characters from a port. As a workaround, many people just called read-delimited with no delimiters, knowing that would produce the behavior they wanted. This prompted Guile developers to add some routines that would read all characters from a port. So it is that (ice-9 rdelim) is also the home for procedures that can reading undelimited text:

Scheme Procedure: read-string [port] [count]

Read all of the characters out of port and return them as a string. If the count is present, treat it as a limit to the number of characters to read.

By default, read from the current input port, with no size limit on the result. This procedure always returns a string, even if no characters were read.

Scheme Procedure: read-string! buf [port] [start] [end]

Fill buf with characters read from port, defaulting to the current input port. Return the number of characters read.

If start or end are specified, store data only into the substring of str bounded by start and end (which default to the beginning and end of the string, respectively).

Some of the aforementioned I/O functions rely on the following C primitives. These will mainly be of interest to people hacking Guile internals.

Scheme Procedure: %read-delimited! delims str gobble [port [start [end]]]
C Function: scm_read_delimited_x (delims, str, gobble, port, start, end)

Read characters from port into str until one of the characters in the delims string is encountered. If gobble is true, discard the delimiter character; otherwise, leave it in the input stream for the next read. If port is not specified, use the value of (current-input-port). If start or end are specified, store data only into the substring of str bounded by start and end (which default to the beginning and end of the string, respectively).

Return a pair consisting of the delimiter that terminated the string and the number of characters read. If reading stopped at the end of file, the delimiter returned is the eof-object; if the string was filled without encountering a delimiter, this value is #f.

Scheme Procedure: %read-line [port]
C Function: scm_read_line (port)

Read a newline-terminated line from port, allocating storage as necessary. The newline terminator (if any) is removed from the string, and a pair consisting of the line and its delimiter is returned. The delimiter may be either a newline or the eof-object; if %read-line is called at the end of file, it returns the pair (#<eof> . #<eof>).


Next: , Previous: , Up: Input and Output   [Contents][Index]