| 
									
										
										
										
											2017-04-27 20:24:53 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | #include "taxonomy.h"
 | 
					
						
							|  |  |  | #include "gettext.h"
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-02 10:21:35 -07:00
										 |  |  | char *taxonomy_category_names[TC_NR_CATEGORIES] = { | 
					
						
							| 
									
										
										
										
											2017-04-25 23:16:46 +02: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 12:25:47 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // these are the names for geoname.org
 | 
					
						
							| 
									
										
										
										
											2015-07-02 10:21:35 -07:00
										 |  |  | char *taxonomy_api_names[TC_NR_CATEGORIES] = { | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | 	"none", | 
					
						
							|  |  |  | 	"name", | 
					
						
							|  |  |  | 	"countryName", | 
					
						
							|  |  |  | 	"adminName1", | 
					
						
							|  |  |  | 	"adminName2", | 
					
						
							| 
									
										
										
										
											2015-07-10 09:51:50 -07:00
										 |  |  | 	"toponymName", | 
					
						
							|  |  |  | 	"adminName3" | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct taxonomy *alloc_taxonomy() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-02 10:21:35 -07:00
										 |  |  | 	return calloc(TC_NR_CATEGORIES, sizeof(struct taxonomy)); | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 07:09:55 -07:00
										 |  |  | void free_taxonomy(struct taxonomy_data *t) | 
					
						
							| 
									
										
										
										
											2015-07-01 12:25:47 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (t) { | 
					
						
							| 
									
										
										
										
											2015-07-13 07:09:55 -07: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 12:25:47 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-13 15:18:52 -07: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-02 22:54:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const char *taxonomy_get_country(struct taxonomy_data *t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-04 07:49:56 -07:00
										 |  |  | 	for (int i = 0; i < t->nr; i++) { | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | 		if (t->category[i].category == TC_COUNTRY) | 
					
						
							|  |  |  | 			return t->category[i].value; | 
					
						
							| 
									
										
										
										
											2017-10-04 07:49:56 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void taxonomy_set_country(struct taxonomy_data *t, const char *country, enum taxonomy_origin origin) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int idx = -1; | 
					
						
							| 
									
										
										
										
											2017-10-05 15:40:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// make sure we have taxonomy data allocated
 | 
					
						
							|  |  |  | 	if (!t->category) | 
					
						
							|  |  |  | 		t->category = alloc_taxonomy(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 07:49:56 -07:00
										 |  |  | 	for (int i = 0; i < t->nr; i++) { | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | 		if (t->category[i].category == TC_COUNTRY) { | 
					
						
							|  |  |  | 			idx = i; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-04 07:49:56 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | 	if (idx == -1) { | 
					
						
							|  |  |  | 		if (t->nr == TC_NR_CATEGORIES - 1) { | 
					
						
							|  |  |  | 			// can't add another one
 | 
					
						
							|  |  |  | 			fprintf(stderr, "Error adding country taxonomy\n"); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-06 22:58:42 -07:00
										 |  |  | 		idx = t->nr++; | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	t->category[idx].value = country; | 
					
						
							|  |  |  | 	t->category[idx].origin = origin; | 
					
						
							| 
									
										
										
										
											2017-10-06 22:58:42 -07:00
										 |  |  | 	t->category[idx].category = TC_COUNTRY; | 
					
						
							|  |  |  | 	fprintf(stderr, "%s: set the taxonomy country to %s\n", __func__, country); | 
					
						
							| 
									
										
										
										
											2017-10-02 22:54:24 -07:00
										 |  |  | } |