CHEWING_API int chewing_Init( const char *dataPath, const char *hashPath ) { /* initialize Tree, Char, and Dict */ /* FIXME: check the validation of dataPath */ InitTree( dataPath ); InitChar( dataPath ); InitDict( dataPath ); /* initialize Hash */ /* FIXME: check the validation of hashPath */ InitHash( hashPath ); /* initialize SymbolTable */ if ( ! InitSymbolTable( (char*) hashPath ) ) InitSymbolTable( (char*) dataPath ); if ( ! InitEasySymbolInput( (char *) hashPath ) ) InitEasySymbolInput( (char *) dataPath ); /* initialize HanyuPinYin table */ if ( ! InitHanyuPinYin( hashPath ) ) InitHanyuPinYin( dataPath ); #ifdef ENABLE_DEBUG { char *dbg_path; int failsafe = 1; dbg_path = getenv( "CHEWING_DEBUG" ); if ( dbg_path ) { fp_g = fopen( dbg_path, "w+" ); if ( fp_g ) failsafe = 0; } if ( failsafe == 1 ) { dbg_path = FAILSAFE_OUTPUT; fp_g = fopen( dbg_path, "w+" ); if ( ! fp_g ) { fprintf( stderr, "Failed to record debug message in file.\n" "--> Output to stderr\n" ); } } /* register debug service */ if ( fp_g ) addTerminateService( TerminateDebug ); } #endif bTerminateCompleted = 0; return 0; }
void InitTree( const char *prefix ) { char filename[ PATH_MAX ]; #ifdef USE_BINARY_DATA size_t offset = 0; size_t csize; #else FILE *infile; int i; #endif sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, PHONE_TREE_FILE ); #ifdef USE_BINARY_DATA plat_mmap_set_invalid( &tree_mmap ); tree_size = plat_mmap_create( &tree_mmap, filename, FLAG_ATTRIBUTE_READ ); assert( plat_mmap_is_valid( &tree_mmap ) ); if ( tree_size <= 0 ) return; csize = tree_size; tree = (TreeType *) plat_mmap_set_view( &tree_mmap, &offset, &csize ); assert( tree ); #else infile = fopen( filename, "r" ); assert( infile ); for ( i = 0; i < TREE_SIZE; i++ ) { if ( fscanf( infile, "%hu%d%d%d", &tree[ i ].phone_id, &tree[ i ].phrase_id, &tree[ i ].child_begin, &tree[ i ].child_end ) != 4 ) break; } fclose( infile ); #endif addTerminateService( TerminateTree ); }