67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#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[number_of_args];
 | 
						|
 | 
						|
	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++)
 | 
						|
	{
 | 
						|
		test[i] = va_arg(opt_args, char*);
 | 
						|
		lua_getglobal(L, test[i]);
 | 
						|
		if (!lua_isstring(L, (i+1))){
 | 
						|
					error(L, "%s Missing variable: %s%s\n",KRED, test[i], KNRM);
 | 
						|
					strcpy(test[i], " ");
 | 
						|
					continue;
 | 
						|
					}
 | 
						|
		strcpy(test[i], lua_tostring(L, (i+1) ));
 | 
						|
	  //printf("Is Test: %s\n", test[i]);
 | 
						|
	
 | 
						|
	}
 | 
						|
	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);
 | 
						|
}
 |