Author(s): Francisco Bueno.
Version: 1.11#222 (2004/5/24, 13:8:7 CEST)
Version of last change: 1.9#9 (2002/5/28, 19:11:29 CEST)
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.
symfnames
)symfnames
)
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:
Stream
is an open stream.
(streams_basic:stream/1
)
File
is any term.
(basic_props:term/1
)
Mode
is an opening mode ('read', 'write' or 'append').
(streams_basic:io_mode/1
)
Stream
is an open stream.
(streams_basic:stream/1
)
symfnames
)
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.
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.
symfnames
)
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
.
Go to the first, previous, next, last section, table of contents.