svdragster

search

May 8th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. -- Search API by svdragster
  2.  
  3. local split = function(str, pat) local t = {} local fpat = "(.-)"..pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end
  4.  
  5. debug = false
  6. found = false
  7. searchFileName = "nada"
  8. searchFileType = "sve"
  9.  
  10. function getProgramType(path)
  11.   programDir = split(path, "/")
  12.   res = split(programDir[#programDir], "%.")
  13.   programPath = path
  14.   programName = res[1]
  15.   programType = res[#res]
  16.   if debug then
  17.     term.setTextColour( colors.blue )
  18.     print(programPath)
  19.     print(programName)
  20.     print(programType)
  21.     term.setTextColour( colors.white )
  22.   end
  23. end
  24.  
  25. function check(path)
  26.   if debug then
  27.     print("check")
  28.   end
  29.     getProgramType(path)
  30.   if debug then
  31.     term.setTextColour( colors.green )
  32.     print(programName)
  33.     term.setTextColour( colors.orange )
  34.     print(programType)
  35.     term.setTextColour( colors.white )
  36.   end
  37.     if programName == searchFileName then
  38.         if programType == searchFileType then
  39.           found = true
  40.           return
  41.       end
  42.     end
  43. end
  44.  
  45. function readDir(dir)
  46.  if debug then
  47.    term.setTextColour( colors.red )
  48.    print("Dir: "..dir)
  49.    term.setTextColour( colors.white )
  50.  end
  51.  local dateien = fs.list(dir)
  52.  for i = 1, #dateien do
  53.    if debug then
  54.      term.setTextColour( colors.red )
  55.      print("Dir: "..dir)
  56.      term.setTextColour( colors.white )
  57.      print("readDir")
  58.    end
  59.    if found then
  60.      return
  61.    end
  62.   local path = fs.combine(dir, dateien[i])
  63.   if fs.isDir(path) then
  64.     if (dateien[i] ~= "rom") then
  65.       readDir(path)
  66.     end
  67.   end
  68.   check(path)
  69.  end
  70. end
  71.  
  72. function lookFor(str)
  73.   local res = split(str, "%.")
  74.   searchFileName = res[1]
  75.   searchFileType = res[#res]
  76.   readDir("/")
  77.   if found then
  78.     return true
  79.   else
  80.     return false
  81.   end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment