This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CoolisTheName007's profile picture

File Search API with glob support, all with efficient iterators

Started by CoolisTheName007, 12 November 2012 - 10:45 AM
CoolisTheName007 #1
Posted 12 November 2012 - 11:45 AM
Update! Old post, be-gone!
This is an API for efficient file searching, meaning that fs.list and fs.isDir (the only functions used from the fs API) are a minimum number of times.

New and exciting feature: you can use glob patterns to define search paths, which are like Lua patterns but easier. e.g. search.findfirst('*/A') will match all files/directories named A contained in any directory! For more details about glob, check out this wiki page.

This project is on Github , full up-to-date documentation is there, below information serves as a sample of what you can do, possibly with slightly different syntax:.

—filesystem glob search API by CoolisTheName007
–The hard work here was writing the iterators to be efficient; for instance they don't use coroutines or recursion.

function findfirst(g)
—returns the first path that matches the glob pattern g.
–@param g glob
–@return path

function searchGlob(g,p)
—returns the first path that matches any of the glob patterns (separated by ';') in the optional string p (default '/') concatenated with glob pattern g.
–@usage search.searchGlob('*log*','APIS;packages/*;/')
–searches first in the directory 'APIS', then in the subdirectories of 'packages', then in the root directory '/' > packages/jumper/30log.lua
–@treturn string path

function iterGlob(g)
—iterator creator, over the valid paths defined by glob g,e.g */filenumber?to
– see the unix part of the table at http://en.wikipedia....ob_(programming) .
–@treturn string path to matching of the dir
–@usage
–for path in search.iterGlob('*/stuff?/a*') do
– print(path)
–end
–APIS/stuff1/a.lua
–var/stuff2/a.var

function searchAll(fname,_sPath,_nDepth)
—searches for the file fname (removing extensions)
–on the directory tree of _sPath (defaults to '')
–with maximum depth _nDepth (defaults to infinite).
–@treturn string path

function iterFiles(dir,depth)
—iterator over files in directory dir and with maximum depth of search depth, default is infinite
–following refer to the iterator returned, not the iterator created.
–@treturn string name of the file
–@treturn string directory where the file is


function iterDir(dir,depth)
—iterator over files in directory dir and with maximum depth of search depth, default is infinite
–following refer to the iterator returned, not the iterator created.
–@treturn string name of the dir
–@treturn string directory where the dir is

function getNameExpansion(s)
—returns name and expansion from a filepath.
–@param s filepath
–@return name
–@return expansion

–helper functions/not-so-straight-forward to use utilities

function globtopattern(g)
—taken from https://github.com/d...ua-glob-pattern , by davidm
–only needed for filename conversion, slashes are dealt with directly for iteration purposes


function compact(g)
—turns a glob into a table structure proper for iterPatterns.


function iterPatterns(l)
—accepts the following table structure: {t1,…,tn}, where ti is:
–for i<n: {dir,pat} - dir is the directory where to look for names matching the pattern pat
–for i=n: {dir,pat,ending} -same but will combine the name (after successful match with pat) with the optional ending (can be nil) and check the resulting path
–e.g., g={{'APIS','*'},{nil,'A'},{'B/C','?','aq/qwerty'}} will search in all subfolders of APIS for subfolders named A, and in each of those for a folder B
–containing a folder C, and for all one-lettered folders in that folder for a folder aq containing a folder/file named qwerty.
CoolisTheName007 #2
Posted 20 November 2012 - 11:38 AM
Bumping for proper update. Github rocks!