1% Copyright The Numerical Algorithms Group Limited 1992-94. All rights reserved.
2% !! DO NOT MODIFY THIS FILE BY HAND !! Created by ht.awk.
3\newcommand{\FileNameXmpTitle}{FileName}
4\newcommand{\FileNameXmpNumber}{9.25}
5%
6% =====================================================================
7\begin{page}{FileNameXmpPage}{9.25 FileName}
8% =====================================================================
9\beginscroll
10
11%
12%
13%
14%
15%
16
17
18
19The \spadtype{FileName} domain provides an interface to the computer's
20file system.
21Functions are provided to manipulate file names and to test properties of
22files.
23
24The simplest way to use file names in the \Language{} interpreter is to
25rely on conversion to and from strings.
26The syntax of these strings depends on the operating system.
27\xtc{
28}{
29\spadpaste{fn: FileName \bound{fndecl}}
30}
31\xtc{
32On AIX, this is a proper file syntax:
33}{
34\spadpaste{fn := "/spad/src/input/fname.input" \free{fndecl}\bound{fn}}
35}
36
37Although it is very convenient to be able to use string notation
38for file names in the interpreter, it is desirable to have a portable
39way of creating and manipulating file names from within programs.
40\xtc{
41A measure of portability is obtained by considering a file name
42to consist of three parts: the {\it directory}, the {\it name},
43and the {\it extension}.
44}{
45\spadpaste{directory fn \free{fn}}
46}
47\xtc{
48}{
49\spadpaste{name fn \free{fn}}
50}
51\xtc{
52}{
53\spadpaste{extension fn \free{fn}}
54}
55The meaning of these three parts depends on the operating system.
56For example, on CMS the file \spad{"SPADPROF INPUT M"}
57would have directory \spad{"M"}, name \spad{"SPADPROF"} and
58extension \spad{"INPUT"}.
59
60\xtc{
61It is possible to create a filename from its parts.
62}{
63\spadpaste{fn := filename("/u/smwatt/work", "fname", "input") \free{fndecl}\bound{fn1}}
64}
65\xtc{
66When writing programs, it is helpful to refer to directories via
67variables.
68}{
69\spadpaste{objdir := "/tmp" \bound{objdir}}
70}
71\xtc{
72}{
73\spadpaste{fn := filename(objdir, "table", "spad") \free{fndecl,objdir}\bound{fn2}}
74}
75\xtc{
76If the directory or the extension is given as an empty string, then
77a default is used.  On AIX, the defaults are the current directory
78and no extension.
79}{
80\spadpaste{fn := filename("", "letter", "") \free{fndecl}\bound{fn3}}
81}
82
83Three tests provide information about names in the file system.
84\xtc{
85The \spadfunFrom{exists?}{FileName} operation tests whether the named file exists.
86}{
87\spadpaste{exists? "/etc/passwd"}
88}
89\xtc{
90The operation \spadfunFrom{readable?}{FileName} tells whether the named file
91can be read.  If the file does not exist, then it cannot be read.
92}{
93\spadpaste{readable? "/etc/passwd"}
94}
95\xtc{
96}{
97\spadpaste{readable? "/etc/security/passwd"}
98}
99\xtc{
100}{
101\spadpaste{readable? "/etc/passwd"}
102}
103\xtc{
104Likewise, the operation \spadfunFrom{writable?}{FileName} tells whether the named file
105can be written.
106If the file does not exist, the test is determined
107by the properties of the directory.
108}{
109\spadpaste{writable? "/etc/passwd"}
110}
111\xtc{
112}{
113\spadpaste{writable? "/dev/null"}
114}
115\xtc{
116}{
117\spadpaste{writable? "/etc/DoesNotExist"}
118}
119\xtc{
120}{
121\spadpaste{writable? "/tmp/DoesNotExist"}
122}
123
124The \spadfunFrom{new}{FileName} operation constructs the name of a new
125writable file.
126The argument sequence is the same as for \spadfunFrom{filename}{FileName},
127except that the name part is actually a prefix for a constructed
128unique name.
129\xtc{
130The resulting file is in the specified directory
131with the given extension, and the same defaults are used.
132}{
133\spadpaste{fn := new(objdir, "xxx", "yy") \free{objdir,fndecl}\bound{fn4}}
134}
135\endscroll
136\autobuttons
137\end{page}
138%
139