Advertisement
Tag365

Ultimate Index API

Apr 8th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. -- Set local variables
  2. -- This is an internal function called by updateIndex to initalize the search index.
  3. function updateIndexFile(path)
  4.     indexedFiles[path] = ""
  5.     local file = fs.open(path, "r")
  6.     if file then
  7.         local line = file.readAll()
  8.         file.close()
  9.         indexedFiles[path] = line
  10.     else
  11.         indexedFiles[path] = ""
  12.     end
  13. end
  14.  
  15. -- This is an internal function called by rebuildIndex to initalize the search index.
  16. -- It loops through the directory list and calls itself if it finds a directory.
  17. function updateIndex(path)
  18.     if fs.isDir(path) then
  19.         for k, v in pairs(fs.list(path)) do
  20.             if fs.isDir(path.."/"..v) then
  21.                 updateIndex(path.."/"..v)
  22.             else
  23.                 updateIndexFile(path.."/"..v)
  24.             end
  25.         end
  26.     elseif fs.exists(path) then
  27.         updateIndexFile(path)
  28.     end
  29. end
  30.  
  31. -- This is an internal function run by Lua Script System to initalize the search index.
  32. -- It is run on startup to initalize the index. If you are not using LuaSS then you need to run this before the
  33. -- first time you search files using the searchFiles or searchFileName functions given by this API.
  34. -- Currently it just calls updateIndex to index the root directory.
  35. function rebuildIndex()
  36.     updateIndex("")
  37. end
  38.  
  39. -- This is an internal function used to check if a file should be included in the table returned by searchFiles.
  40. function findStringInIndexSearchFiles(path, sSearch)
  41.     local contents = indexedFiles[path]
  42.     local startHere = 0
  43.     while true do
  44.         local nextSlash = string.find(string.sub(contents, startHere, -1), sSearch)
  45.         if not nextSlash then
  46.             return
  47.         else
  48.             fileContentIndex[sSearch][#fileContentIndex[sSearch] + 1] = path
  49.             return true
  50.         end
  51.         nextSlash = nextSlash + startHere
  52.         tab = tab + 1
  53.         startHere = nextSlash
  54.     end
  55. end
  56.  
  57. -- This is an internal function used to check if a file should be included in the table returned by searchFileNames.
  58. function findStringInIndexSearchFileNames(path, sSearch)
  59.     local match = string.find(path, sSearch)
  60.     if match then
  61.         fileNameIndex[sSearch][#fileNameIndex[sSearch] + 1] = path
  62.         return true
  63.     end
  64. end
  65.  
  66. function checkIfIndexed()
  67.     if not indexedFiles then
  68.         indexedFiles = {} -- This table is used to store indexed file contents.
  69.         fileContentIndex = {} -- This table is used to store file content matches.
  70.         fileNameIndex = {} -- This table is used to store file name matches.
  71.         print("Indexing Files...")
  72.         rebuildIndex()
  73.     end
  74. end
  75.  
  76. -- This function searches inside files for the searchstring sSearch.
  77. function searchFiles(sSearch)
  78.     checkIfIndexed()
  79.     local tab = fileContentIndex[sSearch]
  80.     if tab then
  81.         return tab
  82.     else
  83.         fileContentIndex[sSearch] = {}
  84.         for k, v in pairs(indexedFiles) do
  85.             findStringInIndexSearchFiles(k, sSearch)
  86.         end
  87.         return fileContentIndex[sSearch]
  88.     end
  89. end
  90.  
  91. -- This function searches for files with names that include the string sSearch.
  92. function searchFileNames(sSearch)
  93.     checkIfIndexed()
  94.     local tab = fileNameIndex[sSearch]
  95.     if tab then
  96.         return tab
  97.     else
  98.         fileNameIndex[sSearch] = {}
  99.         for k, v in pairs(indexedFiles) do
  100.             findStringInIndexSearchFileNames(k, sSearch)
  101.         end
  102.         return fileNameIndex[sSearch]
  103.     end
  104. end
  105.  
  106. local function printUsage()
  107.     if shell then
  108.         print("To search files, use "..shell.getRunningProgram().." search <name>.")
  109.         print("To search file names, use "..shell.getRunningProgram().." find <name>.")
  110.         print("To rebuild the index, use "..shell.getRunningProgram().." rebuild.")
  111.     end
  112. end
  113.  
  114. local tArgs = {...}
  115. if #tArgs > 0 then
  116.     if #tArgs > 1 then
  117.         if tArgs[1] == "find" then
  118.             textutils.pagedTabulate(searchFileNames(tArgs[2]))
  119.         elseif tArgs[1] == "search" then
  120.             textutils.pagedTabulate(searchFiles(tArgs[2]))
  121.         else
  122.             printUsage()
  123.         end
  124.     elseif tArgs[1] == "rebuild" then
  125.         rebuildIndex()
  126.     else
  127.         printUsage()
  128.     end
  129. else
  130.     if shell then
  131.         checkIfIndexed()
  132.         printUsage()
  133.     end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement