From d56cfc0a46e0acce36cbd5266e9ff4b660fca569 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Sat, 23 Jul 2016 23:58:45 -0700 Subject: [PATCH] Added c_lua_converge --- c_lua_converge/.gitignore | 1 + c_lua_converge/Makefile | 22 ++++++++++++++++++ c_lua_converge/fake.txt | 2 ++ c_lua_converge/main.c | 47 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 c_lua_converge/.gitignore create mode 100644 c_lua_converge/Makefile create mode 100644 c_lua_converge/fake.txt create mode 100644 c_lua_converge/main.c diff --git a/c_lua_converge/.gitignore b/c_lua_converge/.gitignore new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/c_lua_converge/.gitignore @@ -0,0 +1 @@ +test diff --git a/c_lua_converge/Makefile b/c_lua_converge/Makefile new file mode 100644 index 0000000..094100f --- /dev/null +++ b/c_lua_converge/Makefile @@ -0,0 +1,22 @@ +#OBJS specifies which files to compile as part of the project +OBJS = main.c + +#CC specifies which compiler to use +CC = clang + +#COMPILER_FLAGS specifies the additional compilation options we're using +# -w suppress all warnings +COMPILER_FLAGS = -Wall + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = -llua + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= test + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) +clean : + rm test + diff --git a/c_lua_converge/fake.txt b/c_lua_converge/fake.txt new file mode 100644 index 0000000..93e6820 --- /dev/null +++ b/c_lua_converge/fake.txt @@ -0,0 +1,2 @@ +width = "My Width" +height = "My Height" diff --git a/c_lua_converge/main.c b/c_lua_converge/main.c new file mode 100644 index 0000000..0cd4836 --- /dev/null +++ b/c_lua_converge/main.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include + +void load(char *filename, char *width, char *height); +void error (lua_State *L, const char *fmt, ...); + +int main (void) { + char width[100]; + char height[100]; + char *w = width; + char *h = height; + load("fake.txt", w, h); + printf("Height: %s Width: %s\n",height, width); + return 0; +} + +void load(char *filename, char *width, char *height){ + + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0)) + error(L, "cannot run configuration file: %s", + lua_tostring(L, -1)); + + lua_getglobal(L, "width"); + lua_getglobal(L, "height"); + + strcpy(height, lua_tostring(L, -1)); + strcpy(width, lua_tostring(L, -2)); + + lua_close(L); +} + +void error (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + vfprintf(stderr,fmt, argp); + va_end(argp); + lua_close(L); + exit(EXIT_FAILURE); +}