From 5fc5697825ead0b9eefa6a6e71e98f0ff070f2a3 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Sun, 24 Jul 2016 16:53:28 -0700 Subject: [PATCH] Added converge with opts --- c_lua_converge_optional_args/Makefile | 22 +++++++++ c_lua_converge_optional_args/fake.txt | 3 ++ c_lua_converge_optional_args/main.c | 65 +++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 c_lua_converge_optional_args/Makefile create mode 100644 c_lua_converge_optional_args/fake.txt create mode 100644 c_lua_converge_optional_args/main.c diff --git a/c_lua_converge_optional_args/Makefile b/c_lua_converge_optional_args/Makefile new file mode 100644 index 0000000..094100f --- /dev/null +++ b/c_lua_converge_optional_args/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_optional_args/fake.txt b/c_lua_converge_optional_args/fake.txt new file mode 100644 index 0000000..be5e8f3 --- /dev/null +++ b/c_lua_converge_optional_args/fake.txt @@ -0,0 +1,3 @@ +width = "Width" +heigt = "Height" +extra = "extra" diff --git a/c_lua_converge_optional_args/main.c b/c_lua_converge_optional_args/main.c new file mode 100644 index 0000000..0c5b92d --- /dev/null +++ b/c_lua_converge_optional_args/main.c @@ -0,0 +1,65 @@ +#define KRED "\x1B[31m" +#define KNRM "\x1B[0m" + +#include +#include +#include +#include +#include +#include + +void load(char *filename, int number_of_args, ...); +void error (lua_State *L, const char *fmt, ...); + +int main () +{ + char *filename="fake.txt"; + char width[50] = "width"; + char height[50] = "height"; + char extra[50] = "extra"; + + + + load(filename, 3, &width, &height, &extra); + printf("From Main width: %s height: %s\n extra: %s\n", width, height, extra); +} + +void load(char *filename, int number_of_args, ...){ + int i =1; + char test[100]; + + va_list opt_args; + + 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)); + va_start(opt_args, number_of_args); + + for(i = 0; i < number_of_args; i++) + { + strcpy(test, va_arg(opt_args, char*)); + lua_getglobal(L, test); + if (!lua_isstring(L, (i+1))){ + error(L, "%s Missing variable: %s%s\n",KRED, test, KNRM); + continue; + } + strcpy(test, lua_tostring(L, (i+1) )); + printf("Is Test: %s\n", test); + + } + va_end(opt_args); + + 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); +}