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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* vim:set ts=8 sts=4 sw=4 tw=0: */
/*
* mnode.c - mnode interfaces.
*
* Written By: MURAOKA Taro <koron@tka.att.ne.jp>
* Last Change: 04-May-2004.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dbg.h"
#include "wordlist.h"
#include "wordbuf.h"
#include "mnode.h"
#define MTREE_MNODE_N 1024
struct _mtree_t
{
mtree_p active;
int used;
mnode nodes[MTREE_MNODE_N];
mtree_p next;
};
#define MNODE_BUFSIZE 16384
#if defined(_MSC_VER) || defined(__GNUC__)
# define INLINE __inline
#else
# define INLINE
#endif
int n_mnode_new = 0;
int n_mnode_delete = 0;
INLINE static mnode*
mnode_new(mtree_p mtree)
{
mtree_p active = mtree->active;
if (active->used >= MTREE_MNODE_N)
{
active->next = (mtree_p)calloc(1, sizeof(*active->next));
/* TODO: G[ */
mtree->active = active->next;
active = active->next;
}
++n_mnode_new;
return &active->nodes[active->used++];
}
static void
mnode_delete(mnode* p)
{
while (p)
{
mnode* child = p->child;
if (p->list)
wordlist_close(p->list);
if (p->next)
mnode_delete(p->next);
/*free(p);*/
p = child;
++n_mnode_delete;
}
}
void
mnode_print_stub(mnode* vp, unsigned char* p)
{
static unsigned char buf [256];
if (!vp)
return;
if (!p)
p = &buf[0];
p[0] = MNODE_GET_CH(vp);
p[1] = '\0';
if (vp->list)
printf("%s (list=%p)\n", buf, vp->list);
if (vp->child)
mnode_print_stub(vp->child, p + 1);
if (vp->next)
mnode_print_stub(vp->next, p);
}
void
mnode_print(mtree_p mtree, unsigned char* p)
{
if (mtree && mtree->used > 0)
mnode_print_stub(&mtree->nodes[0], p);
}
void
mnode_close(mtree_p mtree)
{
if (mtree)
{
mtree_p next;
if (mtree->used > 0)
mnode_delete(&mtree->nodes[0]);
while (mtree)
{
next = mtree->next;
free(mtree);
mtree = next;
}
}
}
INLINE static mnode*
search_or_new_mnode(mtree_p mtree, wordbuf_p buf)
{
/* xPꂪ肵猟ɒlj */
int ch;
unsigned char *word;
mnode **ppnext;
mnode **res = NULL; /* To suppress warning for GCC */
mnode *root;
word = WORDBUF_GET(buf);
root = mtree->used > 0 ? &mtree->nodes[0] : NULL;
ppnext = &root;
while ((ch = *word) != 0)
{
res = ppnext;
if (! *res)
{
*res = mnode_new(mtree);
MNODE_SET_CH(*res, ch);
}
else if (MNODE_GET_CH(*res) != ch)
{
ppnext = &(*res)->next;
continue;
}
ppnext = &(*res)->child;
++word;
}
_ASSERT(*res != NULL);
return *res;
}
/*
* ̃m[hɃt@Cf[^܂Ƃ߂ĒljB
*/
mtree_p
mnode_load(mtree_p mtree, FILE* fp)
{
mnode *pp = NULL;
int mode = 0;
int ch;
wordbuf_p buf;
wordbuf_p prevlabel;
wordlist_p *ppword = NULL; /* To suppress warning for GCC */
/* ǂݍ݃obt@pϐ */
unsigned char cache[MNODE_BUFSIZE];
unsigned char *cache_ptr = cache;
unsigned char *cache_tail = cache;
buf = wordbuf_open();
prevlabel = wordbuf_open();
if (!fp || !buf || !prevlabel)
{
goto END_MNODE_LOAD;
}
/*
* EOF̏BBsȌ`̃t@CꍇlĂȂBe
* [hEOF̓pӂȂƐȂcʓ|Ȃ̂łȂBf[
* ^t@C͐ɊԈĂȂƂOuB
*/
do
{
if (cache_ptr >= cache_tail)
{
cache_ptr = cache;
cache_tail = cache + fread(cache, 1, MNODE_BUFSIZE, fp);
ch = (cache_tail <= cache && feof(fp)) ? EOF : *cache_ptr;
}
else
ch = *cache_ptr;
++cache_ptr;
/* :modẽI[g}g */
switch (mode)
{
case 0: /* xPꌟ[h */
/* ̓xPɂȂ肦܂ */
if (isspace(ch) || ch == EOF)
continue;
/* RgC`FbN */
else if (ch == ';')
{
mode = 2; /* s܂ŐHׂ[h ֈڍs */
continue;
}
else
{
mode = 1; /* xP̓Ǎ[h ֈڍs*/
wordbuf_reset(buf);
wordbuf_add(buf, (unsigned char)ch);
}
break;
case 1: /* xP̓Ǎ[h */
/* x̏Io */
switch (ch)
{
default:
wordbuf_add(buf, (unsigned char)ch);
break;
case '\t':
pp = search_or_new_mnode(mtree, buf);
wordbuf_reset(buf);
mode = 3; /* POǔ[h ֈڍs */
break;
}
break;
case 2: /* s܂ŐHׂ[h */
if (ch == '\n')
{
wordbuf_reset(buf);
mode = 0; /* xPꌟ[h ֖߂ */
}
break;
case 3: /* POǂݔ[h */
if (ch == '\n')
{
wordbuf_reset(buf);
mode = 0; /* xPꌟ[h ֖߂ */
}
else if (ch != '\t')
{
/* Pobt@Zbg */
wordbuf_reset(buf);
wordbuf_add(buf, (unsigned char)ch);
/* PꃊXg̍Ō(ꃉx) */
ppword = &pp->list;
while (*ppword)
ppword = &(*ppword)->next;
mode = 4; /* P̓ǂݍ݃[h ֈڍs */
}
break;
case 4: /* P̓ǂݍ݃[h */
switch (ch)
{
case '\t':
case '\n':
/* PL */
*ppword = wordlist_open_len(WORDBUF_GET(buf),
WORDBUF_LEN(buf));
wordbuf_reset(buf);
if (ch == '\t')
{
ppword = &(*ppword)->next;
mode = 3; /* POǂݔ[h ֖߂ */
}
else
{
ppword = NULL;
mode = 0; /* xPꌟ[h ֖߂ */
}
break;
default:
wordbuf_add(buf, (unsigned char)ch);
break;
}
break;
}
}
while (ch != EOF);
END_MNODE_LOAD:
wordbuf_close(buf);
wordbuf_close(prevlabel);
return mtree;
}
mtree_p
mnode_open(FILE* fp)
{
mtree_p mtree;
mtree = (mtree_p)calloc(1, sizeof(*mtree));
mtree->active = mtree;
if (mtree && fp)
mnode_load(mtree, fp);
return mtree;
}
#if 0
static int
mnode_size(mnode* p)
{
return p ? mnode_size(p->child) + mnode_size(p->next) + 1 : 0;
}
#endif
static mnode*
mnode_query_stub(mnode* node, const unsigned char* query)
{
while (1)
{
if (*query == MNODE_GET_CH(node))
return (*++query == '\0') ? node :
(node->child ? mnode_query_stub(node->child, query) : NULL);
if (!(node = node->next))
break;
}
return NULL;
}
mnode*
mnode_query(mtree_p mtree, const unsigned char* query)
{
return (query && *query != '\0' && mtree)
? mnode_query_stub(&mtree->nodes[0], query) : 0;
}
static void
mnode_traverse_stub(mnode* node, MNODE_TRAVERSE_PROC proc, void* data)
{
while (1)
{
if (node->child)
mnode_traverse_stub(node->child, proc, data);
proc(node, data);
if (!(node = node->next))
break;
}
}
void
mnode_traverse(mnode *node, MNODE_TRAVERSE_PROC proc, void* data)
{
if (node && proc)
{
proc(node, data);
if (node->child)
mnode_traverse_stub(node->child, proc, data);
}
}
|