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)  2011-2015, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(codesio,
   37          [ format_to_codes/3,          % +Format, +Args, -Codes
   38            format_to_codes/4,          % +Format, +Args, -Codes, ?Tail
   39            write_to_codes/2,           % +Term, -Codes
   40            write_to_codes/3,           % +Term, -Codes, ?Tail
   41            write_term_to_codes/3,      % +Term, -Codes, +Options
   42            write_term_to_codes/4,      % +Term, -Codes, ?Tail, +Options
   43                                        % read predicates
   44            read_from_codes/2,          % +Codes, -Term
   45            read_term_from_codes/3,     % +Codes, -Term, +Options
   46            open_codes_stream/2,        % +Codes, -Stream
   47            with_output_to_codes/2,     % :Goal, -Codes
   48            with_output_to_codes/3,     % :Goal, -Codes, ?Tail
   49            with_output_to_codes/4      % :Goal, -Stream, -Codes, ?Tail
   50          ]).   51
   52:- meta_predicate
   53    with_output_to_codes(0, -),
   54    with_output_to_codes(0, -, ?),
   55    with_output_to_codes(0, -, -, ?).   56
   57:- predicate_options(read_term_from_codes/3, 3,
   58                     [pass_to(system:read_term/3, 3)]).   59:- predicate_options(write_term_to_codes/3, 3,
   60                     [pass_to(system:write_term/3, 3)]).   61:- predicate_options(write_term_to_codes/4, 4,
   62                     [pass_to(system:write_term/3, 3)]).

I/O on Lists of Character Codes

This module emulates the SICStus library codesio.pl for reading and writing from/to lists of character codes. Most of these predicates are straight calls into similar SWI-Prolog primitives.

This library is based on library(charsio) that originates from Quintus Prolog. The naming is updated to reflect the ISO naming conventions and the ISO predicates atom_codes/2, etc are obviously removed from this library.

Compatibility
- SICStus 4 */
 format_to_codes(+Format, +Args, -Codes) is det
Use format/2 to write to a list of character codes.
   82format_to_codes(Format, Args, Codes) :-
   83    format(codes(Codes), Format, Args).
 format_to_codes(+Format, +Args, -Codes, ?Tail) is det
Use format/2 to write to a difference list of character codes.
   89format_to_codes(Format, Args, Codes, Tail) :-
   90    format(codes(Codes, Tail), Format, Args).
 write_to_codes(+Term, -Codes)
Codes is a list of character codes produced by write/1 on Term.
   96write_to_codes(Term, Codes) :-
   97    format(codes(Codes), '~w', [Term]).
 write_to_codes(+Term, -Codes, ?Tail)
Codes is a difference-list of character codes produced by write/1 on Term.
  103write_to_codes(Term, Codes, Tail) :-
  104    format(codes(Codes, Tail), '~w', [Term]).
 write_term_to_codes(+Term, -Codes, +Options) is det
True when Codes is a string that matches the output of write_term/3 using Options.
  111write_term_to_codes(Term, Codes, Options) :-
  112    format(codes(Codes), '~W', [Term, Options]).
 write_term_to_codes(+Term, -Codes, ?Tail, +Options) is det
True when Codes\Tail is a difference list containing the character codes that matches the output of write_term/3 using Options.
  120write_term_to_codes(Term, Codes, Tail, Options) :-
  121    format(codes(Codes, Tail), '~W', [Term, Options]).
 read_from_codes(+Codes, -Term) is det
Read Codes into Term.
Compatibility
- The SWI-Prolog version does not require Codes to end in a full-stop.
  130read_from_codes([], Term) :-
  131    !,
  132    Term = end_of_file.
  133read_from_codes(List, Term) :-
  134    atom_to_term(List, Term, _).
 read_term_from_codes(+Codes, -Term, +Options) is det
Read Codes into Term. Options are processed by read_term/3.
Compatibility
- sicstus
  142read_term_from_codes(Codes, Term, Options) :-
  143    read_term_from_atom(Codes, Term, Options).
 open_codes_stream(+Codes, -Stream) is det
Open Codes as an input stream.
See also
- open_string/2.
  151open_codes_stream(Codes, Stream) :-
  152    open_string(Codes, Stream).
 with_output_to_codes(:Goal, Codes) is det
Run Goal with as once/1. Output written to current_output is collected in Codes.
  159with_output_to_codes(Goal, Codes) :-
  160    with_output_to(codes(Codes), Goal).
 with_output_to_codes(:Goal, -Codes, ?Tail) is det
Run Goal with as once/1. Output written to current_output is collected in Codes\Tail.
  167with_output_to_codes(Goal, Codes, Tail) :-
  168    with_output_to(codes(Codes, Tail), Goal).
 with_output_to_codes(:Goal, -Stream, -Codes, ?Tail) is det
As with_output_to_codes/3, but Stream is unified with the temporary stream. This predicate exists for compatibility reasons. In SWI-Prolog, the temporary stream is also available as current_output.
  177with_output_to_codes(Goal, Stream, Codes, Tail) :-
  178    with_output_to(codes(Codes, Tail), with_stream(Stream, Goal)).
  179
  180with_stream(Stream, Goal) :-
  181    current_output(Stream),
  182    call(Goal)