Re: No response?

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

Noah Smith wrote:
>Using only the "open" function, write a function
>which returns all files in a directory.

here's my attempt...
seems to work and is tested code...
this is almost assuredly *not* the fastest way to
do this, but it meets the given problem's criteria...
Enjoy the wait...
------------begin

--I hereby declare and understand that ValidChars may *NOT*
--represent *all* possible characters that can appear in a filename.
--It's a good stab at it tho...and it's easy enuff to change...
constant ValidChars=" abcdefghijklmnopqrstuvwxyz1234567890-_~@${}"

--these are actually devices, not files, so we exclude them...
constant Bad={  "nul     .   ","prn     .   ",
                "aux     .   ","con     .   ",
                "com1    .   ","com2    .   ",
                "com3    .   ","com4    .   "  }

constant TRUE=1, FALSE=0
sequence try,found
integer lengthVC
atom now

function IsBadName(sequence test)
   for i = 1 to length(Bad) do
       if compare(test,Bad[i]) = 0 then return TRUE end if
   end for
   return FALSE
end function

function Exists(sequence filename)
   if open(filename,"r") = -1 then return FALSE end if
   return TRUE
end function

procedure ShowList(sequence list)
--Go crazy here, make it as fancy as you want. I opt for simple since
--the output for the problem wasn't specified.
   position (2,1)
   for i = 1 to length(list) do
      puts (1,"'" & list[i] & "' ")
   end for
end procedure

procedure ScrollNames()
   for try12 =1 to lengthVC do
     try[12]  = ValidChars[try12]
     for try11 =1 to lengthVC do
       try[11]  = ValidChars[try11]
       for try10 =1 to lengthVC do
         try[10]  = ValidChars[try10]
         for try8 =1 to lengthVC do     --note:skip 9 here, it's the '.'
           try[8]  = ValidChars[try8]
           for try7 =1 to lengthVC do
             try[7]  = ValidChars[try7]
             for try6 =1 to lengthVC do
               try[6]  = ValidChars[try6]
               for try5 =1 to lengthVC do
                 try[5]  = ValidChars[try5]
                 for try4 =1 to lengthVC do
                   try[4]  = ValidChars[try4]
                   for try3=1 to lengthVC do
                     try[3] = ValidChars[try3]
                     for try2=1 to lengthVC do
                       try[2] = ValidChars[try2]
                       for try1=1 to lengthVC do
                         try[1] = ValidChars[try1]
                         --for speed, these lines are commented out
                         --to see it work, uncomment the next 2 lines
                         --position(1,1)
                         --puts(1,try)
                         if Exists(try) and (not IsBadName(try)) then
                             found = append(found,try)
                             --for speed, leave this commented
                             --to see it work, uncomment it
                             --ShowList(found)
                         end if
   end for end for end for end for end for end for
   end for end for end for end for end for
end procedure

--MAIN
clear_screen()
found = {}
try=repeat(' ',12)
try[9]='.'
lengthVC=length(ValidChars)
now = time()
ScrollNames()
ShowList(found)
printf(1,"It took %f seconds to do this DIR",time()-now)
--did you actually wait to see the above line printed??? ;)

---------------end

--Hawke'

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

Search



Quick Links

User menu

Not signed in.

Misc Menu