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.  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
A package to manage file access across platforms.
Takes a path and returns a logically simplified version of it.
Joins two paths.
Takes two paths and returns a relative path between them.
Takes a relative path and converts it from the native format to a relative URL.
Takes an array of directory entries and returns one without all the entries that refer to the parent directory, such as ‘.’
Takes a path and returns a version without the file name.
Creates a directory tree corresponding to the passed path, regardless of how many directories do or do not already exist.
Copies a file from one path to another.
Takes a string of directories and returns an array of its elements.

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. 

JoinPath

sub JoinPath #(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. 

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?

Wow, where to begin?  First of all, there’s nothing that gives a relative path between two relative paths. 

Second of all, 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 of all, 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 fucking 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.  Morons. 

Update: This last one appears to be fixed in File::Spec 0.83, but that version isn’t even listed on CPAN.  Lovely.  Apparently it just comes with ActivePerl.  Somehow I don’t think most Linux users are using that. 

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()

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. 

Copy

sub Copy #(source, destination)

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. 

Support Functions

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.  Morons.