Next: , Previous: Various utilities, Up: System features


4.1.3 Filenames

There are some basic filename manipulation facilities exported by the filenames structure.1

— constant: *scheme-file-type* –> symbol
— constant: *load-file-type* –> symbol

*Scheme-file-type* is a symbol denoting the file extension that Scheme48 assumes for Scheme source files; any other extension, for instance in the filename list of a structure definition, must be written explicitly. *Load-file-type* is a symbol denoting the preferable file extension to load files from. (*Load-file-type* was used mostly in bootstrapping Scheme48 from Pseudoscheme or T long ago and is no longer very useful.)

— procedure: file-name-directory filename –> string
— procedure: file-name-nondirectory filename –> string

File-name-directory returns the directory component of the filename denoted by the string filename, including a trailing separator (on Unix, /). File-name-nondirectory returns everything but the directory component of the filename denoted by the string filename, including the extension.

          (file-name-directory "/usr/local/lib/scheme48/scheme48.image")
              => "/usr/local/lib/scheme48/"
          (file-name-nondirectory "/usr/local/lib/scheme48/scheme48.image")
              => "scheme48.image"
          (file-name-directory "scheme48.image")
              => ""
          (file-name-nondirectory "scheme48.image")
              => "scheme48.image"

Namelists are platform-independent means by which to name files. They are represented as readable S-expressions of any of the following forms:

basename
represents a filename with only a basename and no directory or file type/extension;
(directory basename [type])
represents a filename with a single preceding directory component and an optional file type/extension; and
((directory ...) basename [type])
represents a filename with a sequence of directory components, a basename, and an optional file type/extension.

Each atomic component — that is, the basename, the type/extension, and each individual directory component — may be either a string or a symbol. Symbols are converted to the canonical case of the host operating system by namestring (on Unix, lowercase); the case of string components is not touched.

— procedure: namestring namelist directory default-type –> string

Converts namelist to a string in the format required by the host operating system.2 If namelist did not have a directory component, directory, a string in the underlying operating system's format for directory prefixes, is added to the resulting namestring; and, if namelist did not have a type/extension, default-type, which may be a string or a symbol and which should not already contain the host operating system's delimiter (usually a dot), is appended to the resulting namestring.

Directory or default-type may be #f, in which case they are not prefixed or appended to the resulting filename.

          (namestring 'foo #f #f)                 => "foo"
          (namestring 'foo "bar" 'baz)            => "bar/foo.baz"
          (namestring '(rts defenum) "scheme" 'scm)
              => "scheme/rts/defenum.scm"
          (namestring '((foo bar) baz quux) "zot" #f)
              => "zot/foo/bar/baz.quux"
          (namestring "zot/foo/bar/baz.quux" #f "mumble")
              => "zot/foo/bar/baz.quux.mumble"
4.1.3.1 Filename translations

Scheme48 keeps a registry of filename translations, translations from filename prefixes to the real prefixes. This allows abstraction of actual directory prefixes without necessitating running Scheme code to construct directory pathnames (for example, in configuration files).

— procedure: translations –> string/string-alist

Returns the alist of filename translations.

— procedure: set-translation! from to –> unspecified

Adds a filename prefix translation, overwriting an existing one if one already existed.

— procedure: translate filename –> translated-filename

Translates the first prefix of filename found in the registry of translations and returns the translated filename.

     (set-translation! "s48" "/home/me/scheme/scheme48/scheme")
     (translate (namestring '(bcomp frame) "s48" 'scm))
         => "/home/me/scheme/scheme48/scheme/bcomp/frame.scm"
     (translate (namestring "comp-packages" "s48" 'scm))
         => "/home/me/scheme/scheme48/scheme/comp-packages.scm"
     (translate "s48/frobozz")
         => "/home/me/scheme/scheme48/scheme/frobozz"
     (set-translation! "scheme48" "s48")
     (translate (namestring '((scheme48 big) filename) #f 'scm))
         => scheme48/big/filename.scm
     (translate (translate (namestring '((scheme48 big) filename) #f 'scm)))
         => "/home/me/scheme/scheme48/scheme/big/filename.scm"

One filename translation is built-in, mapping =scheme48/ to the directory of system files in a Scheme48 installation, which on Unix is typically a directory in /usr/local/lib.

     (translate "=scheme48/scheme48.image")
         => /usr/local/scheme48/scheme48.image

Footnotes

[1] The facilities Scheme48 provides are very rudimentary, and they are not intended to act as a coherent and comprehensive pathname or logical name facility such as that of Common Lisp. However, they served the basic needs of Scheme48's build process when they were originally created.

[2] However, the current standard distribution of Scheme48 is specific to Unix: the current code implements only Unix filename facilities.