]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/country.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2019 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
21 #include <loc/libloc.h>
22 #include <loc/compat.h>
23 #include <loc/country.h>
24 #include <loc/private.h>
36 LOC_EXPORT
int loc_country_new(struct loc_ctx
* ctx
, struct loc_country
** country
, const char* country_code
) {
37 struct loc_country
* c
= calloc(1, sizeof(*c
));
41 c
->ctx
= loc_ref(ctx
);
44 c
->code
= strdup(country_code
);
46 DEBUG(c
->ctx
, "Country %s allocated at %p\n", c
->code
, c
);
52 LOC_EXPORT
struct loc_country
* loc_country_ref(struct loc_country
* country
) {
58 static void loc_country_free(struct loc_country
* country
) {
59 DEBUG(country
->ctx
, "Releasing country %s %p\n", country
->code
, country
);
64 if (country
->continent_code
)
65 free(country
->continent_code
);
70 loc_unref(country
->ctx
);
74 LOC_EXPORT
struct loc_country
* loc_country_unref(struct loc_country
* country
) {
75 if (--country
->refcount
> 0)
78 loc_country_free(country
);
83 LOC_EXPORT
const char* loc_country_get_code(struct loc_country
* country
) {
87 LOC_EXPORT
const char* loc_country_get_continent_code(struct loc_country
* country
) {
88 return country
->continent_code
;
91 LOC_EXPORT
int loc_country_set_continent_code(struct loc_country
* country
, const char* continent_code
) {
94 // Free previous value
95 if (country
->continent_code
)
96 free(country
->continent_code
);
98 country
->continent_code
= strdup(continent_code
);
103 LOC_EXPORT
const char* loc_country_get_name(struct loc_country
* country
) {
104 return country
->name
;
107 LOC_EXPORT
int loc_country_set_name(struct loc_country
* country
, const char* name
) {
112 country
->name
= strdup(name
);
117 LOC_EXPORT
int loc_country_cmp(struct loc_country
* country1
, struct loc_country
* country2
) {
118 return strcmp(country1
->code
, country2
->code
);
121 int loc_country_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_stringpool
* pool
,
122 struct loc_country
** country
, const struct loc_database_country_v0
* dbobj
) {
126 loc_country_code_copy(buffer
, dbobj
->code
);
128 // Create a new country object
129 int r
= loc_country_new(ctx
, country
, buffer
);
134 loc_country_code_copy(buffer
, dbobj
->continent_code
);
136 r
= loc_country_set_continent_code(*country
, buffer
);
141 const char* name
= loc_stringpool_get(pool
, be32toh(dbobj
->name
));
143 r
= loc_country_set_name(*country
, name
);
151 loc_country_unref(*country
);
155 int loc_country_to_database_v0(struct loc_country
* country
,
156 struct loc_stringpool
* pool
, struct loc_database_country_v0
* dbobj
) {
158 for (unsigned int i
= 0; i
< 2; i
++) {
159 dbobj
->code
[i
] = country
->code
[i
] ? country
->code
[i
] : '\0';
162 // Add continent code
163 if (country
->continent_code
) {
164 for (unsigned int i
= 0; i
< 2; i
++) {
165 dbobj
->continent_code
[i
] = country
->continent_code
[i
] ? country
->continent_code
[i
] : '\0';
169 // Save the name string in the string pool
170 off_t name
= loc_stringpool_add(pool
, country
->name
? country
->name
: "");
171 dbobj
->name
= htobe32(name
);
176 LOC_EXPORT
int loc_country_code_is_valid(const char* cc
) {
181 // It must be 2 characters long
185 // It must only contain A-Z
186 for (unsigned int i
= 0; i
< 2; i
++) {
187 if (cc
[i
] < 'A' || cc
[i
] > 'Z')