Contents | Parent Topic | Previous Topic | Next Topic
Home | Catalog

# 2.4. name.pl - Name Processing Library

## 2.4.1. Purpose

This library provides support for file-name processing. With careful use, the routines provide portability across Unix-like and Windows-like file-naming systems.

## 2.4.2. Interface

    require "name.pl";

    $NAME_OS = ...

    $NAME_OS = ...

    %NAME_DIR_TABLE = ...

    %NAME_PATH_TABLE = ...

    $NAME_DIR_SEP = ...

    $NAME_PATH_SEP = ...

    &NameOS();

    $result =
    &NameIsAbsolute($name);

    $result =
    &NameAbsolute($name);

    $found_name =
    &NameFind($name, @dirs);

    ($dir, $base, $ext, $short) =
    &NameSplit($name);

    &NamePathComponentSplit();

    $name =
    &NameJoin($dir, $base, $ext);

    $new_name =
    &NameSubExt($name, $new_ext);

    &NameLoadConversionRules(*table, $validate);

    $full =
    &NameFindOrGenerate($name, $dir_list_ref, $ext_list_ref, $context);

    $full =
    &NameFindInDirectory($dir, $base, $ext_list_ref, $context);

## 2.4.3. Description

NAME_OS returns the current operating system style, either unix or dos. NAME_DIR_TABLE and NAME_PATH_TABLE are lookup tables of directory and path separators for different operating system styles. NAME_DIR_SEP and NAME_PATH_SEP are the respective separators for NAME_OS.

NameOS returns the SDF version of the OS name.

NameIsAbsolute returns 1 if the name is in absolute (or non-relative) format.

NameAbsolute returns the absolute name for a file.

NameFind searches the directories for a file with the name given. If found, the combined name (directory + local name) is returned. If the name is absolute, the file is checked to exist. i.e. the directories are not searched. In either case, if the file is not found, an empty string is returned.

NameSplit extracts components from a name. short is the name without the directory.

NamePathComponentSplit completely splits a path into its component parts. Returns a list of the parts.

NameJoin builds a name from its components. If the base name is already absolute, the directory is not prepended.

NameSubExt substitutes the extension on a name.

NameLoadConversionRules loads a table of conversion rules to be used by NameFindOrGenerate. The fields in @table are:

- Context - the driver for which this conversion applies
- To - the destination figure format
- From - the original figure format
- Action - the command to use to do the conversion.

Rules do not chain, so defining rules for A->B and B->C do not imply that A will be converted to C. If validate is set, the table is validated.

NameFindOrGenerate searches a list of directories for a file with one of the list of extensions. The extensions are searched for in the order given. If NameLoadConversionRules has been called, this routine will attempt to generate a file in the current directory using the nominated context, if any. If a file was found or generated, the combined name (directory + local name) is returned. If the name is absolute, the file is checked to exist. i.e. the directories are not searched. In either case, if the file is not found, an empty string is returned.

NameFindInDirectory attempts to find a file directory dir using base and the set of extensions given by $ext_list_ref. For each base.ext combination, if it doesn't find that file, it tries to generate a file of that name in the current directory using:

- the conversion rules loaded by NameLoadConversionRules
- the files called base.* in the dir directory
- the context

If the file is found or generated, its name is returned, otherwise an empty string is returned.

Note: If the base already has an extension, the extension list isn't used.