diff options
| author | amy <[email protected]> | 2025-04-14 14:09:22 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-04-14 14:09:22 -0500 |
| commit | dd7a8af4050454c3901987bff24a77334f892cc4 (patch) | |
| tree | 48c6107656e14cfbbcbb49424fc3454de850a5db /src/config.c | |
| parent | 44c68aa7d51ea6b50c442bfbfa4ce11c530d2f7d (diff) | |
| parent | dc7e4527e88ed0c59e17c0ff04c01e1c92136e42 (diff) | |
Merge pull request #1 from ameliasquires/new-config
updates how config values are updated, support for local values when copying states, and annotations
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 115 |
1 files changed, 81 insertions, 34 deletions
diff --git a/src/config.c b/src/config.c index bbbfa19..ad21b0c 100644 --- a/src/config.c +++ b/src/config.c @@ -2,42 +2,89 @@ #include "io.h" #include <string.h> -int _print_type = 0; -int _max_depth = 2; -int _start_nl_at = 3; -int _collapse_all = 0; -int _collapse_to_memory = 1; -int _print_meta = 0; - -int _file_malloc_chunk = 512; - -int get_config_map(const char* key){ - for(int i = 0; config_map[i].key != NULL; i++){ - if(strcmp(config_map[i].key,key) == 0) return i; +int m_config_index(lua_State* L){ + lua_pushstring(L, "_config"); + lua_gettable(L, 1); + struct config* conf = lua_touserdata(L, -1); + int at = 0; + struct config cc; + + for(;;at++){ + cc = conf[at]; + + if(cc.type == c_none){ + lua_pushnil(L); + return 1; + } + + if(strcmp(cc.name, lua_tostring(L, 2)) == 0) { + break; + } } - return -1; + + switch(cc.type){ + case c_int: + lua_pushinteger(L, *cc.value.c_int); + break; + case c_string: + lua_pushlstring(L, *cc.value.c_string, *cc.value.len); + break; + case c_number: + lua_pushnumber(L, *cc.value.c_number); + break; + case c_none:; + } + + return 1; } -int l_set(lua_State* L) { - luaL_checktype(L, 1, LUA_TTABLE); - get_config_map("print_type"); - lua_pushnil(L); - for(;lua_next(L,1) != 0;){ - int ind = get_config_map(lua_tostring(L,-2)); - - if(ind != -1) { - int key; - if(lua_isboolean(L, -1)){ - key = lua_toboolean(L, -1); - } else { - key = lua_tonumber(L, -1); - } - *config_map[ind].value = key; - //lua_pushnumber(L, 1); - //return 1; - } - lua_pop(L,1); +int m_config_newindex(lua_State* L){ + lua_pushstring(L, "_config"); + lua_gettable(L, 1); + struct config* conf = lua_touserdata(L, -1); + int at = 0; + struct config cc; + + for(;;at++){ + cc = conf[at]; + + if(cc.type == c_none){ + lua_pushnil(L); + return 1; } - lua_pushnumber(L, 0); - return 1; + + if(strcmp(cc.name, lua_tostring(L, 2)) == 0) { + break; + } + } + + switch(cc.type){ + case c_int: + *cc.value.c_int = lua_tointeger(L, 3); + break; + case c_string: + *cc.value.c_string = (char*)luaL_tolstring(L, 3, cc.value.len); + break; + case c_number: + *cc.value.c_number = lua_tonumber(L, 3); + break; + case c_none:; + } + + return 1; +}; + +int i_config_metatable(lua_State* L, struct config* conf){ + int idx = lua_gettop(L); + luaI_tsetlud(L, idx, "_config", conf); + + lua_newtable(L); + int meta_idx = lua_gettop(L); + luaI_tsetcf(L, meta_idx, "__index", m_config_index); + luaI_tsetcf(L, meta_idx, "__newindex", m_config_newindex); + + lua_pushvalue(L, meta_idx); + lua_setmetatable(L, idx); + + return 1; } |
