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.
77:- multifile 78 open_hook/6. % +Spec, +Mode, -Stream, -Close, +Options0, -Options
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)
.
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:
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).
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)) :- !.
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(_)).
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 *******************************/
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.Libraries that can open streams can install the hook iostream:open_hook/6 to make their functionality available through open_any/5.