lfs dir added as stead.dir

This commit is contained in:
p.kosyh 2011-03-10 10:40:55 +00:00
parent 70d8d18305
commit 692704aa50
5 changed files with 147 additions and 1 deletions

3
debian/changelog vendored
View file

@ -4,6 +4,9 @@ instead (1.3.4) unstable; urgency=low
* bug fix with imgl/imgr in inv;
* bug fix in video init;
* font faces { } syntax;
* new default font;
* set light hinting for font;
* added stead.dir iterator;
* modules doc fix;
* languages dir renamed to lang;

View file

@ -5,7 +5,7 @@ CFLAGS += $(SDL_CFLAGS) $(LUA_CFLAGS) $(ZLIB_CFLAGS) $(EXTRA_CFLAGS) -DLANG_PATH
LDFLAGS += $(SDL_LFLAGS) $(LUA_LFLAGS) $(ZLIB_LFLAGS) $(EXTRA_LDFLAGS)
SRC := graphics.c input.c game.c main.c instead.c sound.c SDL_rotozoom.c SDL_anigif.c SDL_gfxBlitFunc.c config.c themes.c menu.c util.c cache.c unzip.c ioapi.c unpack.c $(PLATFORM)
SRC := graphics.c input.c game.c main.c instead.c sound.c SDL_rotozoom.c SDL_anigif.c SDL_gfxBlitFunc.c config.c themes.c menu.c util.c cache.c unzip.c ioapi.c unpack.c lfs.c $(PLATFORM)
H := cache.h config.h externals.h game.h graphics.h input.h instead.h internals.h ioapi.h iowin32.h list.h \
menu.h SDL_anigif.h SDL_gfxBlitFunc.h SDL_rotozoom.h sound.h themes.h unzip.h util.h

View file

@ -528,6 +528,9 @@ static int luaB_theme_var(lua_State *L) {
return 0;
}
extern int dir_iter_factory (lua_State *L);
extern int luaopen_lfs (lua_State *L);
static const luaL_Reg base_funcs[] = {
{"doencfile", luaB_doencfile},
{"dofile", luaB_dofile},
@ -538,6 +541,7 @@ static const luaL_Reg base_funcs[] = {
{"get_steadpath", luaB_get_steadpath},
{"set_timer", luaB_set_timer},
{"theme_var", luaB_theme_var},
{"dir", dir_iter_factory},
{NULL, NULL}
};
@ -593,6 +597,7 @@ int instead_init(void)
luaL_openlibs(L);
luaL_register(L, "_G", base_funcs);
luaopen_lfs (L);
instead_package();
instead_lang();

137
src/sdl-instead/lfs.c Normal file
View file

@ -0,0 +1,137 @@
/* From:
** LuaFileSystem
** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
*/
#include "externals.h"
#define DIR_METATABLE "directory metatable"
typedef struct dir_data {
int closed;
#ifdef _WIN32
long hFile;
char pattern[MAX_PATH+1];
#else
DIR *dir;
#endif
} dir_data;
/*
** Directory iterator
*/
static int dir_iter (lua_State *L) {
#ifdef _WIN32
struct _finddata_t c_file;
#else
struct dirent *entry;
#endif
dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
luaL_argcheck (L, d->closed == 0, 1, "closed directory");
#ifdef _WIN32
if (d->hFile == 0L) { /* first entry */
if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
lua_pushnil (L);
lua_pushstring (L, "Error while iterating dir.");
d->closed = 1;
return 2;
} else {
lua_pushstring (L, c_file.name);
return 1;
}
} else { /* next entry */
if (_findnext (d->hFile, &c_file) == -1L) {
/* no more entries => close directory */
_findclose (d->hFile);
d->closed = 1;
return 0;
} else {
lua_pushstring (L, c_file.name);
return 1;
}
}
#else
if ((entry = readdir (d->dir)) != NULL) {
lua_pushstring (L, entry->d_name);
return 1;
} else {
/* no more entries => close directory */
closedir (d->dir);
d->closed = 1;
return 0;
}
#endif
}
/*
** Closes directory iterators
*/
static int dir_close (lua_State *L) {
dir_data *d = (dir_data *)lua_touserdata (L, 1);
#ifdef _WIN32
if (!d->closed && d->hFile) {
_findclose (d->hFile);
}
#else
if (!d->closed && d->dir) {
closedir (d->dir);
}
#endif
d->closed = 1;
return 0;
}
/*
** Factory of directory iterators
*/
int dir_iter_factory (lua_State *L) {
const char *path = luaL_checkstring (L, 1);
dir_data *d;
lua_pushcfunction (L, dir_iter);
d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
d->closed = 0;
#ifdef _WIN32
d->hFile = 0L;
luaL_getmetatable (L, DIR_METATABLE);
lua_setmetatable (L, -2);
if (strlen(path) > MAX_PATH-2)
luaL_error (L, "path too long: %s", path);
else
sprintf (d->pattern, "%s/*", path);
#else
luaL_getmetatable (L, DIR_METATABLE);
lua_setmetatable (L, -2);
d->dir = opendir (path);
if (d->dir == NULL)
luaL_error (L, "cannot open %s.");
#endif
return 2;
}
/*
** Creates directory metatable.
*/
static int dir_create_meta (lua_State *L) {
luaL_newmetatable (L, DIR_METATABLE);
/* set its __gc field */
lua_pushstring (L, "__index");
lua_newtable(L);
lua_pushstring (L, "next");
lua_pushcfunction (L, dir_iter);
lua_settable(L, -3);
lua_pushstring (L, "close");
lua_pushcfunction (L, dir_close);
lua_settable(L, -3);
lua_settable (L, -3);
lua_pushstring (L, "__gc");
lua_pushcfunction (L, dir_close);
lua_settable (L, -3);
return 1;
}
int luaopen_lfs (lua_State *L) {
dir_create_meta (L);
return 1;
}

View file

@ -8,6 +8,7 @@ stead = {
math = math,
io = io,
os = os,
dir = dir,
call_top = 0,
cctx = { txt = nil, self = nil },
-- functions = {}, -- code blocks