1 #define PERL_NO_GET_CONTEXT
10 #include <loc/libloc.h>
11 #include <loc/database.h>
12 #include <loc/network.h>
15 MODULE = Location PACKAGE = Location
22 struct loc_ctx* ctx = NULL;
24 // Initialise location context
25 int err = loc_new(&ctx);
27 croak("Could not initialize libloc context: %d\n", err);
29 // Open the database file for reading
30 FILE* f = fopen(file, "r");
34 croak("Could not open file for reading: %s: %s\n",
35 file, strerror(errno));
39 struct loc_database* db = NULL;
40 err = loc_database_new(ctx, &db, f);
42 // We can close the database file straight away
43 // because loc_database_new creates a copy of the file descriptor
49 croak("Could not read database: %s\n", file);
60 get_country_code(db, address)
61 struct loc_database* db;
66 struct loc_network *network;
67 int err = loc_database_lookup_from_string(db, address, &network);
69 croak("Could not look up for %s\n", address);
72 // Extract the country code
73 const char* country_code = loc_network_get_country_code(network);
74 loc_network_unref(network);
77 croak("Could not get the country code for %s\n", address);
80 RETVAL = strdup(country_code);
87 database_get_vendor(db)
88 struct loc_database* db;
92 RETVAL = loc_database_get_vendor(db);
98 struct loc_database* db = NULL;
102 loc_database_unref(db);