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 --- src/perl/Location.xs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/src/perl/Location.xs b/src/perl/Location.xs index 5693744..3c347db 100644 --- a/src/perl/Location.xs +++ b/src/perl/Location.xs @@ -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 #
The generated database is used by the testsuite of the perl binding.
Signed-off-by: Stefan Schantl stefan.schantl@ipfire.org --- examples/python/create-database.py | 3 +++ 1 file changed, 3 insertions(+)
diff --git a/examples/python/create-database.py b/examples/python/create-database.py index 0eaa945..b57ad94 100644 --- a/examples/python/create-database.py +++ b/examples/python/create-database.py @@ -20,6 +20,9 @@ with open(private_key_path, "r") as pkey: # Set a license w.license = "CC"
+ # Add a country + c = w.add_country("DE") + # Add an AS a = w.add_as(204867) a.name = "Lightning Wire Labs GmbH"
Signed-off-by: Stefan Schantl stefan.schantl@ipfire.org --- src/perl/t/Location.t | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/perl/t/Location.t b/src/perl/t/Location.t index 3ba2d8f..ec7f1e0 100644 --- a/src/perl/t/Location.t +++ b/src/perl/t/Location.t @@ -12,7 +12,7 @@ use warnings; my $testdb = $ENV{'database'}; my $keyfile = $ENV{'keyfile'};
-use Test::More tests => 7; +use Test::More tests => 8; BEGIN { use_ok('Location') };
######################### @@ -56,3 +56,6 @@ if(defined($as_number)) { fail("Test 8 - Lookup Autonomous System Number for add
$as_number = &Location::lookup_asn($db, "a.b.c.d"); if(defined($as_number)) { fail("Test 9 - Lookup Autonomous System Number for invalid address.") } + +my @locations = &Location::database_countries($db); +ok(@locations != 0, "Test 10 - Get database countries.");
Thank you. I merged these. The tests run through.
I won’t tag a release just yet and will wait for the remaining work to be completed.
Best, -Michael
On 20 Aug 2020, at 18:28, Stefan Schantl stefan.schantl@ipfire.org wrote:
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
src/perl/Location.xs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-)
diff --git a/src/perl/Location.xs b/src/perl/Location.xs index 5693744..3c347db 100644 --- a/src/perl/Location.xs +++ b/src/perl/Location.xs @@ -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
#
2.20.1