Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/paths.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeon Bottou <leon@bottou.org>2015-06-03 15:03:16 +0300
committerLeon Bottou <leon@bottou.org>2015-06-03 15:03:16 +0300
commit693942c31111a8948b786f3de5cbb354024cdc52 (patch)
treec76d54e060af3c66fe5180ee442ab6630810f5c4
parent851f31425ca3ec878cbaf516b8e3dad55164d80d (diff)
parentab7b9e9fd22bd3c51b86c9eb7c6ad3f28858d427 (diff)
Merge pull request #8 from nicholas-leonard/dirs
paths.iterfiles and paths.iterdirs
-rw-r--r--README.md63
-rw-r--r--init.lua40
2 files changed, 83 insertions, 20 deletions
diff --git a/README.md b/README.md
index 6b22d18..bca69ec 100644
--- a/README.md
+++ b/README.md
@@ -91,34 +91,73 @@ because it makes sure that the files are removed on exit.
In addition, `os.tmpname()` under windows often returns filenames
for which the user has no permission to write.
-
-
-
<a name="paths.dirs.dok"/>
## Directory functions ##
The following functions can be used
to examine directory contents or manipulate directories.
-
<a name="paths.dir"/>
### paths.dir(dname) ###
-Return a table containing the files in directory `dname`.
-This function return `nil` if the specified directory
-does not exists.
+Return a table containing the files and directories in directory `dname`.
+This function return `nil` if the specified directory does not exists.
+For linux, this includes the `.` and `..` directories.
<a name="paths.files"/>
-### paths.files(dname) ###
+### paths.files(dname [, include]) ###
+
+Returns an iterator over the files and directories located in directory `dname`.
+For linux, this includes the `.` and `..` directories.
+This can be used in *__for__* expression as shown below:
+
+```lua
+for f in paths.files(".") do
+ print(f)
+end
+```
+
+Optional argument `include` is either a function or a string used to
+determine which files are to be included. The function takes the filename
+as argument and should return true if the file is to be included.
+When a string is provided, the following function is used :
+
+```lua
+function(file)
+ return file:find(f)
+end
+```
+
+Files and directories of sub-folders aren't included.
+
+<a name="paths.iterdirs"/>
+### paths.iterdirs(dname) ###
-Returns an iterator over the files located in directory `dname`.
+Returns an iterator over the directories located in directory `dname`.
This can be used in *__for__* expression as shown below:
+
+```lua
+for dir in paths.iterdirs(".") do
+ print(dir)
+end
```
- for f in paths.files(".") do
- print(f)
- end
+
+Directories of sub-folders, and the `.` and `..` folders aren't included.
+
+<a name="paths.iterdirs"/>
+### paths.iterfiles(dname) ###
+
+Returns an iterator over the files (non-directories) located in directory `dname`.
+This can be used in *__for__* expression as shown below:
+
+```lua
+for file in paths.iterfiles(".") do
+ print(file)
+end
```
+Files of sub-folders, and the `.` and `..` folders aren't included.
+
<a name="paths.mkdir"/>
### paths.mkdir(s) ###
diff --git a/init.lua b/init.lua
index cd61b33..c214055 100644
--- a/init.lua
+++ b/init.lua
@@ -22,17 +22,41 @@ else
paths.home = os.getenv('HOME') or '.'
end
-function paths.files(s)
+function paths.files(s, f)
local d = paths.dir(s)
local n = 0
+ if torch.type(f) == 'string' then
+ local pattern = f
+ f = function(file) return file:find(pattern) end
+ elseif f and torch.type(f) ~= 'function' then
+ error("Expecting optional arg 2 to be function or string. Got : "..torch.type(f))
+ end
+ f = f or function(file) return true end
+ local n = 0
return function()
- n = n + 1
- if (d and n <= #d) then
- return d[n]
- else
- return nil
- end
- end
+ while true do
+ n = n + 1
+ if d == nil or n > #d then
+ return nil
+ elseif f(d[n]) then
+ return d[n]
+ end
+ end
+ end
+end
+
+function paths.iterdirs(s)
+ return paths.files(s,
+ function(dir)
+ return paths.dirp(paths.concat(s, dir)) and dir ~= '.' and dir ~= '..'
+ end)
+end
+
+function paths.iterfiles(s)
+ return paths.files(s,
+ function(file)
+ return paths.filep(paths.concat(s, file)) and file ~= '.' and file ~= '..'
+ end)
end
function paths.thisfile(arg, depth)