View source with formatted 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)]).   63
   64/** <module> I/O on Lists of Character Codes
   65
   66This module emulates the SICStus  library   codesio.pl  for  reading and
   67writing from/to lists of character codes.   Most of these predicates are
   68straight calls into similar SWI-Prolog primitives.
   69
   70This library is based on library(charsio)   that originates from Quintus
   71Prolog. The naming is updated to reflect  the ISO naming conventions and
   72the ISO predicates atom_codes/2, etc  are  obviously  removed  from this
   73library.
   74
   75@compat SICStus 4
   76*/
   77
   78%!  format_to_codes(+Format, +Args, -Codes) is det.
   79%
   80%   Use format/2 to write to a list of character codes.
   81
   82format_to_codes(Format, Args, Codes) :-
   83    format(codes(Codes), Format, Args).
   84
   85%!  format_to_codes(+Format, +Args, -Codes, ?Tail) is det.
   86%
   87%   Use format/2 to write to a difference list of character codes.
   88
   89format_to_codes(Format, Args, Codes, Tail) :-
   90    format(codes(Codes, Tail), Format, Args).
   91
   92%!  write_to_codes(+Term, -Codes)
   93%
   94%   Codes is a list of character codes produced by write/1 on Term.
   95
   96write_to_codes(Term, Codes) :-
   97    format(codes(Codes), '~w', [Term]).
   98
   99%!  write_to_codes(+Term, -Codes, ?Tail)
  100%
  101%   Codes is a difference-list of character codes produced by write/1 on Term.
  102
  103write_to_codes(Term, Codes, Tail) :-
  104    format(codes(Codes, Tail), '~w', [Term]).
  105
  106%!  write_term_to_codes(+Term, -Codes, +Options) is det.
  107%
  108%   True  when  Codes  is  a  string  that  matches  the  output  of
  109%   write_term/3 using Options.
  110
  111write_term_to_codes(Term, Codes, Options) :-
  112    format(codes(Codes), '~W', [Term, Options]).
  113
  114%!  write_term_to_codes(+Term, -Codes, ?Tail, +Options) is det.
  115%
  116%   True  when  Codes\Tail  is  a  difference  list  containing  the
  117%   character codes that matches the   output  of write_term/3 using
  118%   Options.
  119
  120write_term_to_codes(Term, Codes, Tail, Options) :-
  121    format(codes(Codes, Tail), '~W', [Term, Options]).
  122
  123%!  read_from_codes(+Codes, -Term) is det.
  124%
  125%   Read Codes into Term.
  126%
  127%   @compat The SWI-Prolog version does not require Codes to end
  128%           in a full-stop.
  129
  130read_from_codes([], Term) :-
  131    !,
  132    Term = end_of_file.
  133read_from_codes(List, Term) :-
  134    atom_to_term(List, Term, _).
  135
  136%!  read_term_from_codes(+Codes, -Term, +Options) is det.
  137%
  138%   Read Codes into Term.  Options are processed by read_term/3.
  139%
  140%   @compat sicstus
  141
  142read_term_from_codes(Codes, Term, Options) :-
  143    read_term_from_atom(Codes, Term, Options).
  144
  145%!  open_codes_stream(+Codes, -Stream) is det.
  146%
  147%   Open Codes as an input stream.
  148%
  149%   @see open_string/2.
  150
  151open_codes_stream(Codes, Stream) :-
  152    open_string(Codes, Stream).
  153
  154%!  with_output_to_codes(:Goal, Codes) is det.
  155%
  156%   Run Goal with as once/1.  Output written to =current_output=
  157%   is collected in Codes.
  158
  159with_output_to_codes(Goal, Codes) :-
  160    with_output_to(codes(Codes), Goal).
  161
  162%!  with_output_to_codes(:Goal, -Codes, ?Tail) is det.
  163%
  164%   Run Goal with as once/1.  Output written to =current_output=
  165%   is collected in Codes\Tail.
  166
  167with_output_to_codes(Goal, Codes, Tail) :-
  168    with_output_to(codes(Codes, Tail), Goal).
  169
  170%!  with_output_to_codes(:Goal, -Stream, -Codes, ?Tail) is det.
  171%
  172%   As  with_output_to_codes/3,  but  Stream  is  unified  with  the
  173%   temporary  stream.  This  predicate   exists  for  compatibility
  174%   reasons. In SWI-Prolog, the temporary   stream is also available
  175%   as `current_output`.
  176
  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)