diff options
author | Clement Farabet <clement.farabet@gmail.com> | 2013-10-09 17:38:26 +0400 |
---|---|---|
committer | Clement Farabet <clement.farabet@gmail.com> | 2013-10-09 17:38:26 +0400 |
commit | e30c8666a9a6fe56f5dfb7cadf37af09eeb916ca (patch) | |
tree | 9d9878b371160a6495d7b835d27cb71efd11a626 | |
parent | 5f8c7342740e1d4df55a12543edada614e09b6af (diff) |
Repackaged sys
-rw-r--r-- | CMakeLists.txt | 9 | ||||
-rw-r--r-- | init.lua | 104 | ||||
-rw-r--r-- | sys-1.0-0.rockspec | 7 | ||||
-rw-r--r-- | sys.c | 284 |
4 files changed, 35 insertions, 369 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 532ca43..bcb8d2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,11 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6 FATAL_ERROR) CMAKE_POLICY(VERSION 2.6) -IF(LUAROCKS_PREFIX) - MESSAGE(STATUS "Installing Torch through Luarocks") - STRING(REGEX REPLACE "(.*)lib/luarocks/rocks.*" "\\1" CMAKE_INSTALL_PREFIX "${LUAROCKS_PREFIX}") - MESSAGE(STATUS "Prefix inferred from Luarocks: ${CMAKE_INSTALL_PREFIX}") -ENDIF() + FIND_PACKAGE(Torch REQUIRED) SET(src sys.c) SET(luasrc init.lua) -ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}" "System") TARGET_LINK_LIBRARIES(sys luaT TH) + +ADD_TORCH_PACKAGE(sys "${src}" "${luasrc}") @@ -34,6 +34,7 @@ require 'os' require 'io' +require 'paths' local _G = _G local print = print @@ -43,6 +44,7 @@ local os = os local io = io local pairs = pairs local ipairs = ipairs +local paths = paths module 'sys' _lib = require 'libsys' @@ -84,7 +86,7 @@ execute = function(cmd, readwhat) end -- TODO: use the following code, which would avoid the side effect. --- For now it doesnt work though, and I can't explain why. +-- Issue: popen seems broken on OSX -- execute = function(cmd) -- local f = io.popen(cmd, 'r') -- local s = f:read('*all') @@ -99,7 +101,7 @@ execute = function(cmd, readwhat) -- more reliable -------------------------------------------------------------------------------- uname = function() - if dirp('C:\\') then + if paths.dirp('C:\\') then return 'windows' else local os = execute('uname -a') @@ -132,8 +134,8 @@ prefix = execute('which lua'):gsub('//','/'):gsub('/bin/lua\n','') -------------------------------------------------------------------------------- function fpath() local fpath = _G.debug.getinfo(2).source:gsub('@','') - if fpath:find('/') ~= 1 then fpath = concat(pwd(),fpath) end - return dirname(fpath),basename(fpath) + if fpath:find('/') ~= 1 then fpath = paths.concat(paths.cwd(),fpath) end + return paths.dirname(fpath),paths.basename(fpath) end -------------------------------------------------------------------------------- @@ -174,74 +176,30 @@ 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 -------------------------------------------------------------------------------- -if _G.qt and _G.qt.qConsole.captureOutput then - COLORS = {none = '', - black = '', - red = '', - green = '', - yellow = '', - blue = '', - magenta = '', - cyan = '', - white = '', - Black = '', - Red = '', - Green = '', - Yellow = '', - Blue = '', - Magenta = '', - Cyan = '', - White = '', - _black = '', - _red = '', - _green = '', - _yellow = '', - _blue = '', - _magenta = '', - _cyan = '', - _white = ''} -else - COLORS = {none = '\27[0m', - black = '\27[0;30m', - red = '\27[0;31m', - green = '\27[0;32m', - yellow = '\27[0;33m', - blue = '\27[0;34m', - magenta = '\27[0;35m', - cyan = '\27[0;36m', - white = '\27[0;37m', - Black = '\27[1;30m', - Red = '\27[1;31m', - Green = '\27[1;32m', - Yellow = '\27[1;33m', - Blue = '\27[1;34m', - Magenta = '\27[1;35m', - Cyan = '\27[1;36m', - White = '\27[1;37m', - _black = '\27[40m', - _red = '\27[41m', - _green = '\27[42m', - _yellow = '\27[43m', - _blue = '\27[44m', - _magenta = '\27[45m', - _cyan = '\27[46m', - _white = '\27[47m'} -end +COLORS = {none = '\27[0m', + black = '\27[0;30m', + red = '\27[0;31m', + green = '\27[0;32m', + yellow = '\27[0;33m', + blue = '\27[0;34m', + magenta = '\27[0;35m', + cyan = '\27[0;36m', + white = '\27[0;37m', + Black = '\27[1;30m', + Red = '\27[1;31m', + Green = '\27[1;32m', + Yellow = '\27[1;33m', + Blue = '\27[1;34m', + Magenta = '\27[1;35m', + Cyan = '\27[1;36m', + White = '\27[1;37m', + _black = '\27[40m', + _red = '\27[41m', + _green = '\27[42m', + _yellow = '\27[43m', + _blue = '\27[44m', + _magenta = '\27[45m', + _cyan = '\27[46m', + _white = '\27[47m'} diff --git a/sys-1.0-0.rockspec b/sys-1.0-0.rockspec index 9045691..08c008b 100644 --- a/sys-1.0-0.rockspec +++ b/sys-1.0-0.rockspec @@ -20,11 +20,6 @@ dependencies = { build = { type = "command", - build_command = [[ -cmake -E make_directory build; -cd build; -cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)"; -$(MAKE) - ]], + build_command = [[cmake -E make_directory build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="$(LUA_BINDIR)/.." -DCMAKE_INSTALL_PREFIX="$(PREFIX)" && $(MAKE)]], install_command = "cd build && $(MAKE) install" } @@ -8,109 +8,8 @@ #include <ctype.h> #include <sys/time.h> -# include <errno.h> -#include <fcntl.h> #include <unistd.h> #include <time.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <dirent.h> -#define NAMLEN(dirent) strlen((dirent)->d_name) - -#define SBINCREMENT 256 - -typedef struct { - char *buffer; - int maxlen; - int len; -} SB; - -static void -sbinit(SB *sb) -{ - sb->buffer = (char*)malloc(SBINCREMENT); - sb->maxlen = SBINCREMENT; - sb->len = 0; -} - -static char * -sbfree(SB *sb) -{ - if (sb->buffer) - free(sb->buffer); - sb->buffer = 0; - return 0; -} - -static void -sbgrow(SB *sb, int n) -{ - if (sb->buffer && sb->len + n > sb->maxlen) - { - int nlen = sb->maxlen; - while (sb->len + n > nlen) - nlen += SBINCREMENT; - sb->buffer = (char*)realloc(sb->buffer, nlen); - sb->maxlen = nlen; - } -} - -static void -sbadd1(SB *sb, char c) -{ - sbgrow(sb, 1); - if (sb->buffer) - sb->buffer[sb->len++] = c; -} - -static void -sbaddn(SB *sb, const char *s, int n) -{ - sbgrow(sb, n); - if (sb->buffer && s && n) - memcpy(sb->buffer + sb->len, s, n); - else if (sb->buffer && n) - sbfree(sb); - sb->len += n; -} - -static void -sbaddsf(SB *sb, char *s) -{ - if (s) - sbaddn(sb, s, strlen(s)); - else - sbfree(sb); - if (s) - free((void*)s); -} - -static void -sbslash(SB *sb) -{ - int i; - if (sb->buffer && sb->len) - for(i=0; i<sb->len; i++) - if (sb->buffer[i]=='\\') - sb->buffer[i]='/'; -} - -static int -sbpush(lua_State *L, SB *sb) -{ - sbslash(sb); - lua_pushlstring(L, sb->buffer, sb->len); - sbfree(sb); - return 1; -} - -static int -sbsetpush(lua_State *L, SB *sb, const char *s) -{ - sbfree(sb); - lua_pushstring(L, s); - return 1; -} static int l_clock(lua_State *L) { struct timeval tv; @@ -130,192 +29,9 @@ static int l_usleep(lua_State *L) { return 1; } -int l_fstat(lua_State *L) { - const char * fname = luaL_checkstring(L, 1); - int file=0; - if((file=open(fname,O_RDONLY)) < -1) - return 0; - struct stat fileStat; - if(fstat(file,&fileStat) < 0) - return 0; - lua_pushnumber(L, (double) fileStat.st_mtime); - lua_pushnumber(L, (double) fileStat.st_atime); - lua_pushnumber(L, (double) fileStat.st_ctime); - return 3; -} - -static int l_dirp(lua_State *L) { - const char *s = luaL_checkstring(L, 1); - struct stat buf; - if ((stat(s,&buf)==0) && (buf.st_mode & S_IFDIR)) - lua_pushboolean(L, 1); - else - lua_pushboolean(L, 0); - return 1; -} - -static int l_filep(lua_State *L) { - const char *s = luaL_checkstring(L, 1); - struct stat buf; - if ((stat(s,&buf) < 0) || (buf.st_mode & S_IFDIR)) - lua_pushboolean(L, 0); - else - lua_pushboolean(L, 1); - return 1; -} - -static int l_dirname(lua_State *L) { - const char *fname = luaL_checkstring(L, 1); - const char *s = fname; - const char *p = 0; - SB sb; - sbinit(&sb); - while (*s) { - if (s[0]=='/' && s[1] && s[1]!='/') - p = s; - s++; - } - if (!p) { - if (fname[0]=='/') - return sbsetpush(L, &sb, fname); - else - return sbsetpush(L, &sb, "."); - } - s = fname; - do { - sbadd1(&sb, *s++); - } while (s<p); - return sbpush(L, &sb); -} - -static int l_basename(lua_State *L) { - const char *fname = luaL_checkstring(L, 1); - const char *suffix = luaL_optstring(L, 2, 0); - int sl; - const char *s, *p; - SB sb; - sbinit(&sb); - /* Position p after last nontrivial slash */ - s = p = fname; - while (*s) { - if (s[0]=='/' && s[1] && s[1]!='/') - p = s + 1; - s++; - } - /* Copy into buffer */ - while (*p && *p!='/') - sbadd1(&sb, *p++); - /* Process suffix */ - if (suffix==0 || suffix[0]==0) - return sbpush(L, &sb); - if (suffix[0]=='.') - suffix += 1; - if (suffix[0]==0) - return sbpush(L, &sb); - sl = strlen(suffix); - if (sb.len > sl) { - s = sb.buffer + sb.len - (sl + 1); - if (s[0]=='.' && strncmp(s+1,suffix, sl)==0) - sb.len = s - sb.buffer; - } - return sbpush(L, &sb); -} - -static int l_pwd(lua_State *L) { - const char *s; - SB sb; - sbinit(&sb); - sbgrow(&sb, PATH_MAX); - s = getwd(sb.buffer); - if (! s) - return sbsetpush(L, &sb, "."); - sb.len += strlen(s); - 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; - SB sb; - sbinit(&sb); - - if (fname && fname[0]=='/') - sbadd1(&sb, '/'); - else - sbaddn(&sb, from, strlen(from)); - for (;;) { - while (fname && fname[0]=='/') - fname++; - if (!fname || !fname[0]) { - sbadd1(&sb, '/'); - while (sb.len > 1 && sb.buffer[sb.len-1]=='/') - sb.len --; - return sbpush(L, &sb); - } - if (fname[0]=='.') { - if (fname[1]=='/' || fname[1]==0) { - fname +=1; - continue; - } - if (fname[1]=='.') - if (fname[2]=='/' || fname[2]==0) { - fname +=2; - while (sb.len > 0 && sb.buffer[sb.len-1]=='/') - sb.len --; - while (sb.len > 0 && sb.buffer[sb.len-1]!='/') - sb.len --; - continue; - } - } - if (sb.len == 0 || sb.buffer[sb.len-1] != '/') - sbadd1(&sb, '/'); - while (*fname!=0 && *fname!='/') - sbadd1(&sb, *fname++); - } -} - -static int l_concat(lua_State *L) { - int i; - int narg = lua_gettop(L); - l_pwd(L); - for (i=1; i<=narg; i++) - { - concat_fname(L, luaL_checkstring(L, i)); - lua_remove(L, -2); - } - return 1; -} - static const struct luaL_reg routines [] = { {"clock", l_clock}, {"usleep", l_usleep}, - {"fstat", l_fstat}, - {"filep", l_filep}, - {"dirp", l_dirp}, - {"dirname", l_dirname}, - {"basename", l_basename}, - {"concat", l_concat}, - {"dir", l_dir}, - {"pwd", l_pwd}, {NULL, NULL} }; |