
% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     - Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%
%     - Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the
%       distribution.
%
%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



The \spadtype{FileName} domain provides an interface to the computer's
file system.
Functions are provided to manipulate file names and to test properties of
files.

The simplest way to use file names in the \Language{} interpreter is to
rely on conversion to and from strings.
The syntax of these strings depends on the operating system.
\xtc{
}{
\spadcommand{fn: FileName \bound{fndecl}}
}
\xtc{
On AIX, this is a proper file syntax:
}{
\spadcommand{fn := "/spad/src/input/fname.input" \free{fndecl}\bound{fn}}
}

Although it is very convenient to be able to use string notation
for file names in the interpreter, it is desirable to have a portable
way of creating and manipulating file names from within programs.
\xtc{
A measure of portability is obtained by considering a file name
to consist of three parts: the {\it directory}, the {\it name},
and the {\it extension}.
}{
\spadcommand{directory fn \free{fn}}
}
\xtc{
}{
\spadcommand{name fn \free{fn}}
}
\xtc{
}{
\spadcommand{extension fn \free{fn}}
}
The meaning of these three parts depends on the operating system.
For example, on CMS the file \spad{"SPADPROF INPUT M"}
would have directory \spad{"M"}, name \spad{"SPADPROF"} and
extension \spad{"INPUT"}.

\xtc{
It is possible to create a filename from its parts.
}{
\spadcommand{fn := filename("/u/smwatt/work", "fname", "input") \free{fndecl}\bound{fn1}}
}
\xtc{
When writing programs, it is helpful to refer to directories via
variables.
}{
\spadcommand{objdir := "/tmp" \bound{objdir}}
}
\xtc{
}{
\spadcommand{fn := filename(objdir, "table", "spad") \free{fndecl,objdir}\bound{fn2}}
}
\xtc{
If the directory or the extension is given as an empty string, then
a default is used.  On AIX, the defaults are the current directory
and no extension.
}{
\spadcommand{fn := filename("", "letter", "") \free{fndecl}\bound{fn3}}
}

Three tests provide information about names in the file system.
\xtc{
The \spadfunFrom{exists?}{FileName} operation tests whether the named file exists.
}{
\spadcommand{exists? "/etc/passwd"}
}
\xtc{
The operation \spadfunFrom{readable?}{FileName} tells whether the named file
can be read.  If the file does not exist, then it cannot be read.
}{
\spadcommand{readable? "/etc/passwd"}
}
\xtc{
}{
\spadcommand{readable? "/etc/security/passwd"}
}
\xtc{
}{
\spadcommand{readable? "/ect/passwd"}
}
\xtc{
Likewise, the operation \spadfunFrom{writable?}{FileName} tells whether the named file
can be written.
If the file does not exist, the test is determined
by the properties of the directory.
}{
\spadcommand{writable? "/etc/passwd"}
}
\xtc{
}{
\spadcommand{writable? "/dev/null"}
}
\xtc{
}{
\spadcommand{writable? "/etc/DoesNotExist"}
}
\xtc{
}{
\spadcommand{writable? "/tmp/DoesNotExist"}
}

The \spadfunFrom{new}{FileName} operation constructs the name of a new
writable file.
The argument sequence is the same as for \spadfunFrom{filename}{FileName},
except that the name part is actually a prefix for a constructed
unique name.
\xtc{
The resulting file is in the specified directory
with the given extension, and the same defaults are used.
}{
\spadcommand{fn := new(objdir, "xxx", "yy") \free{objdir,fndecl}\bound{fn4}}
}
