/* File: flrio.flr ** ** Author(s): Michael Kifer ** Contact: flora-users@lists.sourceforge.net ** ** Copyright (C) The Research Foundation of SUNY, 2001 ** ** FLORA-2 is free software; you can redistribute it and/or modify it under the ** terms of the GNU Library General Public License as published by the Free ** Software Foundation; either version 2 of the License, or (at your option) ** any later version. ** ** FLORA-2 is distributed in the hope that it will be useful, but WITHOUT ANY ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ** FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for ** more details. ** ** You should have received a copy of the GNU Library General Public License ** along with FLORA-2; if not, write to the Free Software Foundation, ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ** ** $Id: flrio.flr,v 1.12 2003/06/18 07:01:27 kifer Exp $ ** */ #include "standard.h" #include "flora_terms.flh" %% The purpose of this library is to provide optimal conversion from %% HiLog to Prolog and back before passing the arguments to the I/O predicates. %% In addition, it passes the result through flora_write_oid/1 in order %% to ensure that all the FLORA module info and encoding is stripped. %% %% This all is ued for user-level output. For file I/O one should use %% @prolog() instead of @flora(io), i.e., use Prolog I/O primitives directly. %%%%%%%%%%%%%%%%%%%%%% %% I/O port based ops %%%%%%%%%%%%%%%%%%%%%% Filename[#open(Mode,Port)] :- file_open(Filename,Mode,Port)@prolog(file_io). Port[#close] :- file_close(Port)@prolog(file_io). Port[#read(HResult)] :- integer(Port), !, file_read(Port, PResult)@prolog(file_io), p2h{PResult,HResult}. Port[#write(Term)] :- integer(Port), !, flora_write(Port,Term)@prolog(flrdisplay). stdread(HResult) :- file_read(STDIN, PResult)@prolog(file_io), p2h{PResult,HResult}. stdwrite(Term) :- flora_write(STDOUT,Term)@prolog(flrdisplay). %% Use arg(obj, obj, obj) to supply multiple arguments fmt_write(Format,O) :- !, florify_arguments(O,FO), fmt_write(Format,FO)@prolog(). FileHandle[#fmt_write(Format,O)] :- florify_arguments(O,FO), fmt_write(FileHandle,Format,FO)@prolog(). fmt_write_string(String,Format,O) :- !, florify_arguments(O,FO), fmt_write_string(String,Format,FO)@prolog(). fmt_read(Format,Result,Status) :- !, fmt_read(Format,PResult,Status)@prolog(), p2h{PResult,Result}. FileHandle[#fmt_read(Format,Result,Status)] :- fmt_read(FileHandle,Format,PResult,Status)@prolog(), p2h{PResult,Result}. Filehandle[#write_canonical(HTerm)] :- p2h{PTerm,HTerm}, file_write_canonical(Filehandle,PTerm)@prolog(xsb_writ). write_canonical(HTerm) :- p2h{PTerm,HTerm}, write_canonical(PTerm)@prolog(). read_canonical(HTerm) :- read_canonical(PTerm)@prolog(), p2h{PTerm,HTerm}. Filehandle[#read_canonical(HTerm)] :- file_read_canonical(Filehandle,PTerm,_)@prolog(machine), p2h{PTerm,HTerm}. Filehandle[#readline(atom,String)] :- !, file_read_line_atom(Filehandle,String)@prolog(file_io). Filehandle[#readline(charlist,String)] :- !, file_read_line_list(Filehandle,String)@prolog(file_io). _Filehandle[#readline(Request,_)] :- abort(('Invalid readline format, ', Request))@flora(sys). readline(atom,String) :- !, file_read_line_atom(String)@prolog(file_io). readline(charlist,String) :- !, file_read_line_list(String)@prolog(file_io). readline(Request,_) :- abort(('Invalid readline format, ', Request))@flora(sys). %%%%%%%%%%%%%%%%%%%% %% Stream-based I/O %%%%%%%%%%%%%%%%%%%% write(O) :- !, flora_write_oid(O)@prolog(flrdecode). IOstream[#write(O)] :- flora_write_oid(IOstream,O)@prolog(flrdecode). writeln(O) :- !, flora_write_oid(O)@prolog(flrdecode), nl@prolog(). IOstream[#writeln(O)] :- flora_write_oid(IOstream,O)@prolog(flrdecode), nl(IOstream)@prolog(). nl :- nl@prolog(). nl(IOstream) :- nl(IOstream)@prolog(). read(Result) :- !, read(PResult)@prolog(), p2h{PResult,Result}. IOstream[#read(Result)] :- read(IOstream,PResult)@prolog(), p2h{PResult,Result}. Filename[#see] :- see(Filename)@prolog(). seen :- seen@prolog(). seeing(Stream) :- seeing(Stream)@prolog(). Filename[#tell] :- tell(Filename)@prolog(). tell(Filename) :- tell(Filename)@prolog(). told :- told@prolog(). telling(Stream) :- telling(Stream)@prolog(). %% UTILITIES %% This is used for fmt_write_* predicates. Takes a *prolog* term argument %% of the form %% _(arg1,...,argn) and returns _(arg1',..., argn'), where arg_i=arg_i' %% if arg_i is not compound. Otherwise we apply flora_write_oid/1 %% This is needed in order make sure that arguments like f(a) are printed %% as f(a) and not _flora'main'f(a) or something like that. florify_arguments(WrappedArgs,OutTerm) :- WrappedArgs =.. [WRAP_HILOG, Fun | Args], !, florify_arglist(Args,OutList), OutTerm =.. [Fun|OutList]. %% single argument to fmt_write without the arg(...) envelope florify_arguments(Arg,OutTerm) :- flora_decode_oid_as_atom(Arg,OutTerm)@prolog(flrdecode). florify_arglist([],[]) :- !. florify_arglist([Arg|Args], [OutArg| OutArgs]) :- compound(Arg)@prolog(), !, flora_decode_oid_as_atom(Arg,OutArg)@prolog(flrdecode), florify_arglist(Args,OutArgs). florify_arglist([_Arg|Args], [_Arg| OutArgs]) :- florify_arglist(Args,OutArgs).