From 692704aa50946aa8576b611b8ca53286d41eade7 Mon Sep 17 00:00:00 2001
From: "p.kosyh"
Date: Thu, 10 Mar 2011 10:40:55 +0000
Subject: [PATCH] lfs dir added as stead.dir
---
debian/changelog | 3 +
src/sdl-instead/Makefile | 2 +-
src/sdl-instead/instead.c | 5 ++
src/sdl-instead/lfs.c | 137 ++++++++++++++++++++++++++++++++++++++
stead/stead.lua | 1 +
5 files changed, 147 insertions(+), 1 deletion(-)
create mode 100644 src/sdl-instead/lfs.c
diff --git a/debian/changelog b/debian/changelog
index 0872f6b..688fd4a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -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;
diff --git a/src/sdl-instead/Makefile b/src/sdl-instead/Makefile
index d2b822f..0938c1d 100644
--- a/src/sdl-instead/Makefile
+++ b/src/sdl-instead/Makefile
@@ -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
diff --git a/src/sdl-instead/instead.c b/src/sdl-instead/instead.c
index 9858678..50e891c 100644
--- a/src/sdl-instead/instead.c
+++ b/src/sdl-instead/instead.c
@@ -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();
diff --git a/src/sdl-instead/lfs.c b/src/sdl-instead/lfs.c
new file mode 100644
index 0000000..8502d1b
--- /dev/null
+++ b/src/sdl-instead/lfs.c
@@ -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;
+}
diff --git a/stead/stead.lua b/stead/stead.lua
index 82a4221..e49a3c1 100644
--- a/stead/stead.lua
+++ b/stead/stead.lua
@@ -8,6 +8,7 @@ stead = {
math = math,
io = io,
os = os,
+ dir = dir,
call_top = 0,
cctx = { txt = nil, self = nil },
-- functions = {}, -- code blocks