~ubuntu-branches/ubuntu/lucid/libchewing/lucid

« back to all changes in this revision

Viewing changes to src/hash.c

  • Committer: Bazaar Package Importer
  • Author(s): Kanru Chen
  • Date: 2008-12-04 15:34:03 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20081204153403-l9cksddl1k2e71zy
Tags: 0.3.2-1
* New Upstream Version 
* [5d96bf5] Update watch address
* [351d417] Add Vcs-Git field
* [63cd26a] Add Vcs-Browser field
* [e2a59e2] Conflict with scim-chewing << 0.3.3
* [cd15be8] Change libchewing3-data from all to any since we build
  binary data now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *      Lu-chuan Kung and Kang-pen Chen.
6
6
 *      All rights reserved.
7
7
 *
8
 
 * Copyright (c) 2004, 2005, 2006, 2007
 
8
 * Copyright (c) 2004, 2005, 2006, 2007, 2008
9
9
 *      libchewing Core Team. See ChangeLog for details.
10
10
 *
11
11
 * See the file "COPYING" for information on usage and redistribution
16
16
#include <sys/stat.h>
17
17
/* ISO C99 Standard: 7.10/5.2.4.2.1 Sizes of integer types */
18
18
#include <limits.h>
 
19
#include <assert.h>
 
20
#include <stdlib.h>
 
21
#include <stdio.h>
19
22
 
20
23
#include "chewing-utf8-util.h"
21
 
#include "hash.h"
 
24
#include "hash-private.h"
22
25
#include "private.h"
23
26
#include "global.h"
24
27
 
25
28
int chewing_lifetime;
26
29
 
27
30
static HASH_ITEM *hashtable[ HASH_TABLE_SIZE ];
28
 
static char formatstring[ 30 ];
29
31
static char hashfilename[ 200 ];
30
32
 
31
33
int AlcUserPhraseSeq( UserPhraseData *pData, int phonelen, int wordlen )
38
40
static int PhoneSeqTheSame( const uint16 p1[], const uint16 p2[] )
39
41
{
40
42
        int i;
 
43
        if ( ! p1 || ! p2 )     /* FIXME: should not happend. */
 
44
                return 0;
41
45
 
42
46
        for ( i = 0; ( p1[ i ] != 0 && p2[ i ] != 0 ); i++ ) {
43
47
                if ( p1[ i ] != p2[ i ] )
59
63
 
60
64
HASH_ITEM *HashFindPhonePhrase( const uint16 phoneSeq[], HASH_ITEM *pItemLast )
61
65
{
62
 
        HASH_ITEM *pNow = ( ! pItemLast ) ?
63
 
                hashtable[ HashFunc( phoneSeq ) ] :
64
 
                pItemLast->next;
 
66
        HASH_ITEM *pNow = pItemLast ?
 
67
                        pItemLast->next :
 
68
                        hashtable[ HashFunc( phoneSeq ) ];
65
69
        
66
70
        for ( ; pNow; pNow = pNow->next ) 
67
71
                if ( PhoneSeqTheSame( pNow->data.phoneSeq, phoneSeq ) )
115
119
        return pItem;
116
120
}
117
121
 
 
122
#ifdef ENABLE_DEBUG
118
123
static void HashItem2String( char *str, HASH_ITEM *pItem )
119
124
{
120
125
        int i, len;
132
137
                pItem->data.maxfreq, pItem->data.origfreq );
133
138
        strcat( str, buf );
134
139
}
 
140
#endif
135
141
 
136
142
/* 
137
143
 * capacity of 'str' MUST bigger then FIELD_SIZE !
138
144
 */
139
 
static void HashItem2Binary( char *str, HASH_ITEM *pItem )
 
145
void HashItem2Binary( char *str, HASH_ITEM *pItem )
140
146
{
141
147
        int i, phraselen;
142
148
        uint16 *pshort;
143
 
        unsigned char buf[ FIELD_SIZE ], *puc;
 
149
        unsigned char *puc;
144
150
 
145
151
        memset(str, 0, FIELD_SIZE);
146
152
        if ( sizeof(int) * 4 + ueStrLen( pItem->data.wordSeq ) * 2 +
183
189
        fwrite( &chewing_lifetime, 1, 4, outfile );
184
190
#ifdef ENABLE_DEBUG
185
191
        sprintf( str, "%d", chewing_lifetime );
186
 
        DEBUG_OUT( 
187
 
                "HashModify-1: formatstring='%s',printing '%s'\n", 
188
 
                formatstring, str );
 
192
        DEBUG_OUT( "HashModify-1: '%-75s'\n", str );
189
193
        DEBUG_FLUSH;
190
194
#endif
191
195
 
202
206
        }
203
207
#ifdef ENABLE_DEBUG
204
208
        HashItem2String( str, pItem );
205
 
        DEBUG_OUT( 
206
 
                "HashModify-2: formatstring='%s',printing '%s'\n",
207
 
                formatstring, str );
 
209
        DEBUG_OUT( "HashModify-2: '%-75s'\n", str );
208
210
        DEBUG_FLUSH;
209
211
#endif
210
212
        HashItem2Binary( str, pItem );
236
238
 */
237
239
int ReadHashItem_bin( const char *srcbuf, HASH_ITEM *pItem, int item_index )
238
240
{
239
 
        int len, i, word_len, ptr;
 
241
        int len, i;
240
242
        uint16 *pshort;
241
 
        char wordbuf[ 64 ];
242
243
        unsigned char recbuf[ FIELD_SIZE ], *puc;
243
244
 
244
245
        memcpy( recbuf, srcbuf, FIELD_SIZE );
349
350
        return tf;
350
351
}
351
352
 
352
 
static char *_load_hash_file( const char *filename, int *size )
 
353
char *_load_hash_file( const char *filename, int *size )
353
354
{
354
355
        int flen;
355
356
        char *pd = NULL;
383
384
{
384
385
        FILE *txtfile;
385
386
        char oldname[ 256 ], *dump, *seekdump;
386
 
        HASH_ITEM item, *pItem;
387
 
        int item_index, hashvalue, iret, tflen;
 
387
        HASH_ITEM item;
 
388
        int item_index, iret, tflen;
388
389
 
389
390
        /* allocate dump buffer */
390
391
        txtfile = open_file_get_length( ofilename, "r", &tflen );
483
484
}
484
485
#endif
485
486
 
486
 
int ReadHash( const char *path )
 
487
static HASH_ITEM *pHead = NULL;
 
488
 
 
489
static void TerminateHash()
 
490
{
 
491
        HASH_ITEM *pItem;
 
492
        while ( pHead ) {
 
493
                pItem = pHead;
 
494
                pHead = pItem->next;
 
495
                DEBUG_CHECKPOINT();
 
496
                free( pItem->data.phoneSeq );
 
497
                free( pItem->data.wordSeq );
 
498
                free( pItem );
 
499
        }
 
500
        pHead = NULL;
 
501
}
 
502
 
 
503
int InitHash( const char *path )
487
504
{
488
505
        HASH_ITEM item, *pItem, *pPool = NULL;
489
506
        int item_index, hashvalue, iret, fsize, hdrlen, oldest = INT_MAX;
507
524
        } else {
508
525
                sprintf( hashfilename, "%s" PLAT_SEPARATOR "%s", path, HASH_FILE );
509
526
        }
510
 
        memset( hashtable, 0, HASH_TABLE_SIZE );
511
 
        sprintf( formatstring, "%%-%ds", FIELD_SIZE );
 
527
        memset( hashtable, (int) NULL, HASH_TABLE_SIZE );
512
528
 
513
529
open_hash_file:
514
530
        dump = _load_hash_file( hashfilename, &fsize );
525
541
                }
526
542
                chewing_lifetime = 0;
527
543
                fwrite( BIN_HASH_SIG, 1, strlen( BIN_HASH_SIG ), outfile );
528
 
                fwrite( &chewing_lifetime, 1, sizeof(chewing_lifetime), outfile );
 
544
                fwrite( &chewing_lifetime, 1,
 
545
                                sizeof(chewing_lifetime), outfile );
529
546
                fclose( outfile );
530
547
        }
531
548
        else {
532
 
                char header[ 5 ];
 
549
                static int is_set_pHead = 0;
533
550
                if ( memcmp(dump, BIN_HASH_SIG, strlen(BIN_HASH_SIG)) != 0 ) {
534
551
                        /* perform migrate from text-based to binary form */
535
552
                        free( dump );
559
576
                        memcpy( pItem, &item, sizeof( HASH_ITEM ) );
560
577
                        pItem->next = pPool;
561
578
                        pPool = pItem;
 
579
                        if ( ! is_set_pHead ) {
 
580
                                pHead = pItem;
 
581
                                is_set_pHead = 1;
 
582
                        }
 
583
 
562
584
                        if ( oldest > pItem->data.recentTime ) {
563
585
                                oldest = pItem->data.recentTime;
564
586
                        }
579
601
                }
580
602
                chewing_lifetime -= oldest;
581
603
        }
 
604
        addTerminateService( TerminateHash );
582
605
        return 1;
583
606
}
584
607