Added converge with opts
This commit is contained in:
parent
db2a8f70e7
commit
5fc5697825
22
c_lua_converge_optional_args/Makefile
Normal file
22
c_lua_converge_optional_args/Makefile
Normal file
@ -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
|
||||
|
3
c_lua_converge_optional_args/fake.txt
Normal file
3
c_lua_converge_optional_args/fake.txt
Normal file
@ -0,0 +1,3 @@
|
||||
width = "Width"
|
||||
heigt = "Height"
|
||||
extra = "extra"
|
65
c_lua_converge_optional_args/main.c
Normal file
65
c_lua_converge_optional_args/main.c
Normal file
@ -0,0 +1,65 @@
|
||||
#define KRED "\x1B[31m"
|
||||
#define KNRM "\x1B[0m"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user