diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2011-02-21 14:35:00 +0000 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2011-04-20 17:46:34 +0200 |
commit | 35bba3de1f4d6464dc03ffc60b6a6b6ec25ef6b0 (patch) | |
tree | 253ebbccdda4b2a8ce36386513a7719967363be0 | |
parent | 2804d01d89ca79082f15e217e46269d6ef7e5c1f (diff) | |
download | apk-tools-35bba3de1f4d6464dc03ffc60b6a6b6ec25ef6b0.tar.bz2 apk-tools-35bba3de1f4d6464dc03ffc60b6a6b6ec25ef6b0.tar.xz |
lua: implement iterator with installed packages
-rw-r--r-- | src/lua-apk.c | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/src/lua-apk.c b/src/lua-apk.c index 779dfd1..07cbc1c 100644 --- a/src/lua-apk.c +++ b/src/lua-apk.c @@ -9,7 +9,8 @@ #include "apk_database.h" #define LIBNAME "apk" -#define APKDB_META "apk_database" +#define APK_DB_META "apk_database" +#define APK_IPKG_META "apk_installed_package" int apk_verbosity; unsigned int apk_flags; @@ -131,9 +132,9 @@ static struct apk_database *checkdb(lua_State *L, int index) { struct apk_database *db; luaL_checktype(L, index, LUA_TUSERDATA); - db = (struct apk_database *) luaL_checkudata(L, index, APKDB_META); + db = (struct apk_database *) luaL_checkudata(L, index, APK_DB_META); if (db == NULL) - luaL_typerror(L, index, APKDB_META); + luaL_typerror(L, index, APK_DB_META); return db; } @@ -152,7 +153,7 @@ static int Papk_db_open(lua_State *L) opts.open_flags |= APK_OPENF_READ; db = lua_newuserdata(L, sizeof(struct apk_database)); - luaL_getmetatable(L, APKDB_META); + luaL_getmetatable(L, APK_DB_META); lua_setmetatable(L, -2); r = apk_db_open(db, &opts); @@ -224,6 +225,42 @@ ret_nil: return 1; } +// Iterator of all installed packages +struct apk_installed_package_iterator { + struct list_head *end; + struct apk_installed_package *node; +}; + +static int iterate_installed(lua_State *L) +{ + struct apk_installed_package_iterator *i; + struct apk_installed_package *ipkg; + i = (struct apk_installed_package_iterator *)lua_touserdata(L, lua_upvalueindex(1)); + ipkg = i->node; + + if (&ipkg->installed_pkgs_list == i->end) + return 0; + + i->node = list_entry(ipkg->installed_pkgs_list.next, + typeof(*ipkg), installed_pkgs_list); + return push_package(L, ipkg->pkg); + +} +static int Pinstalled(lua_State *L) +{ + struct apk_database *db = checkdb(L, 1); + struct apk_installed_package_iterator *i; + + i = (struct apk_installed_package_iterator *) lua_newuserdata(L, sizeof(*i)); + i->end = &db->installed.packages; + i->node = list_entry((&db->installed.packages)->next, + struct apk_installed_package, + installed_pkgs_list); + + lua_pushcclosure(L, iterate_installed, 1); + return 1; +} + static const luaL_reg reg_apk_methods[] = { {"version_validate", Pversion_validate}, {"version_compare", Pversion_compare}, @@ -232,6 +269,7 @@ static const luaL_reg reg_apk_methods[] = { {"who_owns", Papk_who_owns}, {"exists", Papk_exists}, {"is_installed", Papk_exists}, + {"installed", Pinstalled}, {NULL, NULL} }; @@ -247,7 +285,7 @@ LUALIB_API int luaopen_apk(lua_State *L) lua_pushliteral(L, APK_VERSION); lua_settable(L, -3); - luaL_newmetatable(L, APKDB_META); + luaL_newmetatable(L, APK_DB_META); luaL_register(L, NULL, reg_apk_db_meta_methods); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); |