Lua as a Configuration And Data Exchange Language For "C"

config.lua

printers = {
    printer1 = "Foo branch",
    printer2 = "Fek bhanch",
    printer3 = "Fuk branch" 
}
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "lua5.2/lua.h"
#include "lua5.2/lauxlib.h"
#include "lua5.2/lualib.h"
#define CONFIG_FILE "config.lua"
 
static char * getconf (char *mod, char *key)
{
  char *res;
 
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);
 
  if (luaL_loadfile(L, CONFIG_FILE) || lua_pcall(L, 0, 0, 0))
      {
        fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
      }
 
  lua_getglobal(L, mod);
 
  if(!lua_istable(L, -1))
      {
        fprintf(stderr, "Table type error: %s\n", (lua_tostring(L, -1)) );
      }
 
  lua_getfield(L, -1, key);
 
   if(!lua_isstring(L, -1))
       {
         fprintf(stderr, "String type error: %s\n", (lua_tostring(L, -1)));
       }
  res = strdup((char *)lua_tostring(L, -1));
  lua_pop(L, 1);
  lua_close(L);
  return res;
}
int main(int argc, char **argv)
{
static char *device;
device = getconf("printers", "printer1");
}

https://www.netbsd.org/~mbalmer/lua/lua_config.pdf

Только авторизованные участники могут оставлять комментарии.
  • blog/lua_as_config_for_c.txt
  • Последние изменения: 2019/11/28