[1/4] perl: Add get_country_name() function.

Message ID 20200910155747.12553-1-stefan.schantl@ipfire.org
State Accepted
Commit 201bfcd9890198b399530bdf59623d4fe0f993f4
Headers
Series [1/4] perl: Add get_country_name() function. |

Commit Message

Stefan Schantl Sept. 10, 2020, 3:57 p.m. UTC
  This function can be used to get the stored name for a given country
code.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 src/perl/Location.xs | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
  

Patch

diff --git a/src/perl/Location.xs b/src/perl/Location.xs
index 1cb2e21..dcf3f0d 100644
--- a/src/perl/Location.xs
+++ b/src/perl/Location.xs
@@ -243,6 +243,28 @@  lookup_asn(db, address)
 #
 # Get functions
 #
+SV*
+get_country_name(db, ccode)
+	struct loc_database* db;
+	char* ccode;
+
+	CODE:
+		RETVAL = &PL_sv_undef;
+
+		// Lookup country code
+		struct loc_country *country;
+		int err = loc_database_get_country(db, &country, ccode);
+		if(!err) {
+			// Extract the name for the given country code.
+			const char* country_name = loc_country_get_name(country);
+			RETVAL = newSVpv(country_name, strlen(country_name));
+
+			loc_country_unref(country);
+		}
+
+	OUTPUT:
+		RETVAL
+
 SV*
 get_continent_code(db, ccode)
 	struct loc_database* db;