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
|
|
|
]).
|
|
|
|
|
2014-12-19 23:47:50 +00:00
|
|
|
/** @defgroup CharsIO Operations on Sequences of Codes.
|
2014-09-11 20:06:57 +01:00
|
|
|
@ingroup YAPLibrary
|
|
|
|
|
|
|
|
Term to sequence of codes conversion, mostly replaced by engine code.
|
|
|
|
*/
|
|
|
|
|
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,-,?,?)).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred format_to_chars(+ _Form_, + _Args_, - _Result_)
|
|
|
|
|
|
|
|
Execute the built-in procedure format/2 with form _Form_ and
|
|
|
|
arguments _Args_ outputting the result to the string of character
|
|
|
|
codes _Result_.
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
format_to_chars(Format, Args, Codes) :-
|
|
|
|
format(codes(Codes), Format, Args).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred format_to_chars(+ _Form_, + _Args_, - _Result_, - _Result0_)
|
|
|
|
|
|
|
|
Execute the built-in procedure format/2 with form _Form_ and
|
|
|
|
arguments _Args_ outputting the result to the difference list of
|
|
|
|
character codes _Result-Result0_.
|
|
|
|
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
format_to_chars(Format, Args, OUT, L0) :-
|
|
|
|
format(codes(OUT, L0), Format, Args).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred write_to_chars(+ _Term_, - _Result_)
|
|
|
|
|
|
|
|
Execute the built-in procedure write/1 with argument _Term_
|
|
|
|
outputting the result to the string of character codes _Result_.
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
write_to_chars(Term, Codes) :-
|
|
|
|
format(codes(Codes), '~w', [Term]).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred write_to_chars(+ _Term_, - _Result0_, - _Result_)
|
|
|
|
|
|
|
|
Execute the built-in procedure write/1 with argument _Term_
|
|
|
|
outputting the result to the difference list of character codes
|
|
|
|
_Result-Result0_.
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
write_to_chars(Term, Out, Tail) :-
|
|
|
|
format(codes(Out,Tail),'~w',[Term]).
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred atom_to_chars(+ _Atom_, - _Result_)
|
2001-04-09 20:54:03 +01:00
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
Convert the atom _Atom_ to the string of character codes
|
|
|
|
_Result_.
|
|
|
|
*/
|
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
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred atom_to_chars(+ _Atom_, - _Result0_, - _Result_)
|
|
|
|
|
|
|
|
Convert the atom _Atom_ to the difference list of character codes
|
|
|
|
_Result-Result0_.
|
|
|
|
*/
|
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
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred number_to_chars(+ _Number_, - _Result_)
|
|
|
|
|
|
|
|
Convert the number _Number_ to the string of character codes
|
|
|
|
_Result_.
|
|
|
|
*/
|
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
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred number_to_chars(+ _Number_, - _Result0_, - _Result_)
|
|
|
|
|
|
|
|
Convert the atom _Number_ to the difference list of character codes
|
|
|
|
_Result-Result0_.
|
|
|
|
*/
|
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))).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred open_chars_stream(+ _Chars_, - _Stream_)
|
|
|
|
|
|
|
|
Open the list of character codes _Chars_ as a stream _Stream_.
|
|
|
|
*/
|
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
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred with_output_to_chars(? _Goal_, - _Chars_)
|
|
|
|
|
|
|
|
Execute goal _Goal_ such that its standard output will be sent to a
|
|
|
|
memory buffer. After successful execution the contents of the memory
|
|
|
|
buffer will be converted to the list of character codes _Chars_.
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
with_output_to_chars(Goal, Codes) :-
|
|
|
|
with_output_to(codes(Codes), Goal).
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred with_output_to_chars(? _Goal_, ? _Chars0_, - _Chars_)
|
|
|
|
|
|
|
|
Execute goal _Goal_ such that its standard output will be sent to a
|
|
|
|
memory buffer. After successful execution the contents of the memory
|
|
|
|
buffer will be converted to the difference list of character codes
|
|
|
|
_Chars-Chars0_.
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
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.
|
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred with_output_to_chars(? _Goal_, - _Stream_, ? _Chars0_, - _Chars_)
|
|
|
|
|
|
|
|
|
|
|
|
Execute goal _Goal_ such that its standard output will be sent to a
|
|
|
|
memory buffer. After successful execution the contents of the memory
|
|
|
|
buffer will be converted to the difference list of character codes
|
|
|
|
_Chars-Chars0_ and _Stream_ receives the stream corresponding to
|
|
|
|
the memory buffer.
|
|
|
|
|
|
|
|
*/
|
2011-02-12 18:42:44 +00:00
|
|
|
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
|
|
|
|
2014-09-11 20:06:57 +01:00
|
|
|
/** @pred read_from_chars(+ _Chars_, - _Term_)
|
|
|
|
|
|
|
|
Parse the list of character codes _Chars_ and return the result in
|
|
|
|
the term _Term_. The character codes to be read must terminate with
|
|
|
|
a dot character such that either (i) the dot character is followed by
|
|
|
|
blank characters; or (ii) the dot character is the last character in the
|
|
|
|
string.
|
|
|
|
|
2011-06-14 09:00:25 +01:00
|
|
|
% @compat The SWI-Prolog version does not require Codes to end
|
|
|
|
% in a full-stop.
|
2014-09-11 20:06:57 +01:00
|
|
|
*/
|
2011-06-14 09:00:25 +01:00
|
|
|
read_from_chars("", end_of_file) :- !.
|
|
|
|
read_from_chars(List, Term) :-
|
|
|
|
atom_to_term(List, Term, _).
|
2014-09-11 20:06:57 +01:00
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|
2011-06-14 09:00:25 +01:00
|
|
|
|