13.4.1 Resources as files
As of SWI-Prolog 7.7.13, resources that are compiled into the program can be accessed using the normal file handling predicates. Currently the following predicates transparently handle resources as read-only files:
In addition, open_shared_object/3, underlying use_foreign_library/1 handles shared objects or DLLs by copying them to a temporary file and opening this file. If the OS allows for it, the copied file is deleted immediately, otherwise it is deleted on program termination.
With the ability to open resources as if they were files we can use them for many tasks without changing the source code as required when using open_resource/2. Below we describe a typical scenario.
- Related resources are placed in one or more directories. Consider a
web application where we have several directories holding icons. Add
clauses to file_search_path/2
that makes all icons accessible using the term
icon(file)
. - Add a clause as below before creating the state. This causes all
icons to be become available as
res://app/icon/
file.resource(app/icon, icon(.)).
- Add a clause to file_search_path/2
that make the icons available from the resource data. For example using
the code below.
:- asserta(user:file_search_path(icon, 'res://app/icon').