struct loc_database *
init(file)
- char * file;
+ char* file;
CODE:
struct loc_ctx* ctx = NULL;
// Open the database file for reading
FILE* f = fopen(file, "r");
if (!f) {
+ loc_unref(ctx);
+
croak("Could not open file for reading: %s: %s\n",
file, strerror(errno));
}
// Parse the database
- struct loc_database *db = NULL;
+ struct loc_database* db = NULL;
err = loc_database_new(ctx, &db, f);
+
+ // We can close the database file straight away
+ // because loc_database_new creates a copy of the file descriptor
+ fclose(f);
+
if (err) {
+ loc_unref(ctx);
+
croak("Could not read database: %s\n", file);
}
+ // Cleanup
+ loc_unref(ctx);
+
RETVAL = db;
OUTPUT:
RETVAL
char*
get_country_code(db, address)
- struct loc_database* db = NULL;
- char* address = NULL;
+ struct loc_database* db;
+ char* address;
CODE:
- int err;
- const char* country_code = NULL;
-
// Lookup network
struct loc_network *network;
- err = loc_database_lookup_from_string(db, address, &network);
+ int err = loc_database_lookup_from_string(db, address, &network);
if (err) {
croak("Could not look up for %s\n", address);
}
- country_code = loc_network_get_country_code(network);
+ // Extract the country code
+ const char* country_code = loc_network_get_country_code(network);
loc_network_unref(network);
- if (!country_code) {
- croak("Could not get the country code for %s\n", address);
+ if (country_code) {
+ RETVAL = strdup(country_code);
+ } else {
+ RETVAL = NULL;
}
-
- RETVAL = strdup(country_code);
OUTPUT:
RETVAL
-char*
-database_get_vendor(db)
- struct loc_database* db = NULL;
+const char*
+get_vendor(db)
+ struct loc_database* db;
CODE:
- const char* vendor = NULL;
-
// Get vendor
- vendor = loc_database_get_vendor(db);
- if (!vendor) {
- croak("Could not retrieve vendor\n");
- }
-
- RETVAL = strdup(vendor);
+ RETVAL = loc_database_get_vendor(db);
OUTPUT:
RETVAL
void
DESTROY(db)
- struct loc_database* db = NULL;
+ struct loc_database* db;
CODE:
// Close database