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
|
/*
* keywords.c: keep track of all cross-reference keywords
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "halibut.h"
static int kwcmp(const void *av, const void *bv, void *cmpctx)
{
const keyword *a = (const keyword *)av;
const keyword *b = (const keyword *)bv;
return ustrcmp(a->key, b->key);
}
static int kwfind(const void *av, const void *bv, void *cmpctx)
{
wchar_t *a = (wchar_t *)av;
const keyword *b = (const keyword *)bv;
return ustrcmp(a, b->key);
}
keyword *kw_lookup(keywordlist *kl, wchar_t *str) {
return findcmp234(kl->keys, str, kwfind, NULL);
}
/*
* This function reads through source form and collects the
* keywords. They get collected in a heap, sorted by Unicode
* collation, last at the top (so that we can Heapsort them when we
* finish).
*/
keywordlist *get_keywords(paragraph *source, errorstate *es) {
bool errors = false;
keywordlist *kl = snew(keywordlist);
numberstate *n = number_init();
int prevpara = para_NotParaType;
number_cfg(n, source);
kl->size = 0;
kl->keys = newtree234(kwcmp, NULL);
kl->nlooseends = kl->looseendssize = 0;
kl->looseends = NULL;
for (; source; source = source->next) {
wchar_t *p, *q;
p = q = source->keyword;
/*
* Look for the section type override (`example',
* `question' or whatever - to replace `chapter' or
* `section' on a per-section basis).
*/
if (q) {
q = uadv(q); /* point q at the word beyond */
if (!*q) q = NULL;
}
/*
* Number the chapter / section / list-item / whatever.
* This also sets up the `parent', `child' and `sibling'
* links.
*/
source->kwtext = number_mktext(n, source, q, &prevpara, &errors, es);
if (p && *p) {
if (source->kwtext || source->type == para_Biblio) {
keyword *kw, *ret;
kw = snew(keyword);
kw->key = p;
kw->text = source->kwtext;
kw->para = source;
ret = add234(kl->keys, kw);
if (ret != kw) {
err_multikw(es, &source->fpos, &ret->para->fpos, p);
sfree(kw);
/* FIXME: what happens to kw->text? Does it leak? */
}
}
} else {
if (kl->nlooseends >= kl->looseendssize) {
kl->looseendssize = kl->nlooseends + 32;
kl->looseends = sresize(kl->looseends, kl->looseendssize,
word *);
}
kl->looseends[kl->nlooseends++] = source->kwtext;
}
}
number_free(n);
if (errors) {
free_keywords(kl);
return NULL;
}
return kl;
}
void free_keywords(keywordlist *kl) {
keyword *kw;
while (kl->nlooseends)
free_word_list(kl->looseends[--kl->nlooseends]);
sfree(kl->looseends);
while ( (kw = index234(kl->keys, 0)) != NULL) {
delpos234(kl->keys, 0);
free_word_list(kw->text);
sfree(kw);
}
freetree234(kl->keys);
sfree(kl);
}
void subst_keywords(paragraph *source, keywordlist *kl, errorstate *es) {
for (; source; source = source->next) {
word *ptr;
for (ptr = source->words; ptr; ptr = ptr->next) {
if (ptr->type == word_UpperXref ||
ptr->type == word_LowerXref) {
keyword *kw;
word **endptr, *close, *subst;
kw = kw_lookup(kl, ptr->text);
if (!kw) {
err_nosuchkw(es, &ptr->fpos, ptr->text);
subst = NULL;
} else
subst = dup_word_list(kw->text);
if (subst && ptr->type == word_LowerXref &&
kw->para->type != para_Biblio &&
kw->para->type != para_BiblioCited)
ustrlow(subst->text);
close = snew(word);
close->text = NULL;
close->alt = NULL;
close->type = word_XrefEnd;
close->fpos = ptr->fpos;
close->breaks = false;
close->aux = 0;
close->next = ptr->next;
ptr->next = subst;
for (endptr = &ptr->next; *endptr; endptr = &(*endptr)->next)
(*endptr)->fpos = ptr->fpos;
*endptr = close;
ptr = close;
}
}
}
}
|