| 1 |
/* |
| 2 |
* contents.c: build a table of contents |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <assert.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <string.h> |
| 10 |
#include "halibut.h" |
| 11 |
|
| 12 |
struct numberstate_Tag { |
| 13 |
int chapternum; |
| 14 |
int appendixnum; |
| 15 |
int ischapter; |
| 16 |
int *sectionlevels; |
| 17 |
paragraph **currentsects; |
| 18 |
paragraph *lastsect; |
| 19 |
int oklevel; |
| 20 |
int maxsectlevel; |
| 21 |
int listitem; |
| 22 |
wchar_t *chaptertext; /* the word for a chapter */ |
| 23 |
wchar_t *sectiontext; /* the word for a section */ |
| 24 |
wchar_t *apptext; /* the word for an appendix */ |
| 25 |
}; |
| 26 |
|
| 27 |
numberstate *number_init(void) |
| 28 |
{ |
| 29 |
numberstate *ret = mknew(numberstate); |
| 30 |
ret->chapternum = 0; |
| 31 |
ret->appendixnum = -1; |
| 32 |
ret->ischapter = 1; |
| 33 |
ret->oklevel = -1; /* not even in a chapter yet */ |
| 34 |
ret->maxsectlevel = 32; |
| 35 |
ret->sectionlevels = mknewa(int, ret->maxsectlevel); |
| 36 |
ret->currentsects = mknewa(paragraph *, ret->maxsectlevel + 1); |
| 37 |
memset(ret->currentsects, 0, |
| 38 |
(ret->maxsectlevel + 1) * sizeof(paragraph *)); |
| 39 |
ret->lastsect = NULL; |
| 40 |
ret->listitem = -1; |
| 41 |
return ret; |
| 42 |
} |
| 43 |
|
| 44 |
void number_free(numberstate * state) |
| 45 |
{ |
| 46 |
sfree(state->sectionlevels); |
| 47 |
sfree(state->currentsects); |
| 48 |
sfree(state); |
| 49 |
} |
| 50 |
|
| 51 |
static void dotext(word *** wret, wchar_t * text) |
| 52 |
{ |
| 53 |
word *mnewword = mknew(word); |
| 54 |
mnewword->text = ustrdup(text); |
| 55 |
mnewword->type = word_Normal; |
| 56 |
mnewword->alt = NULL; |
| 57 |
mnewword->next = NULL; |
| 58 |
mnewword->breaks = FALSE; |
| 59 |
mnewword->aux = 0; |
| 60 |
**wret = mnewword; |
| 61 |
*wret = &mnewword->next; |
| 62 |
} |
| 63 |
|
| 64 |
static void dospace(word *** wret) |
| 65 |
{ |
| 66 |
word *mnewword = mknew(word); |
| 67 |
mnewword->text = NULL; |
| 68 |
mnewword->type = word_WhiteSpace; |
| 69 |
mnewword->alt = NULL; |
| 70 |
mnewword->next = NULL; |
| 71 |
mnewword->breaks = FALSE; |
| 72 |
mnewword->aux = 0; |
| 73 |
**wret = mnewword; |
| 74 |
*wret = &mnewword->next; |
| 75 |
} |
| 76 |
|
| 77 |
static void donumber(word *** wret, int num) |
| 78 |
{ |
| 79 |
wchar_t text[20]; |
| 80 |
int i = 19; |
| 81 |
text[i] = L'\0'; |
| 82 |
while (num != 0) |
| 83 |
{ |
| 84 |
assert(i >= 0); |
| 85 |
i--; text[i] = L"0123456789"[num % 10]; |
| 86 |
num /= 10; |
| 87 |
} |
| 88 |
dotext(wret, &text[i]); |
| 89 |
} |
| 90 |
|
| 91 |
static void doanumber(word *** wret, int num) |
| 92 |
{ |
| 93 |
wchar_t text[20]; |
| 94 |
int i = 19; |
| 95 |
int nletters, aton; |
| 96 |
nletters = 1; |
| 97 |
aton = 25; |
| 98 |
while (num > aton) |
| 99 |
{ |
| 100 |
nletters++; |
| 101 |
num -= aton + 1; |
| 102 |
if (aton < INT_MAX / 26) |
| 103 |
aton = (aton + 1) * 26 - 1; |
| 104 |
else |
| 105 |
aton = INT_MAX; |
| 106 |
} |
| 107 |
text[i] = L'\0'; |
| 108 |
while (nletters--) |
| 109 |
{ |
| 110 |
assert(i >= 0); |
| 111 |
i--; text[i] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"[num % 26]; |
| 112 |
num /= 26; |
| 113 |
} |
| 114 |
dotext(wret, &text[i]); |
| 115 |
} |
| 116 |
|
| 117 |
void number_cfg(numberstate * state, paragraph * source) |
| 118 |
{ |
| 119 |
/* |
| 120 |
* Defaults |
| 121 |
*/ |
| 122 |
state->chaptertext = L"Chapter"; |
| 123 |
state->sectiontext = L"Section"; |
| 124 |
state->apptext = L"Appendix"; |
| 125 |
|
| 126 |
for (; source; source = source->next) |
| 127 |
{ |
| 128 |
if (source->type == para_Config) |
| 129 |
{ |
| 130 |
if (!ustricmp(source->keyword, L"chapter")) |
| 131 |
{ |
| 132 |
state->chaptertext = uadv(source->keyword); |
| 133 |
} else if (!ustricmp(source->keyword, L"section")) |
| 134 |
{ |
| 135 |
state->sectiontext = uadv(source->keyword); |
| 136 |
} else if (!ustricmp(source->keyword, L"appendix")) |
| 137 |
{ |
| 138 |
state->apptext = uadv(source->keyword); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
word *number_mktext(numberstate * state, paragraph * p, wchar_t * category, |
| 145 |
int prev, int *errflag) |
| 146 |
{ |
| 147 |
word *ret = NULL; |
| 148 |
word **ret2 = &ret; |
| 149 |
word **pret = &ret; |
| 150 |
int i, level; |
| 151 |
|
| 152 |
level = -2; /* default for non-section-heading */ |
| 153 |
switch (p->type) |
| 154 |
{ |
| 155 |
case para_Chapter: |
| 156 |
state->chapternum++; |
| 157 |
for (i = 0; i < state->maxsectlevel; i++) |
| 158 |
state->sectionlevels[i] = 0; |
| 159 |
dotext(&pret, category ? category : state->chaptertext); |
| 160 |
dospace(&pret); |
| 161 |
ret2 = pret; |
| 162 |
donumber(&pret, state->chapternum); |
| 163 |
state->ischapter = 1; |
| 164 |
state->oklevel = 0; |
| 165 |
level = -1; |
| 166 |
break; |
| 167 |
case para_Heading: |
| 168 |
case para_Subsect: |
| 169 |
level = (p->type == para_Heading ? 0 : p->aux); |
| 170 |
if (level > state->oklevel) |
| 171 |
{ |
| 172 |
error(err_sectjump, &p->fpos); |
| 173 |
*errflag = TRUE; |
| 174 |
ret = NULL; |
| 175 |
break; |
| 176 |
} |
| 177 |
state->oklevel = level + 1; |
| 178 |
if (state->maxsectlevel <= level) |
| 179 |
{ |
| 180 |
state->maxsectlevel = level + 32; |
| 181 |
state->sectionlevels = resize(state->sectionlevels, |
| 182 |
state->maxsectlevel); |
| 183 |
} |
| 184 |
state->sectionlevels[level]++; |
| 185 |
for (i = level + 1; i < state->maxsectlevel; i++) |
| 186 |
state->sectionlevels[i] = 0; |
| 187 |
dotext(&pret, category ? category : state->sectiontext); |
| 188 |
dospace(&pret); |
| 189 |
ret2 = pret; |
| 190 |
if (state->ischapter) |
| 191 |
donumber(&pret, state->chapternum); |
| 192 |
else |
| 193 |
doanumber(&pret, state->appendixnum); |
| 194 |
for (i = 0; i <= level; i++) |
| 195 |
{ |
| 196 |
dotext(&pret, L"."); |
| 197 |
if (state->sectionlevels[i] == 0) |
| 198 |
state->sectionlevels[i] = 1; |
| 199 |
donumber(&pret, state->sectionlevels[i]); |
| 200 |
} |
| 201 |
break; |
| 202 |
case para_Appendix: |
| 203 |
state->appendixnum++; |
| 204 |
for (i = 0; i < state->maxsectlevel; i++) |
| 205 |
state->sectionlevels[i] = 0; |
| 206 |
dotext(&pret, category ? category : state->apptext); |
| 207 |
dospace(&pret); |
| 208 |
ret2 = pret; |
| 209 |
doanumber(&pret, state->appendixnum); |
| 210 |
state->ischapter = 0; |
| 211 |
state->oklevel = 0; |
| 212 |
level = -1; |
| 213 |
break; |
| 214 |
case para_UnnumberedChapter: |
| 215 |
level = -1; |
| 216 |
break; |
| 217 |
case para_NumberedList: |
| 218 |
ret2 = pret; |
| 219 |
if (prev != para_NumberedList) |
| 220 |
state->listitem = 0; |
| 221 |
state->listitem++; |
| 222 |
donumber(&pret, state->listitem); |
| 223 |
break; |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* Now set up parent, child and sibling links. |
| 228 |
*/ |
| 229 |
p->parent = p->child = p->sibling = NULL; |
| 230 |
if (level != -2) |
| 231 |
{ |
| 232 |
if (state->currentsects[level + 1]) |
| 233 |
state->currentsects[level + 1]->sibling = p; |
| 234 |
if (level >= 0 && state->currentsects[level]) |
| 235 |
{ |
| 236 |
p->parent = state->currentsects[level]; |
| 237 |
if (!state->currentsects[level]->child) |
| 238 |
state->currentsects[level]->child = p; |
| 239 |
} |
| 240 |
state->currentsects[level + 1] = state->lastsect = p; |
| 241 |
for (i = level + 2; i < state->maxsectlevel + 1; i++) |
| 242 |
state->currentsects[i] = NULL; |
| 243 |
} else |
| 244 |
{ |
| 245 |
p->parent = state->lastsect; |
| 246 |
} |
| 247 |
|
| 248 |
p->kwtext2 = *ret2; |
| 249 |
return ret; |
| 250 |
} |