Advertisement
theoriginalbit

ComputerCraft: File System Iterator

Jan 13th, 2013
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. --[[
  2.   File System Iterator v1.0 [Updated: 14 Jan 2013]
  3.  
  4.   Copyright © 2013-2014 Joshua Asbury a.k.a. theoriginalbit [theoriginalbit@gmail.com]
  5.  
  6.   Permission is hereby granted, free of charge, to any person obtaining a copy
  7.   of this software and associated documentation files (the "Software"), to deal
  8.   in the Software without restriction, including without limitation the rights
  9.   to use, copy, modify, merge, publish, distribute, and/or sublicense copies
  10.   of the Software, and to permit persons to whom the Software is furnished to
  11.   do so, subject to the following conditions:
  12.  
  13.   - The above copyright notice and this permission notice shall be included in
  14.     all copies or substantial portions of the Software;
  15.   - Visible credit is given to the original author;
  16.   - The software is distributed in a non-profit way;
  17.  
  18.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24.   THE SOFTWARE.
  25. --]]
  26.  
  27. local function str_contains( str, seq )
  28.   return (str:find( seq, 1 )) ~= nil
  29. end
  30.  
  31. local function contains( ignore, line )
  32.   if type( ignore ) == "table" then
  33.     for _, v in pairs( ignore ) do
  34.       if line == v or str_contains( line, v ) then
  35.         return true
  36.       end
  37.     end
  38.     return false
  39.   end
  40.   return str_contains( line, ignore )
  41. end
  42.  
  43. local function yieldFileSystem(startPath, ignorePaths, debug)
  44.   local fsList = fs.list(startPath)
  45.   for _, file in ipairs(fsList) do
  46.     local path = fs.combine(startPath, file)
  47.     if not contains(ignorePaths, path) then
  48.       if fs.isDir(path) then
  49.         yieldFileSystem(path, ignorePaths, debug)
  50.       else
  51.         coroutine.yield(path, file)
  52.       end
  53.     elseif debug then
  54.       print( "Ignoring: "..path )
  55.     end
  56.   end
  57. end
  58.  
  59. function fileSystemIter(startPath, ignorePaths, debug)
  60.   startPath = startPath or "/"
  61.   ignorePaths = ignorePaths or {}
  62.   debug = debug == true
  63.   return coroutine.wrap(function()
  64.     yieldFileSystem(startPath, ignorePaths, debug)
  65.   end)
  66. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement