aboutsummaryrefslogtreecommitdiff
path: root/src/emit.c
blob: ff99700cfca2d95129ae9269effb3618d3b6c410 (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
#include "trans.h"
#include <unistd.h>

static unsigned long dict_index;
static unsigned long article_index;

static RAD_LIST *antonym_list;

int
_emit_headers(void *item, void *data)
{
	struct header *hp = item;
	if (hp->forms)
		sql_query("INSERT INTO dict VALUES(%lu,'%s','%s','%s')",
			  dict_index, hp->key, hp->pos, hp->forms);
	else
		sql_query("INSERT INTO dict (ident,word,pos) VALUES(%lu,'%s','%s')",
			  dict_index, hp->key, hp->pos);
	return 0;
}

static int
_antonym_cmp(const void *item, const void *data)
{
	const struct antonym *a = item, *b = data;
	return !(a->index == b->index && strcmp(a->value, b->value) == 0);
}

int
insert_antonym(unsigned long dict_index, u_char *value)
{
	unsigned long index;
	if (sql_query_n(&index,
			"SELECT ident FROM dict WHERE word='%s'",
			value) == 0) {
		sql_query("REPLACE INTO antonym VALUES (%lu,%lu)",
			  index, dict_index);
		sql_query("REPLACE INTO antonym VALUES (%lu,%lu)",
			  dict_index, index);
		return 0;
	}
	return 1;
}

int
_emit_descr(void *item, void *data)
{
	struct descr *descr = item;
	unsigned long index;
	
	switch (descr->type) {
	case descr_topic:
		if (sql_query_n(&index,
				"SELECT ident FROM topic WHERE title='%s'",
				descr->value)) {
			sql_query("INSERT INTO topic (title) VALUES ('%s')",
				  descr->value);
			sql_query_n(&index, "SELECT LAST_INSERT_ID()");
		}
		sql_query("INSERT INTO topic_tab VALUES(%lu,%lu)",
			  index, dict_index);
		break;
		
	case descr_meaning:
		sql_query("INSERT INTO articles VALUES (%lu, %lu, '%s')",
			  dict_index,
			  article_index++,
			  descr->value);
		break;
		
	case descr_antonym:
		if (insert_antonym(dict_index, descr->value)) {
			struct antonym *p = emalloc(sizeof(*p));
			p->index = dict_index;
			p->value = descr->value;
			list_append(antonym_list, p);
		}
	}
	return 0;
}

static int
cmp_descr_type(const void *a, const void *b)
{
	const struct descr *da = a, *db = b;
	return da->type != db->type;
}

void
emit_node(RAD_LIST *hdr, RAD_LIST *descr)
{
	article_index = 0;

	list_iterate(hdr, _emit_headers, NULL);
	list_iterate(descr, _emit_descr, NULL);

	dict_index++;
}

int
_antonym_fixup(void *item, void *data)
{
	struct antonym *ant = item;
	if (insert_antonym(ant->index, ant->value)) {
		fprintf(stderr, "unhandled antonym: %lu - \"%s\"\n",
			ant->index, ant->value);
	}
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.