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)  2015-2020, VU University Amsterdam
    7                              CWI, 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(iostream,
   37          [ open_any/5,         % +Spec, +Mode, -Stream, -Close, +Options
   38            close_any/1         % +Close
   39          ]).   40:- autoload(library(apply),[partition/4]).   41:- autoload(library(error),
   42	    [instantiation_error/1,uninstantiation_error/1,must_be/2]).   43:- autoload(library(option),[option/2]).   44
   45:- if(exists_source(library(uri))).   46:- use_module(library(uri), [uri_file_name/2]).   47:- endif.

Utilities to deal with streams

This library contains utilities that deal with streams, notably originating from non-built-in sources such as URLs, archives, windows, processes, etc.

The predicate open_any/5 acts as a broker between applications that can process data from a stream and libraries that can create streams from diverse sources. Without this predicate, processing data inevitally follows the pattern below. As call_some_open_variation can be anything, this blocks us from writing predicates such as load_xml(From, DOM) that can operate on arbitrary input sources.

setup_call_cleanup(
    call_some_open_variation(Spec, In),
    process(In),
    close(In)).

Libraries that can open streams can install the hook iostream:open_hook/6 to make their functionality available through open_any/5.

See also
- library(archive), library(process), library(zlib), library(http/http_stream) */
   77:- multifile
   78    open_hook/6.    % +Spec, +Mode, -Stream, -Close, +Options0, -Options
 open_any(+Specification, +Mode, -Stream, -Close, +Options)
Establish a stream from Specification that should be closed using Close, which can either be called or passed to close_any/1. Options processed:
encoding(Enc)
Set stream to encoding Enc.

Without loaded plugins, the open_any/5 processes the following values for Specification. If no rule matches, open_any/5 processes Specification as file(Specification).

Stream
A plain stream handle. Possisible post-processing options such as encoding are applied. Close does not close the stream, but resets other side-effects such as the encoding.
stream(Stream)
Same as a plain Stream.
FileURL
If Specification is of the form =file://...=, the pointed to file is opened using open/4. Requires library(uri) to be installed.
file(Path)
Explicitly open the file Path. Path can be an Path(File) term as accepted by absolute_file_name/3.
string(String)
Open a Prolog string, atom, list of characters or codes as an input stream.

The typical usage scenario is given in the code below, where <process> processes the input.

setup_call_cleanup(
    open_any(Spec, read, In, Close, Options),
    <process>(In),
    Close).

Currently, the following libraries extend this predicate:

library(http/http_open)
Adds support for URLs using the http and https schemes.
  125open_any(Spec, Mode, Stream, Close, Options) :-
  126    \+ ( ground(Spec),                      % argument sanity check
  127         var(Stream),
  128         var(Close),
  129         is_list(Options) ),
  130    !,
  131    open_error(Spec, Mode, Stream, Close, Options).
  132open_any(Spec, _Mode, Stream, Close, Options) :-
  133    is_stream(Spec),
  134    !,
  135    Stream = Spec,
  136    input_options(Spec, Stream, true, Close, Options).
  137:- if(current_predicate(uri_file_name/2)).  138open_any(Spec, Mode, Stream, Close, Options0) :-
  139    atomic(Spec),
  140    uri_file_name(Spec, File),
  141    !,
  142    open_any_builtin(file(File), Mode, Stream0, Close0, Options0, Options),
  143    input_options(Stream0, Stream, Close0, Close, Options).
  144:- endif.  145open_any(Spec, Mode, Stream, Close, Options0) :-
  146    open_any_builtin(Spec, Mode, Stream0, Close0, Options0, Options),
  147    !,
  148    input_options(Stream0, Stream, Close0, Close, Options).
  149open_any(Spec, Mode, Stream, Close, Options0) :-
  150    open_hook(Spec, Mode, Stream0, Close0, Options0, Options),
  151    !,
  152    input_options(Stream0, Stream, Close0, Close, Options).
  153open_any(Spec, Mode, Stream, Close, Options0) :-
  154    open_any_builtin(file(Spec), Mode, Stream0, Close0, Options0, Options),
  155    input_options(Stream0, Stream, Close0, Close, Options).
  156
  157open_error(Spec, _Mode, _Stream, _Close, _Options) :-
  158    var(Spec), !, instantiation_error(Spec).
  159open_error(_Spec, _Mode, Stream, _Close, _Options) :-
  160    nonvar(Stream),
  161    !,
  162    uninstantiation_error(Stream).
  163open_error(_Spec, _Mode, _Stream, Close, _Options) :-
  164    nonvar(Close),
  165    !,
  166    uninstantiation_error(Close).
  167open_error(_Spec, _Mode, _Stream, _Close, Options) :-
  168    \+ is_list(Options),
  169    !,
  170    must_be(list, Options).
 input_options(+Stream0, -Stream, +Close0, -Close, +Options) is det
Establish the final stream.
  176input_options(Spec, Stream, Close0, Close, Options) :-
  177    option(encoding(Enc), Options),
  178    !,
  179    Stream = Spec,
  180    stream_property(Stream, encoding(Enc0)),
  181    set_stream(Stream, encoding(Enc)),
  182    mkconj(set_stream(Stream, encoding(Enc0)), Close0, Close).
  183input_options(Stream, Stream, Close, Close, _).
  184
  185mkconj(set_stream(In,encoding(_)), close(In), close(In)) :- !.
  186mkconj(true,                       X,         X) :- !.
  187mkconj(X,                          true,      X) :- !.
  188mkconj(X,                          Y,         (X,Y)) :- !.
 open_any_builtin(+Spec, +Mode, -Stream, -Close, +Options0, -Options) is semidet
Built-in open-any operations
  195open_any_builtin(stream(Stream), _Mode, Stream, true, Options, Options) :-
  196    must_be(stream, Stream).
  197open_any_builtin(file(Spec), Mode, Stream, close(Stream), Options0, Options) :-
  198    (   compound(Spec)
  199    ->  absolute_file_name(Spec, Path, [access(Mode)|Options0])
  200    ;   Path = Spec
  201    ),
  202    partition(open_option, Options0, OpenOptions, Options),
  203    open(Path, Mode, Stream, OpenOptions).
  204open_any_builtin(string(S), read, Stream, close(Stream), Options, Options) :-
  205    open_string(S, Stream).
  206
  207open_option(encoding(_)).
  208open_option(type(_)).
 close_any(+Goal)
Execute the Close closure returned by open_any/5. The closure can also be called directly. Using close_any/1 can be considered better style and enhances tractability of the source code.
  216close_any(Var) :-
  217    var(Var), !, instantiation_error(Var).
  218close_any((A,B)) :- close_any(A), close_any(B).
  219close_any(true).
  220close_any(close(Stream)) :- close(Stream).
  221close_any(set_stream(S, encoding(Enc))) :- set_stream(S, encoding(Enc)).
  222
  223
  224                 /*******************************
  225                 *             HOOKS            *
  226                 *******************************/
 open_hook(+Spec, +Mode, -Stream, -Close, +Options0, -Options) is semidet
Open Spec in Mode, producing Stream.
Arguments:
Close- is unified to a goal that must be called to undo the side-effects of the action, e.g., typically the term close(Stream)
Options0- are the options passed to open_any/5
Options- are passed to the post processing filters that may be installed by open_any/5.