]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/country.c
location-importer: Include all overridden networks
[people/ms/libloc.git] / src / country.c
index ee40d888b576d38f575b9e57a79f122116aafa1e..2ba93e6ef7d5c225d032b8b307ad76942659b0e0 100644 (file)
@@ -19,6 +19,7 @@
 #include <string.h>
 
 #include <loc/libloc.h>
+#include <loc/compat.h>
 #include <loc/country.h>
 #include <loc/private.h>
 
@@ -113,17 +114,20 @@ LOC_EXPORT int loc_country_set_name(struct loc_country* country, const char* nam
        return 0;
 }
 
-int loc_country_cmp(struct loc_country* country1, struct loc_country* country2) {
+LOC_EXPORT int loc_country_cmp(struct loc_country* country1, struct loc_country* country2) {
        return strcmp(country1->code, country2->code);
 }
 
-int loc_country_new_from_database_v0(struct loc_ctx* ctx, struct loc_stringpool* pool,
-               struct loc_country** country, const struct loc_database_country_v0* dbobj) {
+int loc_country_new_from_database_v1(struct loc_ctx* ctx, struct loc_stringpool* pool,
+               struct loc_country** country, const struct loc_database_country_v1* dbobj) {
        char buffer[3];
 
        // Read country code
        loc_country_code_copy(buffer, dbobj->code);
 
+       // Terminate buffer
+       buffer[2] = '\0';
+
        // Create a new country object
        int r = loc_country_new(ctx, country, buffer);
        if (r)
@@ -151,16 +155,18 @@ FAIL:
        return r;
 }
 
-int loc_country_to_database_v0(struct loc_country* country,
-               struct loc_stringpool* pool, struct loc_database_country_v0* dbobj) {
+int loc_country_to_database_v1(struct loc_country* country,
+               struct loc_stringpool* pool, struct loc_database_country_v1* dbobj) {
        // Add country code
        for (unsigned int i = 0; i < 2; i++) {
                dbobj->code[i] = country->code[i] ? country->code[i] : '\0';
        }
 
        // Add continent code
-       for (unsigned int i = 0; i < 2; i++) {
-               dbobj->continent_code[i] = country->continent_code[i] ? country->continent_code[i] : '\0';
+       if (country->continent_code) {
+               for (unsigned int i = 0; i < 2; i++) {
+                       dbobj->continent_code[i] = country->continent_code[i] ? country->continent_code[i] : '\0';
+               }
        }
 
        // Save the name string in the string pool
@@ -169,3 +175,22 @@ int loc_country_to_database_v0(struct loc_country* country,
 
        return 0;
 }
+
+LOC_EXPORT int loc_country_code_is_valid(const char* cc) {
+       // It cannot be NULL
+       if (!cc || !*cc)
+               return 0;
+
+       // It must be 2 characters long
+       if (strlen(cc) != 2)
+               return 0;
+
+       // It must only contain A-Z
+       for (unsigned int i = 0; i < 2; i++) {
+               if (cc[i] < 'A' || cc[i] > 'Z')
+                       return 0;
+       }
+
+       // Looks valid
+       return 1;
+}