1 /**
2 * dat2bin.c
3 *
4 * Copyright (c) 2005
5 * libchewing Core Team. See ChangeLog for details.
6 *
7 * See the file "COPYING" for information on usage and redistribution
8 * of this file.
9 */
10
11 /* @(#)dat2bin.c
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "global.h"
23 #include "char.h"
24 #include "private.h"
25
26 /* Some wrapper definitions */
27 #ifdef WIN32
28 #include <direct.h>
29 #define CHDIR(x) _chdir(x)
30 #define GETCWD(x,y) _getcwd(x,y)
31 #else /* POSIX */
32 #define CHDIR(x) chdir(x)
33 #define GETCWD(x,y) getcwd(x,y)
34 #endif
35
36 /* Let's include other utilities */
37 #define USED_IN_DAT2BIN
38 #include "sort_word.c"
39 #include "sort_dic.c"
40 #include "maketree.c"
41 #undef USED_IN_DAT2BIN
42
main(int argc,char * argv[])43 int main( int argc, char* argv[] )
44 {
45 char* prefix = argc < 2 ? "" : argv[1];
46 char filename[ 100 ];
47 char workdir[ 100 ];
48 int i;
49
50 if ( *prefix )
51 CHDIR( prefix );
52 else {
53 GETCWD( workdir, 100 );
54 prefix = workdir;
55 }
56
57 printf("sort_dic...\n");
58 sort_dic();
59
60 printf("maketree...\n");
61 maketree();
62
63 printf("sort_word...\n");
64 sort_word();
65
66 printf("convert dat files to binary format...\n");
67
68 sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, CHAR_INDEX_FILE );
69 FILE* fi = fopen( filename, "r" );
70 if ( !fi )
71 return 1;
72
73 strcat(filename, "_bin");
74 FILE* fo = fopen( filename, "wb" );
75
76 if( fo )
77 {
78 static uint16 *arrPhone;
79 static int *begin;
80 arrPhone = ALC( uint16, phone_num * 2 );
81 begin = ALC( int, phone_num * 2 );
82 for ( i = 0; i <= phone_num * 2; i++ )
83 {
84 if( fscanf( fi, "%hu %d", &arrPhone[i], &begin[i] ) < 2 )
85 {
86 fwrite( begin, sizeof(int), i, fo );
87 fwrite( arrPhone, sizeof(uint16), i, fo );
88 break;
89 }
90 }
91 free( arrPhone );
92 free( begin );
93 fclose( fo );
94 }
95 fclose( fi );
96
97 sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, PHONE_TREE_FILE );
98 fi = fopen( filename, "r" );
99 if ( !fi )
100 return 1;
101 strcat(filename, "_bin");
102 fo = fopen( filename, "wb" );
103
104 if( fo )
105 {
106 TreeType tree = {0};
107 for ( i = 0; i < tree_size * 2; i++ ) {
108 if ( fscanf( fi, "%hu%d%d%d",
109 &tree.phone_id,
110 &tree.phrase_id,
111 &tree.child_begin,
112 &tree.child_end ) != 4 )
113 break;
114 fwrite( &tree, sizeof(TreeType), 1, fo );
115 }
116 fclose( fo );
117 }
118 fclose( fi );
119
120 sprintf( filename, "%s" PLAT_SEPARATOR "%s", prefix, PH_INDEX_FILE );
121 fi = fopen( filename, "r" );
122 if( !fi )
123 return 1;
124 strcat( filename, "_bin" );
125 fo = fopen( filename, "wb" );
126 if( fo )
127 {
128 int i = 0;
129 int begin;
130 while ( !feof( fi ) )
131 {
132 fscanf( fi, "%d", &begin );
133 fwrite( &begin, sizeof(int), 1, fo );
134 }
135 fclose(fo);
136 }
137 fclose( fi );
138 return 0;
139 }
140
141