summaryrefslogtreecommitdiff
path: root/liblangtag/lt-relation-db.c
blob: d77a5ecb1cf4cf716b4e5b200ea1e20e1599868d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * lt-relation-db.c
 * Copyright (C) 2015-2016 Akira TAGOH
 * 
 * Authors:
 *   Akira TAGOH  <akira@tagoh.org>
 * 
 * You may distribute under the terms of either the GNU
 * Lesser General Public License or the Mozilla Public
 * License, as specified in the README file.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <libxml/xpath.h>
#include "lt-database.h"
#include "lt-error.h"
#include "lt-list.h"
#include "lt-lock.h"
#include "lt-mem.h"
#include "lt-messages.h"
#include "lt-trie.h"
#include "lt-utils.h"
#include "lt-xml.h"
#include "lt-relation-db.h"

/**
 * SECTION:lt-relation-db
 * @Short_Description: An interface to access relation database
 * @Title: Database - Relation
 *
 * This class providdes an interface to access Relation database.
 */
struct _lt_relation_db_t {
	lt_mem_t   parent;
	lt_trie_t *relation_l_s_entries;
	lt_trie_t *relation_s_l_entries;
};

LT_LOCK_DEFINE_STATIC (rdb);

/*< private >*/
static lt_bool_t
lt_relation_db_parse(lt_relation_db_t  *relationdb,
		     lt_error_t       **error)
{
	lt_bool_t retval = TRUE;
	xmlDocPtr doc = NULL;
	xmlXPathContextPtr xctxt = NULL;
	xmlXPathObjectPtr xobj = NULL;
	lt_error_t *err = NULL;
	int i, n;
	lt_lang_db_t *langdb = NULL;
	lt_script_db_t *scriptdb = NULL;
	lt_xml_t *xml;

	lt_return_val_if_fail (relationdb != NULL, FALSE);

	relationdb->relation_l_s_entries = lt_trie_new();
	lt_mem_add_ref((lt_mem_t *)relationdb, relationdb->relation_l_s_entries,
		       (lt_destroy_func_t)lt_trie_unref);
	relationdb->relation_s_l_entries = lt_trie_new();
	lt_mem_add_ref((lt_mem_t *)relationdb, relationdb->relation_s_l_entries,
		       (lt_destroy_func_t)lt_trie_unref);

	xml = lt_xml_new();
	doc = lt_xml_get_cldr(xml, LT_XML_CLDR_SUPPLEMENTAL_SUPPLEMENTAL_DATA);
	xctxt = xmlXPathNewContext(doc);
	if (!xctxt) {
		lt_error_set(&err, LT_ERR_OOM,
			     "Unable to create an instance of xmlXPathContextPtr.");
		goto bail;
	}
	xobj = xmlXPathEvalExpression((const xmlChar *)"/supplementalData/languageData/language[@scripts]", xctxt);
	if (!xobj) {
		lt_error_set(&err, LT_ERR_FAIL_ON_XML,
			     "No valid elements for %s",
			     doc->name);
		goto bail;
	}
	n = xmlXPathNodeSetGetLength(xobj->nodesetval);

	langdb = lt_db_get_lang();
	scriptdb = lt_db_get_script();
	for (i = 0; i < n; i++) {
		xmlNodePtr ent = xmlXPathNodeSetItem(xobj->nodesetval, i);
		xmlChar *type, *scripts;
		char *p, *pp;
		lt_list_t *l;
		lt_lang_t *ol;
		lt_script_t *os;
		lt_bool_t alloced;

		type = xmlGetProp(ent, (const xmlChar *)"type");
		scripts = xmlGetProp(ent, (const xmlChar *)"scripts");
		p = (char *)scripts;
		do {
			pp = strchr(p, ' ');
			if (pp) {
				*pp = 0;
				pp++;
			}
			ol = lt_lang_db_lookup(langdb, (const char *)type);
			os = lt_script_db_lookup(scriptdb, p);
			if (!ol || !os) {
				if (!ol)
					lt_warning("Unknown lang tag: %s", type);
				else
					lt_lang_unref(ol);
				if (!os)
					lt_warning("Unknown script tag: %s", p);
				else
					lt_script_unref(os);
				goto bail1;
			}
			alloced = FALSE;
			l = lt_trie_lookup(relationdb->relation_l_s_entries,
					   (const char *)type);
			if (!l)
				alloced = TRUE;
			l = lt_list_append(l, os, (lt_destroy_func_t)lt_script_unref);
			if (alloced) {
				lt_mem_add_ref((lt_mem_t *)relationdb->relation_l_s_entries,
					       l, (lt_destroy_func_t)lt_list_free);
			}
			lt_trie_replace(relationdb->relation_l_s_entries,
					(const char *)type, l, NULL);
			alloced = FALSE;
			l = lt_trie_lookup(relationdb->relation_s_l_entries, p);
			if (!l)
				alloced = TRUE;
			l = lt_list_append(l, ol, (lt_destroy_func_t)lt_lang_unref);
			if (alloced) {
				lt_mem_add_ref((lt_mem_t *)relationdb->relation_s_l_entries,
					       l, (lt_destroy_func_t)lt_list_free);
			}
			lt_trie_replace(relationdb->relation_s_l_entries, p, l, NULL);
		  bail1:
			p = pp;
		} while (p);
			
		xmlFree(type);
		xmlFree(scripts);
	}

  bail:
	if (lt_error_is_set(err, LT_ERR_ANY)) {
		if (error)
			*error = lt_error_ref(err);
		else
			lt_error_print(err, LT_ERR_ANY);
		lt_error_unref(err);
		retval = FALSE;
	}
	if (langdb)
		lt_lang_db_unref(langdb);
	if (scriptdb)
		lt_script_db_unref(scriptdb);
	if (xobj)
		xmlXPathFreeObject(xobj);
	if (xctxt)
		xmlXPathFreeContext(xctxt);
	if (xml)
		lt_xml_unref(xml);

	return retval;
}

/*< public >*/
/**
 * lt_relation_db_new:
 *
 * Create a new instance of a #lt_relation_db_t.
 *
 * Returns: (transfer full): a new instance of #lt_relation_db_t.
 */
lt_relation_db_t *
lt_relation_db_new(void)
{
	lt_relation_db_t *retval = lt_mem_alloc_object(sizeof (lt_relation_db_t));

	return retval;
}

/**
 * lt_relation_db_ref:
 * @relationdb: a #lt_relation_db_t.
 *
 * Increases the reference count of @relationdb.
 *
 * Returns: (transfer none): the same @relationddb object.
 */
lt_relation_db_t *
lt_relation_db_ref(lt_relation_db_t *relationdb)
{
	lt_return_val_if_fail (relationdb != NULL, NULL);

	return lt_mem_ref((lt_mem_t *)relationdb);
}

/**
 * lt_relation_db_unref:
 * @relationdb: a #lt_relation_db_t.
 *
 * Decreases the reference count of @relationdb. when its reference count
 * drops to 0, the object is finalized (i.e. its memory is freed).
 */
void
lt_relation_db_unref(lt_relation_db_t *relationdb)
{
	if (relationdb)
		lt_mem_unref((lt_mem_t *)relationdb);
}

/**
 * lt_relation_db_lookup_lang_from_script:
 * @relationdb: a #lt_relation_db_t.
 * @script: a #lt_script_t.
 *
 * Look up the languages corresponding to the script @script.
 *
 * Returns: (transfer full): a #lt_list_t containing #lt_lang_t object.
 */
lt_list_t *
lt_relation_db_lookup_lang_from_script(lt_relation_db_t  *relationdb,
				       const lt_script_t *script)
{
	lt_list_t *l, *ll, *retval = NULL;
	char *key;

	lt_return_val_if_fail (relationdb != NULL, NULL);
	lt_return_val_if_fail (script != NULL, NULL);

	LT_LOCK (rdb);
	if (!relationdb->relation_s_l_entries) {
		if (!lt_relation_db_parse(relationdb, NULL)) {
			LT_UNLOCK (rdb);
			return NULL;
		}
	}
	LT_UNLOCK (rdb);

	key = strdup(lt_script_get_name(script));
	l = lt_trie_lookup(relationdb->relation_s_l_entries,
			   lt_strlower(key));
	free(key);

	for (ll = l; ll; ll = lt_list_next(ll)) {
		retval = lt_list_append(retval,
					lt_lang_ref(lt_list_value(ll)),
					(lt_destroy_func_t)lt_lang_unref);
	}

	return retval;
}

/**
 * lt_relation_db_lookup_script_from_lang:
 * @relationdb: a #lt_relation_db_t.
 * @lang: a #lt_lang_t.
 *
 * Look up the scripts corresponding to the language @lang.
 *
 * Returns: (transfer full): a #lt_list_t containing #lt_script_t objects.
 */
lt_list_t *
lt_relation_db_lookup_script_from_lang(lt_relation_db_t *relationdb,
				       const lt_lang_t  *lang)
{
	lt_list_t *l, *ll, *retval = NULL;
	char *key;

	lt_return_val_if_fail (relationdb != NULL, NULL);
	lt_return_val_if_fail (lang != NULL, NULL);

	LT_LOCK (rdb);
	if (!relationdb->relation_l_s_entries) {
		if (!lt_relation_db_parse(relationdb, NULL)) {
			LT_UNLOCK (rdb);
			return NULL;
		}
	}
	LT_UNLOCK (rdb);

	key = strdup(lt_lang_get_tag(lang));
	l = lt_trie_lookup(relationdb->relation_l_s_entries,
			   lt_strlower(key));
	free(key);

	for (ll = l; ll; ll = lt_list_next(ll)) {
		retval = lt_list_append(retval,
					lt_script_ref(lt_list_value(ll)),
					(lt_destroy_func_t)lt_lang_unref);
	}

	return retval;
}