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)  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.   48
   49/** <module> Utilities to deal with streams
   50
   51This  library  contains  utilities  that   deal  with  streams,  notably
   52originating from non-built-in sources such   as URLs, archives, windows,
   53processes, etc.
   54
   55The predicate open_any/5 acts as a   _broker_  between applications that
   56can process data from a stream  and   libraries  that can create streams
   57from diverse sources. Without this predicate, processing data inevitally
   58follows  the  pattern  below.  As    _call_some_open_variation_  can  be
   59anything, this blocks us from writing  predicates such as load_xml(From,
   60DOM) that can operate on arbitrary input sources.
   61
   62  ==
   63  setup_call_cleanup(
   64      call_some_open_variation(Spec, In),
   65      process(In),
   66      close(In)).
   67  ==
   68
   69Libraries   that   can   open    streams     can    install   the   hook
   70iostream:open_hook/6  to  make  their  functionality  available  through
   71open_any/5.
   72
   73@see    library(archive), library(process), library(zlib),
   74        library(http/http_stream)
   75*/
   76
   77:- multifile
   78    open_hook/6.    % +Spec, +Mode, -Stream, -Close, +Options0, -Options
   79
   80%!  open_any(+Specification, +Mode, -Stream, -Close, +Options)
   81%
   82%   Establish a stream from  Specification   that  should  be closed
   83%   using  Close,  which  can  either  be    called   or  passed  to
   84%   close_any/1. Options processed:
   85%
   86%     - encoding(Enc)
   87%     Set stream to encoding Enc.
   88%
   89%   Without loaded plugins, the open_any/5   processes the following
   90%   values  for  Specification.  If  no   rule  matches,  open_any/5
   91%   processes Specification as file(Specification).
   92%
   93%     - Stream
   94%     A plain stream handle. Possisible post-processing options such
   95%     as encoding are applied. Close does _not_ close the stream,
   96%     but resets other side-effects such as the encoding.
   97%     - stream(Stream)
   98%     Same as a plain Stream.
   99%     - FileURL
  100%     If Specification is of the form =file://...=, the pointed
  101%     to file is opened using open/4.  Requires library(uri) to
  102%     be installed.
  103%     - file(Path)
  104%     Explicitly open the file Path.  Path can be an Path(File)
  105%     term as accepted by absolute_file_name/3.
  106%     - string(String)
  107%     Open a Prolog string, atom, list of characters or codes
  108%     as an _input_ stream.
  109%
  110%   The typical usage scenario is  given   in  the code below, where
  111%   <process> processes the input.
  112%
  113%     ==
  114%     setup_call_cleanup(
  115%         open_any(Spec, read, In, Close, Options),
  116%         <process>(In),
  117%         Close).
  118%     ==
  119%
  120%   Currently, the following libraries extend this predicate:
  121%
  122%      - library(http/http_open)
  123%      Adds support for URLs using the `http` and `https` schemes.
  124
  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).
  171
  172%!  input_options(+Stream0, -Stream, +Close0, -Close, +Options) is det.
  173%
  174%   Establish the final stream.
  175
  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)) :- !.
  189
  190%!  open_any_builtin(+Spec, +Mode, -Stream, -Close,
  191%!                   +Options0, -Options) is semidet.
  192%
  193%   Built-in open-any operations
  194
  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(_)).
  209
  210%!  close_any(+Goal)
  211%
  212%   Execute the `Close` closure returned  by open_any/5. The closure
  213%   can also be called directly. Using close_any/1 can be considered
  214%   better style and enhances tractability of the source code.
  215
  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                 *******************************/
  227
  228%!  open_hook(+Spec, +Mode, -Stream, -Close, +Options0, -Options) is semidet.
  229%
  230%   Open Spec in Mode, producing Stream.
  231%
  232%   @arg Close is unified to a goal that must be called to undo the
  233%   side-effects of the action, e.g., typically the term close(Stream)
  234%   @arg Options0 are the options passed to open_any/5
  235%   @arg Options are passed to the post processing filters that
  236%   may be installed by open_any/5.