Proposal for std.path replacement
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Mon Mar 7 04:08:57 PST 2011
On Mon, 07 Mar 2011 10:25:21 +0000, Regan Heath wrote:
> On Sun, 06 Mar 2011 08:37:15 -0000, Rainer Schuetze <r.sagitario at gmx.de>
> wrote:
>
>> Looks good overall. I have a few comments and nitpicks though:
>>
>> > basename("dir/subdir/") --> "subdir"
>> > directory("dir/subdir/") --> "dir"
>>
>> Is this what everybody expects? I'm not sure, but another possibility
>> would be to treat these as if "dir/subdir/." is passed. What is the
>> result of directory("/") or directory("d:/")?
>
> ?? I would expect:
>
> directory("dir/subdir/") --> "dir/subdir"
>
> as subdir _is_ a dir, not a file, as shown by the trailing slash. If it
> was:
>
> directory("dir/subdir") --> "dir"
>
> as subdir is perhaps not a directory, as there is no trailing slash.
>
> I realise this means the trailing slash becomes important, but it kinda
> is important as it does tell us when something is definitely a
> directory.
I don't think it does, or rather, I don't think there is such a thing as
"definitely a directory". What about a symlink to a directory, for
instance? On one hand, it *is* a file that contains a reference to a
directory, and on the other, in most respects it *acts like* a directory.
You can even argue that a "file" is simply the term used for a node in
the filesystem tree, and that "directory" is a special kind of file that
contains a list of other files. This terminology is pretty standard in
*NIX land, at least. (Just google "everything is a file".)
> Alternately, we could ignore the distinction between file and directory
> - as we're essentially just parsing strings here - and have two
> functions:
>
> lastComponent("dir/subdir/") -> "subdir" lastComponent("dir/subdir")
> -> "subdir"
>
> allButLastComponent("dir/subdir/") -> "dir/"
> allButLastComponent("dir/subdir") -> "dir/"
That's how it's done now, and how I think it should be. The two paths
"dir/subdir" and "dir/subdir/" both refer to the same object in the file
system, namely "subdir". baseName gives you the name of the object
referred to by a path, while dirName gives you the directory containing
said object. Whether that object is a file or a directory is
irrelevant. (And if you need to know what it is, there is always
std.file.isDir and isFile.)
-Lars
More information about the Digitalmars-d
mailing list