Home
Contents
Index
Summary
Previous
Next
A resource is very similar to a file. Resources however can
be represented in two different formats: on files, as well as part of
the resource archive of a saved-state (see qsave_program/2).
A resource has a name and a class. The source
data of the resource is a file. Resources are declared by declaring the
predicate resource/3.
They are accessed using the predicate
open_resource/3.
Before going into details, let us start with an example. Short texts
can easily be expressed in Prolog sourcecode, but long texts are
cumbersome. Assume our application defines a command `help' that prints
a helptext to the screen. We put the content of the helptext into a file
called help.txt
. The following code implements our help
command such that help.txt is incorperated into the runtime executable.
resource(help, text, 'help.txt').
help :-
open_resource(help, text, In),
copy_stream(In, user_output),
close(In).
copy_stream(In, Out) :-
get0(In, C),
copy_stream(C, In, Out).
copy_stream(-1, _, _) :- !.
copy_stream(C, In, Out) :-
put(Out, C),
get0(In, C2),
copy_stream(C2, In, Out).
The predicate help/0
opens the resource as a Prolog stream. If we are executing this from the
development environment, this will actually return a stream to the gelp.txt
itself. When executed from the saved-state, the stream will actually be
a stream opened on the program resource file, taking care of the offset
and length of the resource.
- resource(+Name, +Class,
+FileSpec)
-
This predicate is defined as a dynamic predicate in the module
user
. Clauses for it may be defined in any module,
including the user module. Name is the name of the resource
(an atom). A resource name may contain all characters, except for