Home Contents Index Summary Previous Next

6.3 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) ).