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) 1998-2011, University of Amsterdam 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without 10 modification, are permitted provided that the following conditions 11 are met: 12 13 1. Redistributions of source code must retain the above copyright 14 notice, this list of conditions and the following disclaimer. 15 16 2. Redistributions in binary form must reproduce the above copyright 17 notice, this list of conditions and the following disclaimer in 18 the documentation and/or other materials provided with the 19 distribution. 20 21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 POSSIBILITY OF SUCH DAMAGE. 33*/ 34 35:- module('$rc', 36 [ open_resource/3, % +Name, ?Class, -Stream 37 open_resource/4, % +Name, ?Class, +RW, -Stream 38 current_resource/3 % :Name, ?Class, ?File 39 ]). 40 41:- meta_predicate 42 open_resource( , , ), 43 open_resource( , , , ), 44 current_resource( , , ). 45 46:- dynamic 47 user:resource/3. 48:- multifile 49 user:resource/3. 50 51%! open_resource(:Name, ?Class, -Handle) is det. 52%! open_resource(:Name, ?Class, +Mode, -Handle) is det. 53% 54% Open resource with given Name and Class, returning a stream 55% handle. 56 57open_resource(Name, Class, Handle) :- 58 open_resource(Name, Class, read, Handle). 59 60open_resource(Module:RcName, Class, RW, Handle) :- 61 ( default_module(Module, RModule), 62 current_resource(RModule:RcName, Class, FileSpec) 63 -> absolute_file_name(FileSpec, File), 64 open(File, RW, Handle, [type(binary)]) 65 ; '$rc_handle'(RC), 66 tag_rc_name(Module, RcName, TaggedName), 67 '$rc_open'(RC, TaggedName, Class, RW, Handle) 68 ). 69 70tag_rc_name(user, RcName, RcName) :- !. 71tag_rc_name(Module, RcName, TaggedName) :- 72 atomic_list_concat([Module, ':', RcName], TaggedName). 73tag_rc_name(_, RcName, RcName). 74 75%! current_resource(:Name, ?Class, ?File) is nondet. 76% 77% List all currently declared resources. Should eventually deal 78% with resources that are already part of the state. 79 80current_resource(M:Name, Class, File) :- 81 current_module(M), 82 current_predicate(M:resource/3), 83 M:resource(Name, Class, File)