]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/perl/Location/Location.xs
perl: Rename database_get_vendor() to get_vendor(9
[people/ms/libloc.git] / src / perl / Location / Location.xs
index 02f447aaa8bdbf5402199588f32e1c268b33d89d..c4d7fa9ede801152066922f0aa65b616cbb0342c 100644 (file)
@@ -16,7 +16,7 @@ MODULE = Location             PACKAGE = Location
 
 struct loc_database *
 init(file)
-       char * file;
+       char* file;
 
        CODE:
                struct loc_ctx* ctx = NULL;
@@ -29,70 +29,73 @@ init(file)
                // 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_databasedb = 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