]> git.ipfire.org Git - location/libloc.git/commitdiff
perl: Add database_countries() function.
authorStefan Schantl <stefan.schantl@ipfire.org>
Thu, 20 Aug 2020 17:28:54 +0000 (19:28 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 21 Aug 2020 09:55:53 +0000 (09:55 +0000)
This function is used to the stored countries of a database, which
easily can be assigned to an array.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/perl/Location.xs

index 5693744f77f6a8565a411c3d081fb0566e07cbca..3c347db88f2331f574e55c4b1bea5d2ebe7e9ac9 100644 (file)
@@ -6,11 +6,10 @@
 #include <stdio.h>
 #include <string.h>
 
-
 #include <loc/libloc.h>
 #include <loc/database.h>
 #include <loc/network.h>
-
+#include <loc/country.h>
 
 MODULE = Location              PACKAGE = Location
 
@@ -119,6 +118,45 @@ get_license(db)
        OUTPUT:
                RETVAL
 
+void
+database_countries(db)
+       struct loc_database* db;
+
+       PPCODE:
+               // Create Database enumerator
+               struct loc_database_enumerator* enumerator;
+               int err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_COUNTRIES);
+
+               if (err) {
+                       croak("Could not create a database enumerator\n");
+               }
+
+               // Init and enumerate first country.
+               struct loc_country* country;
+               err = loc_database_enumerator_next_country(enumerator, &country);
+               if (err) {
+                       croak("Could not enumerate next country\n");
+               }
+
+               while (country) {
+                       // Extract the country code.
+                       const char* ccode = loc_country_get_code(country);
+
+                       // Push country code.
+                       XPUSHs(sv_2mortal(newSVpv(ccode, 2)));
+
+                       // Unref country pointer.
+                       loc_country_unref(country);
+
+                       // Enumerate next item.
+                       err = loc_database_enumerator_next_country(enumerator, &country);
+                       if (err) {
+                               croak("Could not enumerate next country\n");
+                       }
+               }
+
+               loc_database_enumerator_unref(enumerator);
+
 #
 # Lookup functions
 #