1. Problems concerning dir()
- Posted by Juergen Luethje <j.lue at gmx.de> Jan 16, 2006
- 509 views
Hi all, I've encountered some problems concerning dir() (tested with exw.exe 2.5 on Windows 98).
include file.e object D
1)
D = dir("c:\\autoexec.bat\\") pretty_print(1, D, {3})
should return -1, because on my drive c: there is a file "c:\\autoexec.bat", but no directory "c:\\autoexec.bat\\". A trailing (back)slash should always denote a directory IMHO. 2) When we have a drive that contains only 1 file in the root directory, say "h:\\foo.txt", then the following commands
D = dir("h:\\") pretty_print(1, D, {3}) D = dir("h:\\foo.txt") pretty_print(1, D, {3})
return exactly the same results!! This is strange IMHO, and might lead to subtle bugs. Of course probably all our c: drives contain more than one file,but on a floppy disk or USB drive it can easily happen that there is only one file. Of course the root directory cannot contain a parent entry "..", but why shouldn't it contain the entry ".", like any other directory? That would make things clearer IMHO (see code below). 3) From a call like
D = dir("h:\\") pretty_print(1, D, {3})
we cannot know whether there is an _empty_ drive h:, or whether no drive h: does exist on the concerning system! In both cases, we will get -1 as result. For an existing empty drive, dir() should _not_ return an atom. Currently this is actually confusing. Again it would help, if the result of dir() always contained "." for an existing root directory. I assume that Euphoria does not deliberately behave in the way mentioned above, but just "wraps" the Watcom C dir() function. However, I think at least we should be aware of these points. The following wrapper for dir() tries to address all three issues:
global function clear_dir (sequence name) object d d = dir(name) -- name = file --> length(d) = 1 -- name = normal directory --> length(d) >= 2 -- name = root directory --> length(d) >= 1 (or d = -1, if empty!!) -- name = not existing --> d = -1 if length(name) >= 2 and length(name) <= 3 and name[2] = ':' then if atom(d) then -- drive or root directory ... d = dir(name & "nul") if atom(d) then -- ... does not exist return d end if d = {} -- ... is empty end if d = prepend(d, {".","d",0, 1980,1,1, 0,0,0}) elsif atom(d) or (name[$] = '\\' and length(d) = 1) then return -1 end if return d end function -- Demo -- ---- sequence name name = "h:\\" -- existing empty drive pretty_print(1, clear_dir(name), {3}) name = "x:\\" -- not existing drive pretty_print(1, clear_dir(name), {3}) name = "c:\\temp\\" -- existing empty subdirectory pretty_print(1, clear_dir(name), {3}) name = "c:\\timp\\" -- not existing subdirectory pretty_print(1, clear_dir(name), {3}) name = "c:\\autoexec.bat" -- existing file pretty_print(1, clear_dir(name), {3}) name = "c:\\autoexec.bat\\" -- wrong name for existing file pretty_print(1, clear_dir(name), {3}) name = "c:\\auto.bat" -- not existing file pretty_print(1, clear_dir(name), {3})
Regards, Juergen
2. Re: Problems concerning dir()
- Posted by Robert Craig <rds at RapidEuphoria.com> Jan 16, 2006
- 494 views
Juergen Luethje wrote: > I've encountered some problems concerning dir() > (tested with exw.exe 2.5 on Windows 98). > ... Thanks. I'll look into these examples for the next release. dir() is tricky, because each C compiler does it a bit differently, and because Watcom crashes in certain special cases that I have to avoid. Thanks, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com
3. Re: Problems concerning dir()
- Posted by Greg Haberek <ghaberek at gmail.com> Jan 17, 2006
- 488 views
> Thanks. I'll look into these examples for the next release. > dir() is tricky, because each C compiler does it a bit differently, > and because Watcom crashes in certain special cases that I have to avoid. Why not use my win_dir() routine? It's written from scratch using the Windows API, so it shouldn't have any "bugs" - and a directory must end in a backslash for it to return files, otherwise it returns information about the directory itself. And it a directory contains no files, it returns an empty sequence, but an error (atom) if the directory does not exist. Perhaps, Rob, you could implement my win_dir() routine in the WIN32 Interpr= eter? ~Greg
4. Re: Problems concerning dir()
- Posted by Robert Craig <rds at RapidEuphoria.com> Jan 17, 2006
- 511 views
Greg Haberek wrote: > Why not use my win_dir() routine? It's written from scratch using the > Windows API, so it shouldn't have any "bugs" - and a directory must > end in a backslash for it to return files, otherwise it returns > information about the directory itself. And it a directory contains no > files, it returns an empty sequence, but an error (atom) if the > directory does not exist. > > Perhaps, Rob, you could implement my win_dir() routine in the WIN32 Interpr= > eter? Thanks for the offer. However I have to worry about things like backwards compatibility and cross-platform compatibility. I'd also have to hand-translate your Euphoria code into pure C, and integrate it with the rest of the interpreter and translator. I'd rather just tweak the code I've got now. Regards, Rob Craig Rapid Deployment Software http://www.RapidEuphoria.com