Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Stream in Prolog?

I have to work with some SWI-Prolog code that opens a new stream (which creates a file on the file system) and pours some data in. The generated file is read somewhere else later on in the code.

I would like to replace the file stream with a string stream in Prolog so that no files are created and then read everything that was put in the stream as one big string.

Does SWI-Prolog have string streams? If so, how could I use them to accomplish this task? I would really appreciate it if you could provide a small snippet. Thank you!

like image 919
George Flourentzos Avatar asked Jan 11 '23 15:01

George Flourentzos


1 Answers

SWI-Prolog implements memory mapped files. Here is a snippet from some old code of mine, doing both write/read

%%  html2text(+Html, -Text) is det.
%
%   convert from html to text
%
html2text(Html, Text) :-
    html_clean(Html, HtmlDescription),
    new_memory_file(Handle),
    open_memory_file(Handle, write, S),
    format(S, '<html><head><title>html2text</title></head><body>~s</body></html>', [HtmlDescription]),
    close(S),
    open_memory_file(Handle, read, R, [free_on_close(true)]),
    load_html_file(stream(R), [Xml]),
    close(R),
    xpath(Xml, body(normalize_space), Text).
like image 96
CapelliC Avatar answered Jan 25 '23 21:01

CapelliC