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"
|
2018-10-13 09:52:59 +00:00
|
|
|
#include "subsurface-string.h"
|
2015-07-01 19:25:47 +00:00
|
|
|
#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
|
|
|
};
|
|
|
|
|
2020-09-06 11:13:36 +00:00
|
|
|
static void alloc_taxonomy_table(struct taxonomy_data *t)
|
2015-07-01 19:25:47 +00:00
|
|
|
{
|
2020-09-06 11:13:36 +00:00
|
|
|
if (!t->category)
|
|
|
|
t->category = 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
|
|
|
|
2020-09-03 03:04:05 +00:00
|
|
|
void copy_taxonomy(const struct taxonomy_data *orig, struct taxonomy_data *copy)
|
2018-10-13 09:52:59 +00:00
|
|
|
{
|
|
|
|
if (orig->category == NULL) {
|
|
|
|
free_taxonomy(copy);
|
|
|
|
} else {
|
2020-09-06 11:13:36 +00:00
|
|
|
alloc_taxonomy_table(copy);
|
2018-10-13 09:52:59 +00:00
|
|
|
for (int i = 0; i < TC_NR_CATEGORIES; i++) {
|
|
|
|
if (i < copy->nr) {
|
|
|
|
free((void *)copy->category[i].value);
|
|
|
|
copy->category[i].value = NULL;
|
|
|
|
}
|
|
|
|
if (i < orig->nr) {
|
|
|
|
copy->category[i] = orig->category[i];
|
|
|
|
copy->category[i].value = copy_string(orig->category[i].value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
copy->nr = orig->nr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:52:17 +00:00
|
|
|
static int taxonomy_index_for_category(const struct taxonomy_data *t, enum taxonomy_category cat)
|
2015-07-13 22:18:52 +00:00
|
|
|
{
|
2020-09-05 10:50:03 +00:00
|
|
|
for (int i = 0; i < t->nr; i++) {
|
2015-07-13 22:18:52 +00:00
|
|
|
if (t->category[i].category == cat)
|
|
|
|
return i;
|
2020-09-05 10:50:03 +00:00
|
|
|
}
|
2015-07-13 22:18:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-10-03 05:54:24 +00:00
|
|
|
|
2020-09-06 11:42:25 +00:00
|
|
|
const char *taxonomy_get_value(const struct taxonomy_data *t, enum taxonomy_category cat)
|
2017-10-03 05:54:24 +00:00
|
|
|
{
|
2020-09-06 11:42:25 +00:00
|
|
|
int idx = taxonomy_index_for_category(t, cat);
|
|
|
|
return idx >= 0 ? t->category[idx].value : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *taxonomy_get_country(const struct taxonomy_data *t)
|
|
|
|
{
|
|
|
|
return taxonomy_get_value(t, TC_COUNTRY);
|
2017-10-03 05:54:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 11:23:23 +00:00
|
|
|
void taxonomy_set_category(struct taxonomy_data *t, enum taxonomy_category category, const char *value, enum taxonomy_origin origin)
|
2017-10-03 05:54:24 +00:00
|
|
|
{
|
2020-09-06 11:23:19 +00:00
|
|
|
int idx = taxonomy_index_for_category(t, category);
|
2017-10-05 22:40:19 +00:00
|
|
|
|
2020-09-06 11:23:19 +00:00
|
|
|
if (idx < 0) {
|
|
|
|
alloc_taxonomy_table(t); // make sure we have taxonomy data allocated
|
2017-10-03 05:54:24 +00:00
|
|
|
if (t->nr == TC_NR_CATEGORIES - 1) {
|
|
|
|
// can't add another one
|
2020-09-05 11:23:23 +00:00
|
|
|
fprintf(stderr, "Error adding taxonomy category\n");
|
2017-10-03 05:54:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-07 05:58:42 +00:00
|
|
|
idx = t->nr++;
|
2020-09-06 11:23:19 +00:00
|
|
|
} else {
|
|
|
|
free((void *)t->category[idx].value);
|
|
|
|
t->category[idx].value = NULL;
|
2017-10-03 05:54:24 +00:00
|
|
|
}
|
2020-09-05 11:30:04 +00:00
|
|
|
t->category[idx].value = strdup(value);
|
2017-10-03 05:54:24 +00:00
|
|
|
t->category[idx].origin = origin;
|
2020-09-05 11:23:23 +00:00
|
|
|
t->category[idx].category = category;
|
|
|
|
}
|
|
|
|
|
|
|
|
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin)
|
|
|
|
{
|
2017-10-07 05:58:42 +00:00
|
|
|
fprintf(stderr, "%s: set the taxonomy country to %s\n", __func__, country);
|
2020-09-05 11:23:23 +00:00
|
|
|
taxonomy_set_category(t, TC_COUNTRY, country, origin);
|
2017-10-03 05:54:24 +00:00
|
|
|
}
|