summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKamil Kaminski <kamilkss@gmail.com>2011-07-26 20:37:08 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-26 20:37:08 -0500
commitf167a9ea75ac76df9eca2fb49421a90e65c19a23 (patch)
tree85208769b73bfe229281907e199646591efc12c8
parent16b24f55cc3bb0a2258757ff37abb8baeecb5e97 (diff)
downloadluaload-master.tar.gz
luaload-master.tar.bz2
luaload-master.zip
convert to unix newlinesHEADmaster
-rw-r--r--luaload.c372
1 files changed, 186 insertions, 186 deletions
diff --git a/luaload.c b/luaload.c
index 0161fa8..05fe512 100644
--- a/luaload.c
+++ b/luaload.c
@@ -1,186 +1,186 @@
-/* luaload.c
- *
- * Kamil Kaminski
- * kkaminsk.com
- *
- * Loading a Lua table in C
- *
- *
- */
-
-#include <lua.h>
-#include <lauxlib.h>
-#include <lualib.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-struct platform
-{
- unsigned int xres_w;
- unsigned int yres_w;
- unsigned int bpp_w;
- float fovy_w;
- float znear_w;
- float zfar_w;
-};
-
-const char *LuaGetFieldString(lua_State *L, const char *);
-double luaGetFieldNumber(lua_State *, const char *);
-int luaLoadConfig(lua_State *, const char *);
-int luaFillPlatform(lua_State *, struct platform *);
-void printPlatform(struct platform *);
-
-const char *LuaGetFieldString(lua_State *L, const char *key)
-{
- if (!L || !key)
- return NULL;
-
- const char *ret = NULL;
-
- /* push the key string onto stack */
- lua_pushstring(L, key);
-
- lua_gettable(L, -2);
- if (!lua_isstring(L, -1))
- {
- fprintf(stderr, "Lua: \"%s\" field within the table is not a string\n", key);
- lua_pop(L, 1);
-
- return ret;
- }
-
- ret = lua_tostring(L, -1);
- lua_pop(L, 1);
-
- return ret;
-}
-
-double luaGetFieldNumber(lua_State *L, const char *key)
-{
- if (!L || !key)
- return -1.0;
-
- double ret = -1.0;
-
- /* push the key string onto stack */
- lua_pushstring(L, key);
-
- /* remember, -1 points to top of the stack, our table should a second
- * item from the top, this call effectively accesses t[key] and
- * pushes that value on the stack */
- lua_gettable(L, -2);
- if (!lua_isnumber(L, -1))
- {
- fprintf(stderr, "Lua: \"%s\" field within the table is NaN\n", key);
- lua_pop(L, 1);
-
- return ret;
- }
-
- /* finally retrieve our value */
- ret = lua_tonumber(L, -1);
-
- /* value is no longer needed, pop it */
- lua_pop(L, 1);
-
- return ret;
-}
-
-int luaLoadConfig(lua_State *L, const char *fname)
-{
- if (!L || !fname)
- return -1;
-
- int status;
-
- /* load the file containing the config */
- /* call a function, what function? dry run the script? */
- status = (luaL_loadfile(L, fname) || lua_pcall(L, 0, 0, 0));
- if (status)
- {
- fprintf(stderr, "Lua: %s\n", lua_tostring(L, -1));
- return -1;
- }
-
- return 0;
-}
-
-int luaFillPlatform(lua_State *L, struct platform *p)
-{
- if (!L || !p)
- return -1;
-
- double ret;
-
- /* pushes the table on the stack */
- lua_getglobal(L, "platform");
-
- if (!lua_istable(L, -1))
- {
- fprintf(stderr, "Lua: failed to parse \"platform\" table\n");
- lua_pop(L, 1);
- return -1;
- }
-
- if ((ret = luaGetFieldNumber(L, "xres_w")) != -1.0)
- p->xres_w = (unsigned int) ret;
-
- if ((ret = luaGetFieldNumber(L, "yres_w")) != -1.0)
- p->yres_w = (unsigned int) ret;
-
- if ((ret = luaGetFieldNumber(L, "bpp_w")) != -1.0)
- p->bpp_w = (unsigned int) ret;
-
- if ((ret = luaGetFieldNumber(L, "fovy_w")) != -1.0)
- p->fovy_w = (float) ret;
-
- if ((ret = luaGetFieldNumber(L, "znear_w")) != -1.0)
- p->znear_w = (float) ret;
-
- if ((ret = luaGetFieldNumber(L, "zfar_w")) != -1.0)
- p->zfar_w = (float) ret;
-
- /* pop the table */
- lua_pop(L, 1);
-
- return 0;
-}
-
-void printPlatform(struct platform *p)
-{
- if (!p)
- return;
-
- printf("xres_w : %u\n"
- "yres_w : %u\n"
- "bpp_w : %u\n"
- "fovy_w : %f\n"
- "znear_w: %f\n"
- "zfar_w : %f",
- p->xres_w, p->yres_w, p->bpp_w, p->fovy_w,
- p->znear_w, p->zfar_w);
- puts("");
-}
-
-int main(int argc, char **argv)
-{
- lua_State *L;
-
- /* create new lua state */
- L = luaL_newstate();
-
- /* load lua libraries */
- luaL_openlibs(L);
-
- struct platform p;
- memset(&p, 0, sizeof(p));
- luaLoadConfig(L, "config.lua");
- luaFillPlatform(L, &p);
- printPlatform(&p);
-
- lua_close(L);
-
- return 0;
-}
-
+/* luaload.c
+ *
+ * Kamil Kaminski
+ * kkaminsk.com
+ *
+ * Loading a Lua table in C
+ *
+ *
+ */
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct platform
+{
+ unsigned int xres_w;
+ unsigned int yres_w;
+ unsigned int bpp_w;
+ float fovy_w;
+ float znear_w;
+ float zfar_w;
+};
+
+const char *LuaGetFieldString(lua_State *L, const char *);
+double luaGetFieldNumber(lua_State *, const char *);
+int luaLoadConfig(lua_State *, const char *);
+int luaFillPlatform(lua_State *, struct platform *);
+void printPlatform(struct platform *);
+
+const char *LuaGetFieldString(lua_State *L, const char *key)
+{
+ if (!L || !key)
+ return NULL;
+
+ const char *ret = NULL;
+
+ /* push the key string onto stack */
+ lua_pushstring(L, key);
+
+ lua_gettable(L, -2);
+ if (!lua_isstring(L, -1))
+ {
+ fprintf(stderr, "Lua: \"%s\" field within the table is not a string\n", key);
+ lua_pop(L, 1);
+
+ return ret;
+ }
+
+ ret = lua_tostring(L, -1);
+ lua_pop(L, 1);
+
+ return ret;
+}
+
+double luaGetFieldNumber(lua_State *L, const char *key)
+{
+ if (!L || !key)
+ return -1.0;
+
+ double ret = -1.0;
+
+ /* push the key string onto stack */
+ lua_pushstring(L, key);
+
+ /* remember, -1 points to top of the stack, our table should a second
+ * item from the top, this call effectively accesses t[key] and
+ * pushes that value on the stack */
+ lua_gettable(L, -2);
+ if (!lua_isnumber(L, -1))
+ {
+ fprintf(stderr, "Lua: \"%s\" field within the table is NaN\n", key);
+ lua_pop(L, 1);
+
+ return ret;
+ }
+
+ /* finally retrieve our value */
+ ret = lua_tonumber(L, -1);
+
+ /* value is no longer needed, pop it */
+ lua_pop(L, 1);
+
+ return ret;
+}
+
+int luaLoadConfig(lua_State *L, const char *fname)
+{
+ if (!L || !fname)
+ return -1;
+
+ int status;
+
+ /* load the file containing the config */
+ /* call a function, what function? dry run the script? */
+ status = (luaL_loadfile(L, fname) || lua_pcall(L, 0, 0, 0));
+ if (status)
+ {
+ fprintf(stderr, "Lua: %s\n", lua_tostring(L, -1));
+ return -1;
+ }
+
+ return 0;
+}
+
+int luaFillPlatform(lua_State *L, struct platform *p)
+{
+ if (!L || !p)
+ return -1;
+
+ double ret;
+
+ /* pushes the table on the stack */
+ lua_getglobal(L, "platform");
+
+ if (!lua_istable(L, -1))
+ {
+ fprintf(stderr, "Lua: failed to parse \"platform\" table\n");
+ lua_pop(L, 1);
+ return -1;
+ }
+
+ if ((ret = luaGetFieldNumber(L, "xres_w")) != -1.0)
+ p->xres_w = (unsigned int) ret;
+
+ if ((ret = luaGetFieldNumber(L, "yres_w")) != -1.0)
+ p->yres_w = (unsigned int) ret;
+
+ if ((ret = luaGetFieldNumber(L, "bpp_w")) != -1.0)
+ p->bpp_w = (unsigned int) ret;
+
+ if ((ret = luaGetFieldNumber(L, "fovy_w")) != -1.0)
+ p->fovy_w = (float) ret;
+
+ if ((ret = luaGetFieldNumber(L, "znear_w")) != -1.0)
+ p->znear_w = (float) ret;
+
+ if ((ret = luaGetFieldNumber(L, "zfar_w")) != -1.0)
+ p->zfar_w = (float) ret;
+
+ /* pop the table */
+ lua_pop(L, 1);
+
+ return 0;
+}
+
+void printPlatform(struct platform *p)
+{
+ if (!p)
+ return;
+
+ printf("xres_w : %u\n"
+ "yres_w : %u\n"
+ "bpp_w : %u\n"
+ "fovy_w : %f\n"
+ "znear_w: %f\n"
+ "zfar_w : %f",
+ p->xres_w, p->yres_w, p->bpp_w, p->fovy_w,
+ p->znear_w, p->zfar_w);
+ puts("");
+}
+
+int main(int argc, char **argv)
+{
+ lua_State *L;
+
+ /* create new lua state */
+ L = luaL_newstate();
+
+ /* load lua libraries */
+ luaL_openlibs(L);
+
+ struct platform p;
+ memset(&p, 0, sizeof(p));
+ luaLoadConfig(L, "config.lua");
+ luaFillPlatform(L, &p);
+ printPlatform(&p);
+
+ lua_close(L);
+
+ return 0;
+}
+