1 /**
2  * dict.h
3  *
4  * Copyright (c) 1999, 2000, 2001
5  *	Lu-chuan Kung and Kang-pen Chen.
6  *	All rights reserved.
7  *
8  * Copyright (c) 2004, 2005
9  *	libchewing Core Team. See ChangeLog for details.
10  *
11  * See the file "COPYING" for information on usage and redistribution
12  * of this file.
13  */
14 
15 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 
19 #include "private.h"
20 #include "plat_mmap.h"
21 #include "dict.h"
22 
23 #ifdef USE_BINARY_DATA
24 static int *begin = NULL;
25 static plat_mmap m_mmap;
26 #else
27 static int begin[ PHONE_PHRASE_NUM + 1 ];
28 #endif
29 static FILE *dictfile;
30 static int end_pos;
31 
fgettab(char * buf,int maxlen,FILE * fp)32 static char *fgettab( char *buf, int maxlen, FILE *fp )
33 {
34 	int i;
35 
36 	for ( i = 0; i < maxlen; i++ ) {
37 		buf[ i ] = (char) fgetc( fp );
38 		if ( feof( fp ) )
39 			break;
40 		if ( buf[ i ] == '\t' )
41 			break;
42 	}
43 	if ( feof( fp ) )
44 		return 0;
45 	buf[ i ] = '\0';
46 	return buf;
47 }
48 
TerminateDict()49 static void TerminateDict()
50 {
51 	if ( dictfile )
52 		fclose( dictfile );
53 #ifdef USE_BINARY_DATA
54 	if ( begin )
55 		free( begin );
56 #endif
57 }
58 
InitDict(const char * prefix)59 int InitDict( const char *prefix )
60 {
61 	FILE *indexfile;
62 	int i;
63 	char filename[ 100 ];
64 
65 #ifdef USE_BINARY_DATA
66 	unsigned int dictSize;
67 	size_t offset = 0;
68 	size_t csize = 0;
69 #endif
70 
71 	sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, DICT_FILE );
72 	dictfile = fopen( filename, "r" );
73 
74 	sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, PH_INDEX_FILE );
75 
76 #ifdef USE_BINARY_DATA
77 	dictSize = plat_mmap_create(
78 			&m_mmap,
79                         filename,
80                         FLAG_ATTRIBUTE_READ );
81 	if ( dictSize == 0 )
82 		return 0;
83 	csize = dictSize + sizeof(int);
84 	begin = plat_mmap_set_view( &m_mmap, &offset, &csize );
85 
86 	plat_mmap_close( &m_mmap );
87 #else
88 	indexfile = fopen( filename, "r" );
89 	assert( dictfile && indexfile );
90 	i = 0;
91 	while ( !feof( indexfile ) )
92 		fscanf( indexfile, "%d", &begin[ i++ ] );
93 	fclose( indexfile );
94 #endif
95 	addTerminateService( TerminateDict );
96 	return 1;
97 }
98 
Str2Phrase(Phrase * phr_ptr)99 static void Str2Phrase( Phrase *phr_ptr )
100 {
101 	char buf[ 1000 ];
102 
103 	fgettab( buf, 1000, dictfile );
104 	sscanf( buf, "%s %d", phr_ptr->phrase, &( phr_ptr->freq ) );
105 }
106 
GetPhraseFirst(Phrase * phr_ptr,int phone_phr_id)107 int GetPhraseFirst( Phrase *phr_ptr, int phone_phr_id )
108 {
109 	assert( ( 0 <= phone_phr_id ) && ( phone_phr_id < PHONE_PHRASE_NUM ) );
110 
111 	fseek( dictfile, begin[ phone_phr_id ], SEEK_SET );
112 	end_pos = begin[ phone_phr_id + 1 ];
113 	Str2Phrase( phr_ptr );
114 	return 1;
115 }
116 
GetPhraseNext(Phrase * phr_ptr)117 int GetPhraseNext( Phrase *phr_ptr )
118 {
119 	if ( ftell( dictfile ) >= end_pos )
120 		return 0;
121 	Str2Phrase( phr_ptr );
122 	return 1;
123 }
124 
125