]>
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/country.h>
23 #include <loc/private.h>
35 LOC_EXPORT
int loc_country_new(struct loc_ctx
* ctx
, struct loc_country
** country
, const char* country_code
) {
36 struct loc_country
* c
= calloc(1, sizeof(*c
));
40 c
->ctx
= loc_ref(ctx
);
43 c
->code
= strdup(country_code
);
45 DEBUG(c
->ctx
, "Country %s allocated at %p\n", c
->code
, c
);
51 LOC_EXPORT
struct loc_country
* loc_country_ref(struct loc_country
* country
) {
57 static void loc_country_free(struct loc_country
* country
) {
58 DEBUG(country
->ctx
, "Releasing country %s %p\n", country
->code
, country
);
63 if (country
->continent_code
)
64 free(country
->continent_code
);
69 loc_unref(country
->ctx
);
73 LOC_EXPORT
struct loc_country
* loc_country_unref(struct loc_country
* country
) {
74 if (--country
->refcount
> 0)
77 loc_country_free(country
);
82 LOC_EXPORT
const char* loc_country_get_code(struct loc_country
* country
) {
86 LOC_EXPORT
const char* loc_country_get_continent_code(struct loc_country
* country
) {
87 return country
->continent_code
;
90 LOC_EXPORT
int loc_country_set_continent_code(struct loc_country
* country
, const char* continent_code
) {
93 // Free previous value
94 if (country
->continent_code
)
95 free(country
->continent_code
);
97 country
->continent_code
= strdup(continent_code
);
102 LOC_EXPORT
const char* loc_country_get_name(struct loc_country
* country
) {
103 return country
->name
;
106 LOC_EXPORT
int loc_country_set_name(struct loc_country
* country
, const char* name
) {
111 country
->name
= strdup(name
);
116 int loc_country_cmp(struct loc_country
* country1
, struct loc_country
* country2
) {
117 return strcmp(country1
->code
, country2
->code
);
120 int loc_country_new_from_database_v0(struct loc_ctx
* ctx
, struct loc_stringpool
* pool
,
121 struct loc_country
** country
, const struct loc_database_country_v0
* dbobj
) {
125 loc_country_copy_code(buffer
, dbobj
->code
);
127 // Create a new country object
128 int r
= loc_country_new(ctx
, country
, buffer
);
133 loc_country_copy_code(buffer
, dbobj
->continent_code
);
135 r
= loc_country_set_continent_code(*country
, buffer
);
141 // XXX Reading from the stringpool makes test-country.c fail
142 const char* name
= loc_stringpool_get(pool
, be32toh(dbobj
->name
));
144 r
= loc_country_set_name(*country
, name
);
153 loc_country_unref(*country
);
157 int loc_country_to_database_v0(struct loc_country
* country
,
158 struct loc_stringpool
* pool
, struct loc_database_country_v0
* dbobj
) {
160 for (unsigned int i
= 0; i
< 2; i
++) {
161 dbobj
->code
[i
] = country
->code
[i
] ? country
->code
[i
] : '\0';
164 // Add continent code
165 for (unsigned int i
= 0; i
< 2; i
++) {
166 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
);