250 #include "ispell_checker.h"
254 static void dumpindex P ((
struct flagptr * indexp,
int depth));
264 int ISpellChecker::linit (
char *hashname)
270 struct flagent * entry;
271 struct flagptr * ind;
276 if ((fpHash = fopen (hashname,
"rb")) == NULL)
281 m_hashsize = fread (
reinterpret_cast<char *
>(&m_hashheader), 1,
sizeof m_hashheader, fpHash);
282 if (m_hashsize <
static_cast<int>(
sizeof(m_hashheader)))
285 fprintf (stderr, LOOKUP_C_CANT_READ, hashname);
286 else if (m_hashsize == 0)
287 fprintf (stderr, LOOKUP_C_NULL_HASH, hashname);
290 LOOKUP_C_SHORT_HASH (m_hashname, m_hashsize,
291 static_cast<int>(
sizeof m_hashheader)));
294 else if (m_hashheader.magic != MAGIC)
297 LOOKUP_C_BAD_MAGIC (hashname,
static_cast<unsigned int>(MAGIC),
298 static_cast<unsigned int>(m_hashheader.magic)));
301 else if (m_hashheader.magic2 != MAGIC)
304 LOOKUP_C_BAD_MAGIC2 (hashname,
static_cast<unsigned int>(MAGIC),
305 static_cast<unsigned int>(m_hashheader.magic2)));
310 || m_hashheader.maxstringchars != MAXSTRINGCHARS
311 || m_hashheader.maxstringcharlen != MAXSTRINGCHARLEN)
314 LOOKUP_C_BAD_OPTIONS (
static_cast<unsigned int>(m_hashheader.compileoptions),
315 m_hashheader.maxstringchars, m_hashheader.maxstringcharlen,
316 static_cast<unsigned int>(COMPILEOPTIONS), MAXSTRINGCHARS, MAXSTRINGCHARLEN));
323 calloc (
static_cast<unsigned>(m_hashheader.tblsize),
sizeof (
struct dent));
324 m_hashsize = m_hashheader.tblsize;
325 m_hashstrings =
static_cast<char *
>(malloc(
static_cast<unsigned>(m_hashheader.stringsize)));
327 m_numsflags = m_hashheader.stblsize;
328 m_numpflags = m_hashheader.ptblsize;
329 m_sflaglist = (
struct flagent *)
330 malloc ((m_numsflags + m_numpflags) *
sizeof (
struct flagent));
331 if (m_hashtbl == NULL || m_hashstrings == NULL || m_sflaglist == NULL)
333 fprintf (stderr, LOOKUP_C_NO_HASH_SPACE);
336 m_pflaglist = m_sflaglist + m_numsflags;
339 if( fread ( m_hashstrings, 1,
static_cast<unsigned>(m_hashheader.stringsize), fpHash)
340 !=
static_cast<size_t>(m_hashheader.stringsize) )
342 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
343 fprintf (stderr,
"stringsize err\n" );
346 if ( m_hashheader.compileoptions & 0x04 )
348 if( fread (
reinterpret_cast<char *
>(m_hashtbl), 1,
static_cast<unsigned>(m_hashheader.tblsize) *
sizeof(
struct dent), fpHash)
349 != (
static_cast<size_t>(m_hashheader.tblsize * sizeof (
struct dent))))
351 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
357 for( x=0; x<m_hashheader.tblsize; x++ )
359 if( fread (
reinterpret_cast<char*
>(m_hashtbl+x),
sizeof(
struct dent)-
sizeof( MASKTYPE ), 1, fpHash)
362 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
368 if (fread (
reinterpret_cast<char *
>(m_sflaglist), 1,
369 static_cast<unsigned>(m_numsflags+ m_numpflags) *
sizeof (
struct flagent), fpHash)
370 != (m_numsflags + m_numpflags) *
sizeof (
struct flagent))
372 fprintf (stderr, LOOKUP_C_BAD_FORMAT);
378 for (i = m_hashsize, dp = m_hashtbl; --i >= 0; dp++)
380 if (dp->word == (
char *) -1)
383 dp->word = &m_hashstrings [
reinterpret_cast<size_t>(dp->word) ];
384 if (dp->next == (
struct dent *) -1)
387 dp->next = &m_hashtbl [
reinterpret_cast<size_t>(dp->next) ];
391 for (i = m_numsflags + m_numpflags, entry = m_sflaglist; --i >= 0; entry++)
394 entry->strip =
reinterpret_cast<ichar_t *
>(&m_hashstrings[
reinterpret_cast<size_t>(entry->strip)]);
398 entry->affix =
reinterpret_cast<ichar_t *
>(&m_hashstrings[
reinterpret_cast<size_t>(entry->affix)]);
407 for (i = m_numsflags, entry = m_sflaglist; i > 0; i--, entry++)
409 if (entry->affl == 0)
412 ind = &m_sflagindex[0];
417 cp = entry->affix + entry->affl - 1;
418 ind = &m_sflagindex[*cp];
420 while (ind->numents == 0 && ind->pu.fp != NULL)
422 if (cp == entry->affix)
424 ind = &ind->pu.fp[0];
429 ind = &ind->pu.fp[*--cp];
434 if (ind->numents == 0)
447 if (!viazero && ind->numents >= MAXSEARCH
448 && icharcmp (entry->affix, ind->pu.ent->affix) != 0)
451 entry = ind->pu.ent - 1;
452 i = m_numsflags - (entry - m_sflaglist);
455 calloc (
static_cast<unsigned>(SET_SIZE + m_hashheader.nstrchars),
456 sizeof (
struct flagptr));
457 if (ind->pu.fp == NULL)
459 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
470 for (i = m_numpflags, entry = m_pflaglist; i > 0; i--, entry++)
472 if (entry->affl == 0)
475 ind = &m_pflagindex[0];
481 ind = &m_pflagindex[*cp++];
483 while (ind->numents == 0 && ind->pu.fp != NULL)
487 ind = &ind->pu.fp[0];
492 ind = &ind->pu.fp[*cp++];
497 if (ind->numents == 0)
510 if (!viazero && ind->numents >= MAXSEARCH
511 && icharcmp (entry->affix, ind->pu.ent->affix) != 0)
514 entry = ind->pu.ent - 1;
515 i = m_numpflags - (entry - m_pflaglist);
517 static_cast<struct flagptr *
>(calloc(SET_SIZE + m_hashheader.nstrchars,
518 sizeof (
struct flagptr)));
519 if (ind->pu.fp == NULL)
521 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
528 fprintf (stderr,
"Prefix index table:\n");
529 dumpindex (m_pflagindex, 0);
530 fprintf (stderr,
"Suffix index table:\n");
531 dumpindex (m_sflagindex, 0);
533 if (m_hashheader.nstrchartype == 0)
537 m_chartypes = (
struct strchartype *)
538 malloc (m_hashheader.nstrchartype * sizeof (
struct strchartype));
539 if (m_chartypes == NULL)
541 fprintf (stderr, LOOKUP_C_NO_LANG_SPACE);
544 for (i = 0, nextchar = m_hashheader.strtypestart;
545 i < m_hashheader.nstrchartype;
548 m_chartypes[i].name = &m_hashstrings[nextchar];
549 nextchar += strlen (m_chartypes[i].name) + 1;
550 m_chartypes[i].deformatter = &m_hashstrings[nextchar];
551 nextchar += strlen (m_chartypes[i].deformatter) + 1;
552 m_chartypes[i].suffixes = &m_hashstrings[nextchar];
553 while (m_hashstrings[nextchar] !=
'\0')
554 nextchar += strlen (&m_hashstrings[nextchar]) + 1;
565 #define FREEP(p) do { if (p) free(p); } while (0)
571 void ISpellChecker::initckch (
char *wchars)
576 for (c = 0; c < static_cast<ichar_t>(SET_SIZE+ m_hashheader.nstrchars); ++c)
586 else if (isboundarych (c))
594 while (m_Trynum < SET_SIZE && *wchars !=
'\0')
596 if (*wchars !=
'n' && *wchars !=
'\\')
608 if (isdigit (wchars[0]))
611 if (isdigit (wchars[1]))
614 if (isdigit (wchars[2]))
618 if (wchars[-1] ==
'n')
620 wchars += strlen (num);
625 wchars += strlen (num);
642 if (!m_hashheader.wordchars[c])
644 m_hashheader.wordchars[c] = 1;
645 m_hashheader.sortorder[c] = m_hashheader.sortval++;
656 void ISpellChecker::clearindex (
struct flagptr *indexp)
659 for (i = 0; i < SET_SIZE + m_hashheader.nstrchars; i++, indexp++)
661 if (indexp->numents == 0 && indexp->pu.fp != NULL)
663 clearindex(indexp->pu.fp);
670 static void dumpindex (indexp, depth)
671 struct flagptr * indexp;
677 char stripbuf[INPUTWORDLEN + 4 * MAXAFFIXLEN + 4];
679 for (i = 0; i < SET_SIZE + hashheader.nstrchars; i++, indexp++)
681 if (indexp->numents == 0 && indexp->pu.fp != NULL)
683 for (j = depth; --j >= 0; )
685 if (i >=
' ' && i <=
'~')
688 fprintf (stderr,
"0x%x", i);
690 dumpindex (indexp->pu.fp, depth + 1);
692 else if (indexp->numents)
694 for (j = depth; --j >= 0; )
696 if (i >=
' ' && i <=
'~')
699 fprintf (stderr,
"0x%x", i);
700 fprintf (stderr,
" -> %d entries\n", indexp->numents);
701 for (k = 0; k < indexp->numents; k++)
703 for (j = depth; --j >= 0; )
705 if (indexp->pu.ent[k].stripl)
707 ichartostr (stripbuf, indexp->pu.ent[k].strip,
709 fprintf (stderr,
" entry %d (-%s,%s)\n",
710 &indexp->pu.ent[k] - sflaglist,
712 indexp->pu.ent[k].affl
713 ? ichartosstr (indexp->pu.ent[k].affix, 1) :
"-");
716 fprintf (stderr,
" entry %d (%s)\n",
717 &indexp->pu.ent[k] - sflaglist,
718 ichartosstr (indexp->pu.ent[k].affix, 1));
733 struct dent * ISpellChecker::ispell_lookup (ichar_t *s,
int dotree)
737 char schar[INPUTWORDLEN + MAXAFFIXLEN];
739 dp = &m_hashtbl[hash (s, m_hashsize)];
740 if (ichartostr (schar, s,
sizeof schar, 1))
741 fprintf (stderr, WORD_TOO_LONG (schar));
742 for ( ; dp != NULL; dp = dp->next)
746 if (s1 && s1[0] == schar[0] && strcmp (s1 + 1, schar + 1) == 0)
748 #ifndef NO_CAPITALIZATION_SUPPORT
749 while (dp->flagfield & MOREVARIANTS)
756 void ISpellChecker::alloc_ispell_struct()
761 void ISpellChecker::free_ispell_struct()