diff options
Diffstat (limited to 'src/emit.c')
-rw-r--r-- | src/emit.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/emit.c b/src/emit.c new file mode 100644 index 0000000..ff99700 --- /dev/null +++ b/src/emit.c @@ -0,0 +1,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; +} |