Added converge with opts
This commit is contained in:
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);
|
||||
}
|
Reference in New Issue
Block a user