Re: dir() in linux

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

Rob wrote:

> Jerry Story writes:
>> The Euphoria manual, under "dir", says:
>> "On Windows and DOS st can contain * and ? wildcards
>>  to select multiple files."

>>   Does this mean dir() can't use wildcards on Linux?

> That's right.
> On Windows and DOS, the C library provides
> this wildcard functionality. On Linux, 
> you'll have to write your own Euphoria code to filter out 
> the filenames that you want/don't want.
> Maybe someday I'll get around to enhancing dir on Linux/FreeBSD etc.

As far as I can see, if a path without a file name/pattern is given to
dir(), it returns all entries in that folder, right?
This also must apply to Linux, otherwise walk_dir() wouldn't work on
Linux.

So I assume that maybe the following function dir_wild() does what you
want, Jerry. (I only have the possibility to test it on Windows, where
it works as expected.)

Regards,
   Juergen

   
------------------------------------------------------------------------
include file.e
include wildcard.e

integer SLASH
if platform() = LINUX then
   SLASH = '/'
else
   SLASH = '\\'
end if


function path_end (sequence name)
   for i = length(name) to 1 by -1 do
      if name[i] = SLASH then
         return i
      end if
   end for
   return find(':', name)
end function


global function file_path (sequence name)
   -- in : filename, e.g. "c:\\programs\\nicetool.exe"
   -- out: path (always with trailing slash), e.g. "c:\\programs\\"
   integer p

   p = path_end(name)
   if p = length(name) or name[p+1] != '.' then
      return name[1..p]
   else
      return name & SLASH
   end if
end function


global function file_name (sequence name)
   -- in : filename, e.g. "c:\\programs\\nicetool.exe"
   -- out: name without path, e.g. "nicetool.exe"
   integer p

   p = path_end(name) + 1
   if p <= length(name) and name[p] != '.' then
      return name[p..length(name)]
   else
      return ""
   end if
end function


global function dir_wild (sequence filespec)
   object list
   sequence name, path, entry, ret

   name = file_name(filespec)
   if find('?', name) or find('*', name) then
      path = file_path(filespec)
      if length(path) = 0 then
         path = current_dir()
      end if
      list = dir(path)
      if atom(list) then return -1 end if

      ret = ""
      for i = 1 to length(list) do
         entry = list[i]
         -- Note from the documentation of wildcard_file():
         -- In DOS [or Windows] "*ABC.*" will match all files.
         -- wildcard_file("*ABC.*", s) will only match when the file name part
         -- has "ABC" at the end (as you would expect).
         if wildcard_file(name, entry[D_NAME]) then
            ret = append(ret, entry)
         end if
      end for
      if length(ret) = 0 then return -1 end if
      return ret
   end if

   return dir(filespec)
end function


---------------[ Demo ]---------------
object list, std_list
sequence pattern

pattern = "*.REQ"
list = dir_wild(pattern)

-- for testing on DOS or Windows:
if platform() != LINUX then
   std_list = dir(pattern)
   if equal(list, std_list) then
      puts(1, "dir_wild() found the same entries as dir() !")
      puts(1, "\n\n")
   else
      puts(1, "dir_wild() found other entries than dir() !")
      puts(1, "\n\n")
      puts(1, "-- dir_wild():\n")
   end if
end if

if atom(list) then
   ? list
   abort(0)
end if
for i = 1 to length(list) do
   puts(1, list[i][D_NAME] & '\n')
end for
------------------------------------------------------------------------

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

Search



Quick Links

User menu

Not signed in.

Misc Menu