Section9.25

]>
Book Contents Previous Section 9.24 File Next Section 9.26 FlexibleArray Book Index

## 9.25 FileName

The 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 Axiom interpreter is to rely on conversion to and from strings. The syntax of these strings depends on the operating system.

fn: FileName
Type: Void

On Linux, this is a proper file syntax:

fn := "/tmp/fname.input"

"/tmp/fname.input"

Type: FileName

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.

A measure of portability is obtained by considering a file name to consist of three parts: the directory, the name, and the extension.

directory fn

"/tmp"

Type: String
name fn

"fname"

Type: String
extension fn

"input"

Type: String

The meaning of these three parts depends on the operating system. For example, on CMS the file ``SPADPROF INPUT M'' would have directory ``M'', name ``SPADPROF'' and extension ``INPUT''. It is possible to create a filename from its parts.

fn := filename("/u/smwatt/work", "fname", "input")

"/u/smwatt/work/fname.input"

Type: FileName

When writing programs, it is helpful to refer to directories via variables.

objdir := "/tmp"

"/tmp"

Type: String
fn := filename(objdir, "table", "spad")

"/tmp/table.spad"

Type: FileName

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.

fn := filename("", "letter", "")

"letter"

Type: FileName

Three tests provide information about names in the file system.

The exists? exists? FileName operation tests whether the named file exists.

exists? "/etc/passwd"

true

Type: Boolean

The operation readable? readable? FileName tells whether the named file can be read. If the file does not exist, then it cannot be read.

readable? "/etc/passwd"

true

Type: Boolean
readable? "/etc/security/passwd"

false

Type: Boolean
readable? "/ect/passwd"

false

Type: Boolean

Likewise, the operation writable? 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.

writable? "/etc/passwd"

false

Type: Boolean
writable? "/dev/null"

true

Type: Boolean
writable? "/etc/DoesNotExist"

false

Type: Boolean
writable? "/tmp/DoesNotExist"

true

Type: Boolean

The newnewFileName operation constructs the name of a new writable file. The argument sequence is the same as for filenamefilenameFileName, except that the name part is actually a prefix for a constructed unique name.

The resulting file is in the specified directory with the given extension, and the same defaults are used.

fn := new(objdir, "xxx", "yy")

"/tmp/xxx82404.yy"

Type: FileName
Book Contents Previous Section 9.24 File Next Section 9.26 FlexibleArray Book Index