NaturalDocs::File

A package to manage file access across platforms.  Incorporates functions from various standard File:: packages, but more importantly, works around the glorious suckage present in File::Spec, at least in version 0.82 and earlier.  Read the “Why oh why?” sections for why this package was necessary.

Usage and Dependencies

  • The package doesn’t depend on any other Natural Docs packages and is ready to use immediately.
  • All functions except CanonizePath() assume that all parameters are canonized.
Summary
NaturalDocs::FileA package to manage file access across platforms.
CheckCompatibilityChecks if the standard packages required by this one are up to snuff and dies if they aren’t.
Path String Functions
CanonizePathTakes a path and returns a logically simplified version of it.
PathIsAbsoluteReturns whether the passed path is absolute.
JoinPathCreates a path from its elements.
JoinPathsJoins two paths.
SplitPathTakes a path and returns its elements.
JoinDirectoriesCreates a directory string from an array of directory names.
SplitDirectoriesTakes a string of directories and returns an array of its elements.
MakeRelativePathTakes two paths and returns a relative path between them.
IsSubPathOfReturns whether the path is a descendant of another path.
ConvertToURLTakes a relative path and converts it from the native format to a relative URL.
NoUpwardsTakes an array of directory entries and returns one without all the entries that refer to the parent directory, such as ‘.’
NoFileNameTakes a path and returns a version without the file name.
NoExtensionReturns the path without an extension.
ExtensionOfReturns the extension of the passed path, or undef if none.
IsCaseSensitiveReturns whether the current platform has case-sensitive paths.
Disk Functions
CreatePathCreates a directory tree corresponding to the passed path, regardless of how many directories do or do not already exist.
RemoveEmptyTreeRemoves an empty directory tree.
CopyCopies a file from one path to another.

CheckCompatibility

sub CheckCompatibility

Checks if the standard packages required by this one are up to snuff and dies if they aren’t.  This is done because I can’t tell which versions of File::Spec have splitpath just by the version numbers.

Path String Functions

CanonizePath

sub CanonizePath #(path)

Takes a path and returns a logically simplified version of it.

Why oh why?

Because File::Spec->canonpath doesn’t strip quotes on Windows.  So if you pass in “a b\c” or “a b”\c, they still end up as different strings even though they’re logically the same.

It also doesn’t remove things like “..”, so “a/b/../c” doesn’t simplify to “a/c” like it should.

PathIsAbsolute

sub PathIsAbsolute #(path)

Returns whether the passed path is absolute.

JoinPath

sub JoinPath #(volume,
dirString,
$file)

Creates a path from its elements.

Parameters

volumeThe volume, such as the drive letter on Windows.  Undef if none.
dirStringThe directory string.  Create with JoinDirectories() if necessary.
fileThe file name, or undef if none.

Returns

The joined path.

JoinPaths

sub JoinPaths #(basePath,
extraPath,
noFileInExtra)

Joins two paths.

Parameters

basePathMay be a relative path, an absolute path, or undef.
extraPathMay be a relative path, a file, a relative path and file together, or undef.
noFileInExtraSet this to true if extraPath is a relative path only, and doesn’t have a file.

Returns

The joined path.

Why oh why?

Because nothing in File::Spec will simply slap two paths together.  They have to be split up for catpath/file, and rel2abs requires the base to be absolute.

SplitPath

sub SplitPath #(path,
noFile)

Takes a path and returns its elements.

Parameters

pathThe path to split.
noFileSet to true if the path doesn’t have a file at the end.

Returns

The array ( volume, directoryString, file ).  If any don’t apply, they will be undef.  Use SplitDirectories() to split the directory string if desired.

Why oh Why?

Because File::Spec->splitpath may leave a trailing slash/backslash/whatever on the directory string, which makes it a bit hard to match it with results from File::Spec->catdir.

JoinDirectories

sub JoinDirectories #(directory,
directory,
...)

Creates a directory string from an array of directory names.

Parameters

directoryA directory name.  There may be as many of these as desired.

SplitDirectories

sub SplitDirectories #(directoryString)

Takes a string of directories and returns an array of its elements.

Why oh why?

Because File::Spec->splitdir might leave an empty element at the end of the array, which screws up both joining in ConvertToURL and navigation in MakeRelativePath.

MakeRelativePath

sub MakeRelativePath #(basePath,
targetPath)

Takes two paths and returns a relative path between them.

Parameters

basePathThe starting path.  May be relative or absolute, so long as the target path is as well.
targetPathThe target path.  May be relative or absolute, so long as the base path is as well.

If both paths are relative, they are assumed to be relative to the same base.

Returns

The target path relative to base.

Why oh why?

First, there’s nothing that gives a relative path between two relative paths.

Second, if target and base are absolute but on different volumes, File::Spec->abs2rel creates a totally non-functional relative path.  It should return the target as is, since there is no relative path.

Third, File::Spec->abs2rel between absolute paths on the same volume, at least on Windows, leaves the drive letter on.  So abs2rel(‘a:\b\c\d’, ‘a:\b’) returns ‘a:c\d’ instead of the expected ‘c\d’.  That makes no sense whatsoever.  It’s not like it was designed to handle only directory names, either; the documentation says ‘path’ and the code seems to explicitly handle it.  There’s just an ‘unless’ in there that tacks on the volume, defeating the purpose of a relative path and making the function worthless.

IsSubPathOf

sub IsSubPathOf #(base,
path)

Returns whether the path is a descendant of another path.

Parameters

baseThe base path to test against.
pathThe possible subpath to test.

Returns

Whether path is a descendant of base.

ConvertToURL

sub ConvertToURL #(path)

Takes a relative path and converts it from the native format to a relative URL.  Note that it doesn’t convert special characters to amp chars.

NoUpwards

sub NoUpwards #(array)

Takes an array of directory entries and returns one without all the entries that refer to the parent directory, such as ‘.’ and ‘..’.

NoFileName

sub NoFileName #(path)

Takes a path and returns a version without the file name.  Useful for sending paths to CreatePath().

NoExtension

sub NoExtension #(path)

Returns the path without an extension.

ExtensionOf

sub ExtensionOf #(path)

Returns the extension of the passed path, or undef if none.

IsCaseSensitive

sub IsCaseSensitive

Returns whether the current platform has case-sensitive paths.

Disk Functions

CreatePath

sub CreatePath #(path)

Creates a directory tree corresponding to the passed path, regardless of how many directories do or do not already exist.  Do not include a file name in the path.  Use NoFileName() first if you need to.

RemoveEmptyTree

sub RemoveEmptyTree #(path,
limit)

Removes an empty directory tree.  The passed directory will be removed if it’s empty, and it will keep removing its parents until it reaches one that’s not empty or a set limit.

Parameters

pathThe path to start from.  It will try to remove this directory and work it’s way down.
limitThe path to stop at if it doesn’t find any non-empty directories first.  This path will not be removed.

Copy

sub Copy #(source,
destination) => bool

Copies a file from one path to another.  If the destination file exists, it is overwritten.

Parameters

sourceThe file to copy.
destinationThe destination to copy to.

Returns

Whether it succeeded

sub CheckCompatibility
Checks if the standard packages required by this one are up to snuff and dies if they aren’t.
sub CanonizePath #(path)
Takes a path and returns a logically simplified version of it.
sub PathIsAbsolute #(path)
Returns whether the passed path is absolute.
sub JoinPath #(volume,
dirString,
$file)
Creates a path from its elements.
sub JoinPaths #(basePath,
extraPath,
noFileInExtra)
Joins two paths.
sub SplitPath #(path,
noFile)
Takes a path and returns its elements.
sub JoinDirectories #(directory,
directory,
...)
Creates a directory string from an array of directory names.
sub SplitDirectories #(directoryString)
Takes a string of directories and returns an array of its elements.
sub MakeRelativePath #(basePath,
targetPath)
Takes two paths and returns a relative path between them.
sub IsSubPathOf #(base,
path)
Returns whether the path is a descendant of another path.
sub ConvertToURL #(path)
Takes a relative path and converts it from the native format to a relative URL.
sub NoUpwards #(array)
Takes an array of directory entries and returns one without all the entries that refer to the parent directory, such as ‘.’
sub NoFileName #(path)
Takes a path and returns a version without the file name.
sub NoExtension #(path)
Returns the path without an extension.
sub ExtensionOf #(path)
Returns the extension of the passed path, or undef if none.
sub IsCaseSensitive
Returns whether the current platform has case-sensitive paths.
sub CreatePath #(path)
Creates a directory tree corresponding to the passed path, regardless of how many directories do or do not already exist.
sub RemoveEmptyTree #(path,
limit)
Removes an empty directory tree.
sub Copy #(source,
destination) => bool
Copies a file from one path to another.
Close