diff options
author | Clement Farabet <clement.farabet@gmail.com> | 2011-07-01 07:59:09 +0400 |
---|---|---|
committer | Clement Farabet <clement.farabet@gmail.com> | 2011-07-01 07:59:09 +0400 |
commit | babe225090ad2d4e2cdcaf6b0dae2e237d2e55b8 (patch) | |
tree | 5ae1168c16cfb2821a9d2532a5890a26f6413ae3 | |
parent | 47c0cc7d8bbd33b3737c23b007d356e9cb1c3406 (diff) |
Imported a few extra functions from paths package.
-rw-r--r-- | sys.c | 22 | ||||
-rw-r--r-- | sys.lua | 22 |
2 files changed, 41 insertions, 3 deletions
@@ -14,6 +14,8 @@ #include <time.h> #include <sys/stat.h> #include <sys/types.h> +#include <dirent.h> +#define NAMLEN(dirent) strlen((dirent)->d_name) #define SBINCREMENT 256 @@ -231,6 +233,25 @@ static int l_pwd(lua_State *L) { return sbpush(L, &sb); } +static int l_dir(lua_State *L) { + int k = 0; + const char *s = luaL_checkstring(L, 1); + DIR *dirp; + struct dirent *d; + dirp = opendir(s); + if (dirp) { + lua_createtable(L, 0, 0); + while ((d = readdir(dirp))) { + int n = NAMLEN(d); + lua_pushlstring(L, d->d_name, n); + lua_rawseti(L, -2, ++k); + } + closedir(dirp); + } else + lua_pushnil(L); + return 1; +} + static int concat_fname(lua_State *L, const char *fname) { const char *from = lua_tostring(L, -1); const char *s; @@ -293,6 +314,7 @@ static const struct luaL_reg routines [] = { {"dirname", l_dirname}, {"basename", l_basename}, {"concat", l_concat}, + {"dir", l_dir}, {"pwd", l_pwd}, {NULL, NULL} }; @@ -1,6 +1,6 @@ ---------------------------------------------------------------------- -- --- Copyright (c) 2010 Clement Farabet +-- Copyright (c) 2011 Clement Farabet -- -- Permission is hereby granted, free of charge, to any person obtaining -- a copy of this software and associated documentation files (the @@ -26,10 +26,10 @@ -- sys - a package that provides simple system (unix) tools -- -- ack: --- the C lib was largely taken from Torch5 +-- the C lib was largely taken from Torch5 (code from Ronan) -- -- history: --- March 27, 2011, 9:58PM - creation +-- March 27, 2011, 9:58PM - creation - Clement Farabet ---------------------------------------------------------------------- require 'os' @@ -126,6 +126,22 @@ function sleep(seconds) end -------------------------------------------------------------------------------- +-- file iterator, in given path +-------------------------------------------------------------------------------- +function files(path) + local d = dir(path) + local n = 0 + return function() + n = n + 1 + if (d and n <= #d) then + return d[n] + else + return nil + end + end +end + +-------------------------------------------------------------------------------- -- colors, can be used to print things in color -------------------------------------------------------------------------------- COLORS = {none = '\27[0m', |