From: Michael Tremer Date: Fri, 5 Apr 2024 15:36:14 +0000 (+0000) Subject: lua: Create a simple iterator for all networks X-Git-Tag: 0.9.18~68 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cbc4c6a7be5dc8b9ea37edbcfa395a373b429b98;p=location%2Flibloc.git lua: Create a simple iterator for all networks Signed-off-by: Michael Tremer --- diff --git a/src/lua/database.c b/src/lua/database.c index aada739..ae769f7 100644 --- a/src/lua/database.c +++ b/src/lua/database.c @@ -217,6 +217,50 @@ static int Database_verify(lua_State* L) { return 1; } +static int Database_next_network(lua_State* L) { + struct loc_network* network = NULL; + int r; + + struct loc_database_enumerator* e = lua_touserdata(L, lua_upvalueindex(1)); + + // Fetch the next network + r = loc_database_enumerator_next_network(e, &network); + if (r) + return luaL_error(L, "Could not fetch network: %s\n", strerror(errno)); + + // If we have received no network, we have reached the end + if (!network) { + lua_pushnil(L); + return 1; + } + + // Create a network object + r = create_network(L, network); + loc_network_unref(network); + + return r; +} + +static int Database_list_networks(lua_State* L) { + struct loc_database_enumerator* e = NULL; + int r; + + Database* self = luaL_checkdatabase(L, 1); + + // Create a new enumerator + r = loc_database_enumerator_new(&e, self->db, LOC_DB_ENUMERATE_NETWORKS, 0); + if (r) + return luaL_error(L, "Could not create enumerator: %s\n", strerror(errno)); + + // Push the enumerator onto the stack + lua_pushlightuserdata(L, e); + + // Push the closure onto the stack + lua_pushcclosure(L, Database_next_network, 1); + + return 1; +} + static const struct luaL_Reg database_functions[] = { { "get_as", Database_get_as }, { "get_description", Database_get_description }, @@ -225,6 +269,7 @@ static const struct luaL_Reg database_functions[] = { { "get_vendor", Database_get_vendor }, { "open", Database_open }, { "lookup", Database_lookup }, + { "list_networks", Database_list_networks }, { "verify", Database_verify }, { "__gc", Database_gc }, { NULL, NULL }, diff --git a/tests/lua/main.lua b/tests/lua/main.lua index 4a7fc5d..a824513 100755 --- a/tests/lua/main.lua +++ b/tests/lua/main.lua @@ -151,4 +151,15 @@ function test_gc() print("GC: " .. collectgarbage("collect")) end +function test_list_networks() + location = require("location") + + -- Open the database + db = location.Database.open(ENV_TEST_DATABASE) + + for network in db:list_networks() do + print(network) + end +end + os.exit(luaunit.LuaUnit.run())