summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouie Shprung <lshprung@tutanota.com>2024-08-07 22:34:44 -0400
committerLouie Shprung <lshprung@tutanota.com>2024-08-07 22:34:44 -0400
commita18a8b783d71859e01c550b0acf6e0cefbef0d9f (patch)
treec113e31d614aea194e6871eda19006d91ea03b1e
parenta23c315905c126667f13091989ca2879b5337582 (diff)
Working demo for loading groups and entries from lua config
-rw-r--r--.gitignore3
-rw-r--r--src/include/read_cfg.h2
-rw-r--r--src/read_cfg.c44
3 files changed, 45 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..540814b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.cache
+*~
+*.tar.gz
diff --git a/src/include/read_cfg.h b/src/include/read_cfg.h
index df3e5bd..a76e55e 100644
--- a/src/include/read_cfg.h
+++ b/src/include/read_cfg.h
@@ -1,6 +1,8 @@
#ifndef READ_CFG_H
#define READ_CFG_H
+#include <stdbool.h>
+
#define BUF_LEN 1024
bool cfg_interp(char *path);
diff --git a/src/read_cfg.c b/src/read_cfg.c
index fa8a424..940630b 100644
--- a/src/read_cfg.c
+++ b/src/read_cfg.c
@@ -55,16 +55,51 @@ bool cfg_interp(char *path){
// load lua configuration
lua_State *L = luaL_newstate();
- int config_load_status = luaL_dofile(L, path);
+ int config_load_status = luaL_loadfile(L, path);
if(config_load_status != 0) {
printf("Error: could not load configuration \"%s\"\nis there a syntax error?\n", path);
return false;
}
+ // set up base configuration variables
+ // TODO set helper variables and functions (e.g., so that Groups table doesn't need to be manually created in the user's config)
+ //lua_newtable(L);
+ //lua_setglobal(L, "Groups");
+ lua_pcall(L, 0, 0, 0);
+
// demo
- lua_getglobal(L, "Message");
- const char *message = lua_tostring(L, -1);
- printf("message: %s\n", message);
+ lua_getglobal(L, "Groups");
+ i = lua_gettop(L);
+ // add each group
+ const char *group_name;
+ lua_pushnil(L);
+ while(lua_next(L, i)) {
+ // the key is index -2, value is -1
+ if(lua_type(L, -2) != LUA_TSTRING) continue; // skip if invalid key
+ group_name = lua_tostring(L, -2);
+ group_add(group_name, NULL);
+ // add each entry
+ if(lua_type(L, -1) != LUA_TTABLE) continue; // skip if invalid value
+ lua_pushstring(L, "Entries");
+ lua_gettable(L, -2);
+ j = lua_gettop(L);
+ lua_pushnil(L);
+ while(lua_next(L, j)) {
+ if(lua_type(L, -1) != LUA_TSTRING) continue; // skip if invalid value
+ handle_fname(lua_tostring(L, -1), group_name, false, true, NULL, -1);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 2);
+ }
+
+ //lua_getfield(L, -1, "Pictures");
+ //lua_getfield(L, -1, "Entries");
+ //lua_rawgeti(L, -1, 1);
+ //printf("message: %d\n", (int)lua_tonumber(L, -1));
+ //lua_rawgeti(L, -2, 2);
+ //printf("message: %d\n", (int)lua_tonumber(L, -1));
+ //lua_rawgeti(L, -3, 3);
+ //printf("message: %d\n", (int)lua_tonumber(L, -1));
lua_close(L);
return true;
@@ -89,6 +124,7 @@ bool cfg_interp(char *path){
//Read each line of "config"
while(fgets(buffer, BUF_LEN, fp)){
+ printf("%d\n", i);
i++;
check_line(buffer, options, i);
}