Added c_lua_converge

This commit is contained in:
Logen Kain 2016-07-23 23:58:45 -07:00
parent f15f3ea7ba
commit d56cfc0a46
4 changed files with 72 additions and 0 deletions

1
c_lua_converge/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
test

22
c_lua_converge/Makefile Normal file
View 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

2
c_lua_converge/fake.txt Normal file
View File

@ -0,0 +1,2 @@
width = "My Width"
height = "My Height"

47
c_lua_converge/main.c Normal file
View File

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
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);
}