2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-07-01 19:25:47 +00:00
|
|
|
#include "taxonomy.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
#include <stdlib.h>
|
2017-10-03 05:54:24 +00:00
|
|
|
#include <stdio.h>
|
2015-07-01 19:25:47 +00:00
|
|
|
|
2015-07-02 17:21:35 +00:00
|
|
|
char *taxonomy_category_names[TC_NR_CATEGORIES] = {
|
2017-04-25 21:16:46 +00:00
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "None"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Ocean"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Country"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "State"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "County"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "Town"),
|
|
|
|
QT_TRANSLATE_NOOP("gettextFromC", "City")
|
2015-07-01 19:25:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// these are the names for geoname.org
|
2015-07-02 17:21:35 +00:00
|
|
|
char *taxonomy_api_names[TC_NR_CATEGORIES] = {
|
2015-07-01 19:25:47 +00:00
|
|
|
"none",
|
|
|
|
"name",
|
|
|
|
"countryName",
|
|
|
|
"adminName1",
|
|
|
|
"adminName2",
|
2015-07-10 16:51:50 +00:00
|
|
|
"toponymName",
|
|
|
|
"adminName3"
|
2015-07-01 19:25:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct taxonomy *alloc_taxonomy()
|
|
|
|
{
|
2015-07-02 17:21:35 +00:00
|
|
|
return calloc(TC_NR_CATEGORIES, sizeof(struct taxonomy));
|
2015-07-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 14:09:55 +00:00
|
|
|
void free_taxonomy(struct taxonomy_data *t)
|
2015-07-01 19:25:47 +00:00
|
|
|
{
|
|
|
|
if (t) {
|
2015-07-13 14:09:55 +00:00
|
|
|
for (int i = 0; i < t->nr; i++)
|
|
|
|
free((void *)t->category[i].value);
|
|
|
|
free(t->category);
|
|
|
|
t->category = NULL;
|
|
|
|
t->nr = 0;
|
2015-07-01 19:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-13 22:18:52 +00:00
|
|
|
|
|
|
|
int taxonomy_index_for_category(struct taxonomy_data *t, enum taxonomy_category cat)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < t->nr; i++)
|
|
|
|
if (t->category[i].category == cat)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-10-03 05:54:24 +00:00
|
|
|
|
|
|
|
const char *taxonomy_get_country(struct taxonomy_data *t)
|
|
|
|
{
|
2017-10-04 14:49:56 +00:00
|
|
|
for (int i = 0; i < t->nr; i++) {
|
2017-10-03 05:54:24 +00:00
|
|
|
if (t->category[i].category == TC_COUNTRY)
|
|
|
|
return t->category[i].value;
|
2017-10-04 14:49:56 +00:00
|
|
|
}
|
2017-10-03 05:54:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin)
|
|
|
|
{
|
|
|
|
int idx = -1;
|
2017-10-04 14:49:56 +00:00
|
|
|
for (int i = 0; i < t->nr; i++) {
|
2017-10-03 05:54:24 +00:00
|
|
|
if (t->category[i].category == TC_COUNTRY) {
|
|
|
|
idx = i;
|
|
|
|
break;
|
|
|
|
}
|
2017-10-04 14:49:56 +00:00
|
|
|
}
|
2017-10-03 05:54:24 +00:00
|
|
|
if (idx == -1) {
|
|
|
|
if (t->nr == TC_NR_CATEGORIES - 1) {
|
|
|
|
// can't add another one
|
|
|
|
fprintf(stderr, "Error adding country taxonomy\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
idx = ++t->nr;
|
|
|
|
}
|
|
|
|
t->category[idx].value = country;
|
|
|
|
t->category[idx].origin = origin;
|
|
|
|
}
|