2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: charsio.yap *
|
|
|
|
* Last rev: 5/12/99 *
|
|
|
|
* mods: *
|
|
|
|
* comments: I/O on character strings *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
:- module(charsio, [
|
|
|
|
format_to_chars/3,
|
|
|
|
format_to_chars/4,
|
|
|
|
write_to_chars/3,
|
|
|
|
write_to_chars/2,
|
|
|
|
atom_to_chars/3,
|
|
|
|
atom_to_chars/2,
|
|
|
|
number_to_chars/3,
|
|
|
|
number_to_chars/2,
|
|
|
|
read_from_chars/2,
|
|
|
|
open_chars_stream/2,
|
|
|
|
with_output_to_chars/2,
|
|
|
|
with_output_to_chars/3,
|
2010-11-24 20:53:16 +00:00
|
|
|
with_output_to_chars/4,
|
|
|
|
term_to_atom/2
|
2001-04-09 20:54:03 +01:00
|
|
|
]).
|
|
|
|
|
2010-10-28 17:18:57 +01:00
|
|
|
:- meta_predicate(with_output_to_chars(0,?)).
|
|
|
|
:- meta_predicate(with_output_to_chars(0,-,?)).
|
|
|
|
:- meta_predicate(with_output_to_chars(0,-,?,?)).
|
|
|
|
|
2011-02-12 18:42:44 +00:00
|
|
|
format_to_chars(Format, Args, Codes) :-
|
|
|
|
format(codes(Codes), Format, Args).
|
|
|
|
|
|
|
|
format_to_chars(Format, Args, OUT, L0) :-
|
|
|
|
format(codes(OUT, L0), Format, Args).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2011-02-12 18:42:44 +00:00
|
|
|
write_to_chars(Term, Codes) :-
|
|
|
|
format(codes(Codes), '~w', [Term]).
|
|
|
|
|
|
|
|
write_to_chars(Term, Out, Tail) :-
|
|
|
|
format(codes(Out,Tail),'~w',[Term]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
atom_to_chars(Atom, OUT) :-
|
2011-02-12 18:42:44 +00:00
|
|
|
atom_codes(Atom, OUT).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
atom_to_chars(Atom, L0, OUT) :-
|
2011-02-12 18:42:44 +00:00
|
|
|
format(codes(L0, OUT), '~a', [Atom]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
number_to_chars(Number, OUT) :-
|
2011-02-12 18:42:44 +00:00
|
|
|
number_codes(Number, OUT).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
number_to_chars(Number, L0, OUT) :-
|
|
|
|
var(Number), !,
|
|
|
|
throw(error(instantiation_error,number_to_chars(Number, L0, OUT))).
|
|
|
|
number_to_chars(Number, L0, OUT) :-
|
|
|
|
number(Number), !,
|
2011-02-12 18:42:44 +00:00
|
|
|
format(codes(L0, OUT), '~w', [Number]).
|
2001-04-09 20:54:03 +01:00
|
|
|
number_to_chars(Number, L0, OUT) :-
|
|
|
|
throw(error(type_error(number,Number),number_to_chars(Number, L0, OUT))).
|
|
|
|
|
2011-02-12 18:42:44 +00:00
|
|
|
open_chars_stream(Codes, Stream) :-
|
|
|
|
open_chars_stream(Codes, Stream, '').
|
|
|
|
|
|
|
|
open_chars_stream(Codes, Stream, Postfix) :-
|
2011-03-11 19:48:43 +00:00
|
|
|
predicate_property(memory_file:open_memory_file(_,_,_),_), !,
|
|
|
|
memory_file:new_memory_file(MF),
|
|
|
|
memory_file:open_memory_file(MF, write, Out),
|
2011-02-12 18:42:44 +00:00
|
|
|
format(Out, '~s~w', [Codes, Postfix]),
|
|
|
|
close(Out),
|
2011-03-11 19:48:43 +00:00
|
|
|
memory_file:open_memory_file(MF, read, Stream,
|
2011-02-12 18:42:44 +00:00
|
|
|
[ free_on_close(true)
|
|
|
|
]).
|
2011-03-11 19:48:43 +00:00
|
|
|
open_chars_stream(Codes, Stream, Postfix) :-
|
|
|
|
ensure_loaded(library(memfile)),
|
|
|
|
open_chars_stream(Codes, Stream, Postfix).
|
2011-02-12 18:42:44 +00:00
|
|
|
|
|
|
|
with_output_to_chars(Goal, Codes) :-
|
|
|
|
with_output_to(codes(Codes), Goal).
|
|
|
|
|
|
|
|
with_output_to_chars(Goal, Codes, L0) :-
|
|
|
|
with_output_to(codes(Codes, L0), Goal).
|
|
|
|
%% with_output_to_chars(:Goal, -Stream, -Codes, ?Tail) is det.
|
|
|
|
%
|
|
|
|
% As with_output_to_chars/2, but Stream is unified with the
|
|
|
|
% temporary stream.
|
|
|
|
|
|
|
|
with_output_to_chars(Goal, Stream, Codes, Tail) :-
|
|
|
|
with_output_to(codes(Codes, Tail), with_stream(Stream, Goal)).
|
|
|
|
|
|
|
|
with_stream(Stream, Goal) :-
|
|
|
|
current_output(Stream),
|
|
|
|
call(Goal).
|
2001-04-09 20:54:03 +01:00
|
|
|
|