Home Contents Index Summary Previous Next

8.4 Finding Application files

If your application uses files that are not part of the saved program such as database files, configuration files, etc., the runtime version has to be able to locate these files. The file_search_path/2 mechanism in combination with the -palias command-line argument is the preferred way to locate runtime files. The first step is to define an alias for the toplevel directory of your application. We will call this directory gnatdir in our examples.

A good place for storing data associated with SWI-Prolog runtime systems is below the emulator's home-directory. swi is a predefined alias for this directory. The following is a useful default definition for the search path.


user:file_search_path(gnatdir, swi(gnat)).

The application should locate all files using absolute_file_name. Suppose gnatdir contains a file config.pl to define local configuration. Then use the code below to load this file:


configure_gnat :-
        (   absolute_file_name(gnatdir('config.pl'), ConfigFile)
            ->  consult(ConfigFile)
            ;   format(user_error, 'gnat: Cannot locate config.pl~n'),
            halt(1)
            ).

8.4.1 Passing a path to the application

Suppose the system administrator has installed the SWI-Prolog runtime environment in /usr/local/lib/rt-pl-3.2.0. A user wants to install gnat, but gnat will look for its configuration in /usr/local/lib/rt-pl-3.2.0/gnat where the user cannot write.

The user decides to install the gnat runtime files in /users/bob/lib/gnat. For one-time usage, the user may decide to start gnat using the command:


% gnat -p gnatdir=/users/bob/lib/gnat