www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.file: isfile/isdir

reply vathixSpamFix dprogramming.com (Vathix) writes:
Functions isfile() and isdir() from std.file test if the name is a file or 
directory, but if the file or directory doesn't exist they throw an exception. 
I'd say it's not a file or directory if it doesn't even exist. Well, I don't 
mind the current behavior if it would specify this in phobos.html; and I 
suggest adding another function exists().


-- 
Christopher E. Miller
May 07 2004
next sibling parent reply C <qbert atari-soldiers.com> writes:
Ughh, are you serious it throws an exception ?

Can we please change this to return boolean ?

Phobos has become a seriously crippling element for the D language.  But 
im done preaching (begging?), ive shouted myself hoarse. What a shame.

Charles

On Fri, 7 May 2004 16:26:51 +0000 (UTC), Vathix 
<vathixSpamFix dprogramming.com> wrote:

 Functions isfile() and isdir() from std.file test if the name is a file 
 or
 directory, but if the file or directory doesn't exist they throw an 
 exception.
 I'd say it's not a file or directory if it doesn't even exist. Well, I 
 don't
 mind the current behavior if it would specify this in phobos.html; and I
 suggest adding another function exists().

May 07 2004
parent reply Andy Friesen <andy ikagames.com> writes:
C wrote:

 Ughh, are you serious it throws an exception ?
 
 Can we please change this to return boolean ?
 
 Phobos has become a seriously crippling element for the D language.  But 
 im done preaching (begging?), ive shouted myself hoarse. What a shame.
 
 Charles

I think this is the correct behaviour. Asking the OS for information about something that isn't there at all sounds exceptional to me. It's generally better for a program to explode spectacularly than to subtly fail in the face of oversight. It's not at all unrealistic to suppose that someone might write something like this: if (isfile(fileName) { .... } else { // assume it is a directory .... } -- andy
May 07 2004
parent reply "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
Andy Friesen wrote:

 I think this is the correct behaviour.  Asking the OS for information 
 about something that isn't there at all sounds exceptional to me.
 
 It's generally better for a program to explode spectacularly than to 
 subtly fail in the face of oversight.  It's not at all unrealistic to 
 suppose that someone might write something like this:
 
    if (isfile(fileName) {
       ....
    } else {
       // assume it is a directory
       ....
    }
 
  -- andy

Not always. I am mentioning it again, and it's not even a compiled language, but PHP handles this differently for good reason. Basically, because PHP supports streams (ssl://, http://, zlib://, ftp://, etc.) it's possible you could get your hands on a variable that contains a URL. The is_file() function returns if this is a *regular filesystem* file... and, similarly, the is_link() function returns if it is a *regular filesystem* symbolic link. (whereas file_exists() might work over zlib:// and tell you if the file exists as a gzip, etc. or under ftp:// the normal way.) But, assuming there is a function like exists(), or file_exists(), etc. it would not be a problem at all. Either way, personally I see assuming that !file is dir as more of an oversight than using isfile on a non-existant path. -[Unknown]
May 07 2004
parent Andy Friesen <andy ikagames.com> writes:
Unknown W. Brackets wrote:

    if (isfile(fileName) {
       ....
    } else {
       // assume it is a directory
       ....
    } 

oversight than using isfile on a non-existant path.

That's exactly what I meant, actually. If it isn't a bug, it sure looks like one to me. If it is, then it would be preferable for the program to instantly explode with a line number and a good error message. -- andy
May 07 2004
prev sibling next sibling parent "Walter" <newshound digitalmars.com> writes:
"Vathix" <vathixSpamFix dprogramming.com> wrote in message
news:c7gdca$orb$1 digitaldaemon.com...
 Functions isfile() and isdir() from std.file test if the name is a file or
 directory, but if the file or directory doesn't exist they throw an

 I'd say it's not a file or directory if it doesn't even exist. Well, I

 mind the current behavior if it would specify this in phobos.html; and I
 suggest adding another function exists().

I thought there was an exists() function already, but there isn't. You're right, there needs to be one.
May 07 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
I've attached the overlooked function.
May 07 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:c7gkv5$160c$1 digitaldaemon.com...
 I've attached the overlooked function.

You're returning false if getting the file attributes fails, but a different error could have occured like due to permissions or disk failure. I think it should test, on failure if GetLastError() returns ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND; and something with errno for linux, I assume. Sorry for nitpicking :> -- Christopher E. Miller
May 07 2004
next sibling parent "Walter" <newshound digitalmars.com> writes:
"Vathix" <vathixSpamFix dprogramming.com> wrote in message
news:c7gnga$19im$1 digitaldaemon.com...
 "Walter" <newshound digitalmars.com> wrote in message
 news:c7gkv5$160c$1 digitaldaemon.com...
 I've attached the overlooked function.

You're returning false if getting the file attributes fails, but a

 error could have occured like due to permissions or disk failure. I think

 should test, on failure if GetLastError() returns ERROR_FILE_NOT_FOUND or
 ERROR_PATH_NOT_FOUND; and something with errno for linux, I assume. Sorry
 for nitpicking :>

It's a good point, and I could argue it either way.
May 07 2004
prev sibling parent "Unknown W. Brackets" <unknown at.simplemachines.dot.org> writes:
Vathix wrote:

 You're returning false if getting the file attributes fails, but a different
 error could have occured like due to permissions or disk failure. I think it
 should test, on failure if GetLastError() returns ERROR_FILE_NOT_FOUND or
 ERROR_PATH_NOT_FOUND; and something with errno for linux, I assume. Sorry
 for nitpicking :>
 

Many other implementations simply say a file does not exist if you cannot access it too. It's called security through obscurity :P. (kidding) -[Unknown]
May 07 2004