View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2016, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(fastrw,
   36          [ fast_read/1,                % -Term
   37            fast_write/1,               % +Term
   38            fast_read/2,                % +Stream, -Term
   39            fast_write/2,               % +Stream, +Term
   40            fast_write_to_string/3      % +Term, -String, ?Tail
   41          ]).   42:- use_module(library(lists)).

Fast reading and writing of terms

This library provides the SICStus and Ciao library(fastrw) interface. The idea behind this library is to design a fast serialization for Prolog terms. Ideally, this should be portable between Prolog implementation. Unfortunately there is no portably binary term format defined.

The current implementation is based on PL_record_external(), which provides a binary representation of terms that is processed efficiently and can handle cycles as well as attributes. We try to keep the format compatible between versions, but this is not guaranteed. Conversion is always possible by reading a database using the old version, dump it using write_canonical/1 and read it into the new version.

This library is built upon the following built in predicates:

Compatibility
- The format is not compatible to SICStus/Ciao (which are not compatible either). Funture versions of this library might implement a different encoding.
bug
-
The current implementation of fast_read/1 is not safe. It is guaranteed to safely read terms written using fast_write/1, but may crash on arbitrary input. The implementation does perform some basic sanity checks, including validation of the magic start byte. */
To be done
- Establish a portable binary format.
 fast_read(-Term)
The next term is read from current standard input and is unified with Term. The syntax of the term must agree with fast_read / fast_write format. If the end of the input has been reached, Term is unified with the term end_of_file.
   83fast_read(Term) :-
   84    fast_read(current_input, Term).
 fast_write(+Term)
Output Term in a way that fast_read/1 and fast_read/2 will be able to read it back.
   91fast_write(Term) :-
   92    fast_write(current_output, Term).
 fast_write_to_string(+Term, -String, ?Tail)
Perform a fast-write to the difference-slist String\Tail.
   98fast_write_to_string(T, S, R) :-
   99    fast_term_serialized(T, String),
  100    string_codes(String, Codes),
  101    append(Codes, R, S)