Symbolic filenames

Author(s): Francisco Bueno.

This module provides a predicate for file opening which can use any term as an alias for the filename (i.e., symbolic filenames) instead of the usual constants which are file system path names of the actual files.

The correspondence between an alias and the actual file path is done dynamically, without having to recompile the program. It is possible to define the correspondence via facts for file_alias/2 in a file declared with multifile:alias_file/1 in the program: those facts will be dynamically loaded when running the program. Alternatively, the correspondence can be defined via shell environment variables, by defining the value of a variable by the (symbolic) name of the file to be the path of the actual file.

Usage and interface

Documentation on exports

PREDICATE
open(File,Mode,Stream)

Open File with mode Mode and return in Stream the stream associated with the file. It is like streams_basic:open/3, but File is considered a symbolic name: either defined by user:file_alias/2 or as an environment variable. Predicate user:file_alias/2 is inspected before the environment variables.

Usage:

  • Calls should, and exit will be compatible with:
    (streams_basic:stream/1)Stream is an open stream.
  • The following properties should hold at call time:
    (basic_props:term/1)File is any term.
    (streams_basic:io_mode/1)Mode is an opening mode ('read', 'write' or 'append').
  • The following properties hold upon exit:
    (streams_basic:stream/1)Stream is an open stream.

Documentation on multifiles

PREDICATE
alias_file(File)

Declares File to be a file defining symbolic names via file_alias/2. Anything else in File is simply ignored.
The predicate is multifile.

PREDICATE
file_alias(Alias,File)

Declares Alias as a symbolic name for File, the real name of an actual file (or directory).
The predicate is multifile.
The predicate is of type data.

Other information

The example discussed here is included in the distribution files. There is a main application file which uses module mm. This module reads a line from a file; the main predicate in the main file then prints this line. The important thing is that the file read is named by a symbolic name "file". The main application file declares another file where the symbolic names are assigned actual file names:

:- use_module(mm).

:- multifile alias_file/1.
alias_file(myfiles).

main :- p(X), display(X), nl.

Now, the file myfiles.pl can be used to change the file you want to read from without having to recompile the application. The current assignment is:

%:- use_package([]).
file_alias(file,'mm.pl').

so the execution of the application will show the first line of mm.pl. However, you can change to:

file_alias(file,'main.pl').

and then execution of the same executable will show the first line of main.pl.