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
|
/************************************************************************/
/* */
/* Miscelaneous utility routines for managing private dictionaries. */
/* */
/************************************************************************/
# include "config.h"
# include <ind.h>
# include <string.h>
# include <debugon.h>
/************************************************************************/
/* Move a word from forgotten to learnt, or the other way round. */
/************************************************************************/
int indMoveWord( void * fromInd,
void * toInd,
const unsigned char * word )
{
int rval= 0;
int accepted;
if ( fromInd &&
indGet( &accepted, fromInd, word ) >= 0 &&
accepted )
{
if ( indForget( fromInd, word ) )
{ SDEB((char *)word); rval= -1; }
}
if ( indGet( &accepted, toInd, word ) < 0 ||
! accepted )
{
if ( indPut( toInd, word ) < 0 )
{ SDEB((char *)word); rval= -1; }
}
return rval;
}
/************************************************************************/
/* Read a private dictionary and store its contents in two indices. */
/************************************************************************/
# define SPELBUFL 300
int indReadPrivateDictionary( FILE * f,
void ** pLearntInd,
void ** pForgotInd )
{
void * learntInd= *pLearntInd;
void * forgotInd= *pForgotInd;
unsigned char buf[SPELBUFL+ 1];
int rval= 0;
if ( ! learntInd )
{
learntInd= indMake();
if ( ! learntInd )
{ XDEB( learntInd ); return -1; }
}
if ( ! forgotInd )
{
forgotInd= indMake();
if ( ! forgotInd )
{ XDEB( forgotInd ); return -1; }
}
while( fgets( (char *)buf, SPELBUFL, f ) == (char *)buf )
{
buf[strlen( (char *)buf )- 1]= '\0';
switch( buf[0] )
{
case 'F':
(void) indMoveWord( learntInd, forgotInd, buf+ 2 );
break;
case 'L':
(void) indMoveWord( forgotInd, learntInd, buf+ 2 );
break;
default:
SDEB((char *)buf); rval= -1; break;
}
}
if ( ! rval )
{
*pLearntInd= learntInd;
*pForgotInd= forgotInd;
}
return rval;
}
/************************************************************************/
/* Learn/Forget a word and store a record about this in the private */
/* dictionary. */
/************************************************************************/
int indLearnWord( FILE * f,
void * learntInd,
void * forgotInd,
const unsigned char * word )
{
fprintf( f, "%s %s\n", "L", word );
if ( indMoveWord( forgotInd, learntInd, word ) )
{ SDEB((char *)word); return -1; }
return 0;
}
int indForgetWord( FILE * f,
void * learntInd,
void * forgotInd,
const unsigned char * word )
{
fprintf( f, "%s %s\n", "F", word );
if ( indMoveWord( learntInd, forgotInd, word ) )
{ SDEB((char *)word); return -1; }
return 0;
}
|