## digitalmars.D.learn - another std.path problem: getBaseName

Greetings!

I already have a problem with std.path.getDirName, but that's another on-going
post.  However, what is the use of getBaseName?  Shouldn't it be getting the
last entry of a path?

Take a look at this piece of code:

import std.stdio;
import std.path;
void main ()
{
char[] dir0 = "c:\\this\\is\\a\\path\\long.pog";
char[] dir1 = "c:\\this\\is\\also\\a\\path\\";
writefln(std.path.getBaseName(dir0));
writefln(std.path.getBaseName(dir1));
}

when run, it outputs:

long.pog

And as you can see, the second line is empty.  That is wrong.  There is no such
a path as a blank or null value.  Yes, if I take the \ from the end it works.
However, this should be taken care by the function.  I should not need to do any
cleanup for a function when I have a legitimate path.  This also goes for
getDirName.  It should return the last directory name in a path.  Even though it
is a directory.  Otherwise, confusing outcomes will be the result.

thanks,

josé
Jul 19 2005
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"jicman" <jicman_member pathlink.com> wrote in message
news:dbjis4$iet$1 digitaldaemon.com...
I already have a problem with std.path.getDirName, but that's another
on-going
post.  However, what is the use of getBaseName?  Shouldn't it be getting
the
last entry of a path?

Maybe it's just a badly-named function. Seems to me that it should be called getFileName(), not getBaseName(). Here. Have this. char[] getLastPathElement(char[] path) { if(path.length==0) return null; if(path[length-1]=='\\' || path[length-1]=='/') path.length=path.length-1; return getBaseName(path); }
Take a look at this piece of code:

import std.stdio;
import std.path;
void main ()
{
char[] dir0 = "c:\\this\\is\\a\\path\\long.pog";
char[] dir1 = "c:\\this\\is\\also\\a\\path\\";
writefln(std.path.getBaseName(dir0));
writefln(std.path.getBaseName(dir1));
}

when run, it outputs:

long.pog

And as you can see, the second line is empty.  That is wrong.

If the name of the function were getFileName (which it should be), then no, it's not wrong.
There is no such
a path as a blank or null value.  Yes, if I take the \ from the end it
works.

That's because the function then thinks that "path" is the filename, and returns that.
However, this should be taken care by the function.  I should not need to
do any
cleanup for a function when I have a legitimate path.  This also goes for
getDirName.  It should return the last directory name in a path.  Even
though it
is a directory.  Otherwise, confusing outcomes will be the result.

What do you mean? getDirName functions just fine.
Jul 19 2005
Jarrett Billingsley says...
"jicman" <jicman_member pathlink.com> wrote in message
news:dbjis4$iet$1 digitaldaemon.com...
I already have a problem with std.path.getDirName, but that's another
on-going
post.  However, what is the use of getBaseName?  Shouldn't it be getting
the
last entry of a path?

Maybe it's just a badly-named function. Seems to me that it should be called getFileName(), not getBaseName().

Ok, I guess I saw getBaseName and I thought it would always return the last item of a path. My mistake.
Here.  Have this.

char[] getLastPathElement(char[] path)
{
if(path.length==0)
return null;
if(path[length-1]=='\\' || path[length-1]=='/')
path.length=path.length-1;
return getBaseName(path);
}

Yep, already did it, except that I splitted to an array and use the last entry of the array. :-) Yours is too hard to read. ;-) Just kidding.
Take a look at this piece of code:

import std.stdio;
import std.path;
void main ()
{
char[] dir0 = "c:\\this\\is\\a\\path\\long.pog";
char[] dir1 = "c:\\this\\is\\also\\a\\path\\";
writefln(std.path.getBaseName(dir0));
writefln(std.path.getBaseName(dir1));
}

when run, it outputs:

long.pog

And as you can see, the second line is empty.  That is wrong.

If the name of the function were getFileName (which it should be), then no, it's not wrong.

Right. Again, the lack of documentation and the few hours trying to find a bug was what caused my complaining. My apologies.
There is no such
a path as a blank or null value.  Yes, if I take the \ from the end it
works.

That's because the function then thinks that "path" is the filename, and returns that.

Well, that would have worked the way I wanted it to work, but it does not. "path" does not get return. Instead, null value is returned. The other question is, what if that "path" is a file without a .txt or a .* ending?
However, this should be taken care by the function.  I should not need to
do any
cleanup for a function when I have a legitimate path.  This also goes for
getDirName.  It should return the last directory name in a path.  Even
though it
is a directory.  Otherwise, confusing outcomes will be the result.

What do you mean? getDirName functions just fine.

Well, again, lack of documentation and expecting it to work like JScript was my problem. For example, var fso, s = ""; var w = WScript; fso = new ActiveXObject("Scripting.FileSystemObject"); w.Echo(fso.GetBaseName("c:\\logs\\JICW2klog")); if you run it, with this command, cscript filetest.js you will get, Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. JICW2klog Also, take a look at this D program, import std.stdio; import std.path; import std.file; void main () { char[] dir1 = "c:\\this\\is\\a\\path\\"; char[] dir2 = "c:\\this\\is\\also\\a\\path"; char[] dir3 = "c:\\this\\is\\another\\path\\file.txt"; writefln(std.path.getDirName(dir1)); writefln(std.path.getDirName(dir2)); writefln(std.path.getDirName(dir3)); } if you compile it and run it, you'll get, c:\this\is\a\path c:\this\is\also\a c:\this\is\another\path Which is not consistent, but anyway, I don't mean to go on. I apologize. thanks and sorry, josé
Jul 19 2005
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"jicman" <jicman_member pathlink.com> wrote in message
news:dbkf7j$19h2$1 digitaldaemon.com...
Also, take a look at this D program,

import std.stdio;
import std.path;
import std.file;
void main ()
{
char[] dir1 = "c:\\this\\is\\a\\path\\";
char[] dir2 = "c:\\this\\is\\also\\a\\path";
char[] dir3 = "c:\\this\\is\\another\\path\\file.txt";
writefln(std.path.getDirName(dir1));
writefln(std.path.getDirName(dir2));
writefln(std.path.getDirName(dir3));
}

if you compile it and run it, you'll get,

c:\this\is\a\path
c:\this\is\also\a
c:\this\is\another\path

Which is not consistent, but anyway, I don't mean to go on.  I apologize.

Umm, yes, it actually is consistent! The getDirName() function starts at the end of the string and searches for the first / or \ it finds. Then it returns the slice of the string from the beginning until one before that character. dir1 has a \ at the end. So the function returns immediately. With dir2, "path" is _not_ part of the path, as far as the function is concerned. It is a filename. It will go back to the after "a", and return up to that. dir3 is the same situation as dir2. Remember, these functions are just string manip! No file system checking! If they did do file system checking, you wouldn't even be able to do this: if(std.file.exists(std.path.join(std.file.getcwd(),"blah.txt"))) ... Or: char[] newname=std.path.join(std.file.getcwd(), "something.bmp"); File f=new File(newname, FileMode.Out);
Jul 20 2005
In article <dblnfi$7el$1 digitaldaemon.com>, Jarrett Billingsley says...
"jicman" <jicman_member pathlink.com> wrote in message
news:dbkf7j$19h2$1 digitaldaemon.com...
Also, take a look at this D program,

import std.stdio;
import std.path;
import std.file;
void main ()
{
char[] dir1 = "c:\\this\\is\\a\\path\\";
char[] dir2 = "c:\\this\\is\\also\\a\\path";
char[] dir3 = "c:\\this\\is\\another\\path\\file.txt";
writefln(std.path.getDirName(dir1));
writefln(std.path.getDirName(dir2));
writefln(std.path.getDirName(dir3));
}

if you compile it and run it, you'll get,

c:\this\is\a\path
c:\this\is\also\a
c:\this\is\another\path

Which is not consistent, but anyway, I don't mean to go on.  I apologize.

Umm, yes, it actually is consistent!

It may be consistent with the search and split logic, but not consistent with the directory structure. This entry, c:\path\to\file\ is the same as c:\path\to\file my friend. There is no "" directory. If you run these two same "directory structure" examples with getDirName(), you'll get a different outcome. I am no longer talking about checking if it's file or directory. I am now talking just plain directory structure definition.
The getDirName() function starts at
the end of the string and searches for the first / or \ it finds.  Then it
returns the slice of the string from the beginning until one before that
character.

last character is a \ and make an appropiate "directory structure" result.
dir1 has a \ at the end.  So the function returns immediately.

With dir2, "path" is _not_ part of the path, as far as the function is
concerned.  It is a filename.  It will go back to the  after "a", and return
up to that.

dir3 is the same situation as dir2.

Remember, these functions are just string manip!  No file system checking!
If they did do file system checking, you wouldn't even be able to do this:

if(std.file.exists(std.path.join(std.file.getcwd(),"blah.txt")))
...

Or:

char[] newname=std.path.join(std.file.getcwd(), "something.bmp");
File f=new File(newname, FileMode.Out);

I understand. Thanks. jic
Jul 20 2005
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"jicman" <jicman_member pathlink.com> wrote in message
news:dbmdkb$rei$1 digitaldaemon.com...
It may be consistent with the search and split logic, but not consistent
with
the directory structure.  This entry,

c:\path\to\file\

is the same as

c:\path\to\file

my friend.

The first is nothing but a pathname. The second is a directory called "c:\path\to\" with a filename of "file". What exactly are you finding so difficult about this?
There is no "" directory.  If you run these two same "directory
structure" examples with getDirName(), you'll get a different outcome.  I
am no
longer talking about checking if it's file or directory.  I am now talking
just
plain directory structure definition.

Yes, you'll get a different outcome, because they _are_ different. In "directory structure definition", directories end in a \. Full filenames end with a letter. End.
Yes, which is exactly what is going on here.  However, it should check if
the
last character is a \ and make an appropiate "directory structure" result.

It does!
I understand.  Thanks.

But.. but.. you're still arguing that you don't!
Jul 20 2005
Jarrett Billingsley says...
"jicman" <jicman_member pathlink.com> wrote in message
news:dbmdkb$rei$1 digitaldaemon.com...
It may be consistent with the search and split logic, but not consistent
with
the directory structure.  This entry,

c:\path\to\file\

is the same as

c:\path\to\file

my friend.

The first is nothing but a pathname. The second is a directory called "c:\path\to\" with a filename of "file". What exactly are you finding so difficult about this?

Never mind. I see that you're completely missing the point and so, why explain it. :-) Don't worry about it. I get it.
There is no "" directory.  If you run these two same "directory
structure" examples with getDirName(), you'll get a different outcome.  I
am no
longer talking about checking if it's file or directory.  I am now talking
just
plain directory structure definition.

Yes, you'll get a different outcome, because they _are_ different. In "directory structure definition", directories end in a \. Full filenames end with a letter. End.
Yes, which is exactly what is going on here.  However, it should check if
the
last character is a \ and make an appropiate "directory structure" result.

It does!
I understand.  Thanks.

But.. but.. you're still arguing that you don't!

Jul 20 2005
Derek Parnell <derek psych.ward> writes:
On Tue, 19 Jul 2005 19:04:36 +0000 (UTC), jicman wrote:

Greetings!

I already have a problem with std.path.getDirName, but that's another on-going
post.  However, what is the use of getBaseName?  Shouldn't it be getting the
last entry of a path?

Take a look at this piece of code:

import std.stdio;
import std.path;
void main ()
{
char[] dir0 = "c:\\this\\is\\a\\path\\long.pog";
char[] dir1 = "c:\\this\\is\\also\\a\\path\\";
writefln(std.path.getBaseName(dir0));
writefln(std.path.getBaseName(dir1));
}

when run, it outputs:

long.pog

And as you can see, the second line is empty.  That is wrong.  There is no such
a path as a blank or null value.  Yes, if I take the \ from the end it works.
However, this should be taken care by the function.  I should not need to do
any
cleanup for a function when I have a legitimate path.  This also goes for
getDirName.  It should return the last directory name in a path.  Even though
it
is a directory.  Otherwise, confusing outcomes will be the result.

I guess the names of these functions could be better, but the functions in std.path are string manipulation functions and not file system functions. You use them to get and set strings which represent files and paths, and not actually touch the file or path itself. It means you can use them on non-existing files and they still work. Check out the stuff in std.file to check for the actual existence or not of files and paths. I have had to supplement both modules with extra functions for my D applications to date. I can post those if you'd like to check them out ;-) -- Derek Parnell Melbourne, Australia 20/07/2005 8:18:29 AM
Jul 19 2005
Derek Parnell says...
On Tue, 19 Jul 2005 19:04:36 +0000 (UTC), jicman wrote:

Greetings!

I already have a problem with std.path.getDirName, but that's another on-going
post.  However, what is the use of getBaseName?  Shouldn't it be getting the
last entry of a path?

Take a look at this piece of code:

import std.stdio;
import std.path;
void main ()
{
char[] dir0 = "c:\\this\\is\\a\\path\\long.pog";
char[] dir1 = "c:\\this\\is\\also\\a\\path\\";
writefln(std.path.getBaseName(dir0));
writefln(std.path.getBaseName(dir1));
}

when run, it outputs:

long.pog

And as you can see, the second line is empty.  That is wrong.  There is no such
a path as a blank or null value.  Yes, if I take the \ from the end it works.
However, this should be taken care by the function.  I should not need to do
any
cleanup for a function when I have a legitimate path.  This also goes for
getDirName.  It should return the last directory name in a path.  Even though
it
is a directory.  Otherwise, confusing outcomes will be the result.

I guess the names of these functions could be better, but the functions in std.path are string manipulation functions and not file system functions. You use them to get and set strings which represent files and paths, and not actually touch the file or path itself. It means you can use them on non-existing files and they still work. Check out the stuff in std.file to check for the actual existence or not of files and paths. I have had to supplement both modules with extra functions for my D applications to date. I can post those if you'd like to check them out ;-)

if you don't mind, I would surely use it. Since I use your build tool all the time, might as well start using some of your code also. ;-) thanks, josé
Jul 19 2005