mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
cleanup: add helper function to set taxonomy category
Setting a taxonomy category was cumbersome: the caller had to make sure that the category-table was allocated. Introduce a helper function to make that simpler. Make taxonomy_set_country() the first caller of the new function, since it is just a special case with category = TC_COUNTRY. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
86c24dd832
commit
6da78a29c4
2 changed files with 12 additions and 6 deletions
|
@ -81,7 +81,7 @@ const char *taxonomy_get_country(struct taxonomy_data *t)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin)
|
void taxonomy_set_category(struct taxonomy_data *t, enum taxonomy_category category, const char *value, enum taxonomy_origin origin)
|
||||||
{
|
{
|
||||||
int idx = -1;
|
int idx = -1;
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum tax
|
||||||
t->category = alloc_taxonomy();
|
t->category = alloc_taxonomy();
|
||||||
|
|
||||||
for (int i = 0; i < t->nr; i++) {
|
for (int i = 0; i < t->nr; i++) {
|
||||||
if (t->category[i].category == TC_COUNTRY) {
|
if (t->category[i].category == category) {
|
||||||
free((void *)t->category[i].value);
|
free((void *)t->category[i].value);
|
||||||
t->category[i].value = NULL;
|
t->category[i].value = NULL;
|
||||||
idx = i;
|
idx = i;
|
||||||
|
@ -100,13 +100,18 @@ void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum tax
|
||||||
if (idx == -1) {
|
if (idx == -1) {
|
||||||
if (t->nr == TC_NR_CATEGORIES - 1) {
|
if (t->nr == TC_NR_CATEGORIES - 1) {
|
||||||
// can't add another one
|
// can't add another one
|
||||||
fprintf(stderr, "Error adding country taxonomy\n");
|
fprintf(stderr, "Error adding taxonomy category\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
idx = t->nr++;
|
idx = t->nr++;
|
||||||
}
|
}
|
||||||
t->category[idx].value = country;
|
t->category[idx].value = value;
|
||||||
t->category[idx].origin = origin;
|
t->category[idx].origin = origin;
|
||||||
t->category[idx].category = TC_COUNTRY;
|
t->category[idx].category = category;
|
||||||
fprintf(stderr, "%s: set the taxonomy country to %s\n", __func__, country);
|
}
|
||||||
|
|
||||||
|
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: set the taxonomy country to %s\n", __func__, country);
|
||||||
|
taxonomy_set_category(t, TC_COUNTRY, country, origin);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,7 @@ void free_taxonomy(struct taxonomy_data *t);
|
||||||
void copy_taxonomy(const struct taxonomy_data *orig, struct taxonomy_data *copy);
|
void copy_taxonomy(const struct taxonomy_data *orig, struct taxonomy_data *copy);
|
||||||
int taxonomy_index_for_category(const struct taxonomy_data *t, enum taxonomy_category cat);
|
int taxonomy_index_for_category(const struct taxonomy_data *t, enum taxonomy_category cat);
|
||||||
const char *taxonomy_get_country(struct taxonomy_data *t);
|
const char *taxonomy_get_country(struct taxonomy_data *t);
|
||||||
|
void taxonomy_set_category(struct taxonomy_data *t, enum taxonomy_category category, const char *value, enum taxonomy_origin origin);
|
||||||
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin);
|
void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
Loading…
Add table
Reference in a new issue