Mode memory

This module provides non-random access memory streams. A new read memory stream can be created via the predicate memory_read/3. A new write memory stream can be created via the predicate memory_write/2. The content of write memory stream can be retrieved via the predicate memory_get/2.

Example:

?- memory_write([], S), write(S, foo), memory_get(S, atom(A)).
S = 0r1479d830,
A = foo

?- memory_read(atom('foo.\n'), [], S), read(S, T).
S = 0r28100c13,
T = foo

The current implementation is limited in that the streams cannot be created with the reposition property. It is therefore not possibly to use the methods set_stream_position/2 or set_stream_length/2. Otherwise all the byte, char, term and stream operations can be applied as if they were file or web streams.

The following memory predicates are provided:

memory_read(T, O, S):
The predicate succeeds with a new read memory stream S for the initial data T and the open options O. The predicate recognizes the following open options:

type(T): [ISO]
T is the type (text or binary), default value is text.

The predicate recognizes the following data formats:

atom(A):
The atom A is the text content of the stream.
bytes(L):
The byte list L is the binary content of the stream

memory_write(O, S):
The predicate succeeds with a new write memory stream S for the open options O. The predicate recognizes the same open options as the predicate memory_read/3.
memory_get(S, T):
The predicate succeeds with the current data T of the write stream S. The predicate recognizes the same data formats as the predicate memory_read/3.

Comments