11 Memory files

The library(memfile) provides an alternative to temporary files, intended for temporary buffering of data. Memory files in general are faster than temporary files and do not suffer from security riscs or naming conflicts associated with temporary-file management. They do assume proper memory management by the hosting OS and cannot be used to pass data to external processes using a file-name.

There is no limit to the number of memory streams, nor the size of them. However, memory-streams cannot have multiple streams at the same time (i.e. cannot be opened for reading and writing at the same time).

These predicates are first of all intended for building higher-level primitives. See also sformat/3, atom_to_term/3, term_to_atom/2 and the XPCE primitive pce_open/3.

new_memory_file(-Handle)
Create a new memory file and return a unique opaque handle to it.
free_memory_file(+Handle)
Discard the memory file and its contents. If the file is open it is first closed.
open_memory_file(+Handle, +Mode, -Stream)
Open the memory-file. Mode is currently one of read or write. The resulting Stream must be closed using close/1.
open_memory_file(+Handle, +Mode, -Stream, +Options)
Open a memory-file as open_memory_file/3. Options:
encoding(+Encoding)
Set the encoding for a memory file and the created stream. Encoding names are the same as used with open/4. By default, memoryfiles represent UTF-8 streams, making them capable of storing arbitrary Unicode text. In practice the only alternative is octet, turning the memoryfile into binary mode. Please study SWI-Prolog Unicode and encoding issues before using this option.
free_on_close(+Bool)
If true (default false and the memory file is opened for reading, discard the file (see free_memory_file/1) if the input is closed. This is used to realise open_chars_stream/2 in library(charsio).
size_memory_file(+Handle, -Bytes)
Return the content-length of the memory-file it Bytes. The file should be closed and contain data.
atom_to_memory_file(+Atom, -Handle)
Turn an atom into a read-only memory-file containing the (shared) characters of the atom. Opening this memory-file in mode write yields a permission error.
memory_file_to_atom(+Handle, -Atom)
Return the content of the memory-file in Atom.
memory_file_to_atom(+Handle, -Atom, +Encoding)
Return the content of the memory-file in Atom, pretending the data is in the given Encoding. This can be used to convert from one encoding into another, typically from/to bytes. For example, if we must convert a set of bytes that contain text in UTF-8, open the memory file as octet stream, fill it, and get the result using Encoding is utf8.
memory_file_to_codes(+Handle, -Codes)
Return the content of the memory-file as a list of character-codes in Codes.
memory_file_to_codes(+Handle, -Codes, +Encoding)
Return the content of the memory-file as a list of character-codes in Codes, pretending the data is in the given Encoding.