Problems concerning dir()

new topic     » topic index » view thread      » older message » newer message

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, blink 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

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu