|
1 #include <stdio.h> |
|
2 #include "dictre.h" |
|
3 |
|
4 extern struct Word words[]; |
|
5 extern int nwords; |
|
6 extern struct Def defs[]; |
|
7 extern int ndefs; |
|
8 |
|
9 static char num_to_ia5char(int n) |
|
10 { |
|
11 /* From RFC 1421 */ |
|
12 if (n >= 0 && n <= 25) |
|
13 return 'A' + n; |
|
14 else if (n >= 26 && n <= 51) |
|
15 return 'a' + (n - 26); |
|
16 else if (n >= 52 && n <= 61) |
|
17 return '0' + (n - 52); |
|
18 else if (n == 62) |
|
19 return '+'; |
|
20 else if (n == 63) |
|
21 return '-'; |
|
22 else |
|
23 return '!'; /* Error */ |
|
24 } |
|
25 |
|
26 static int num_to_ia5(char *dest, int n) |
|
27 { |
|
28 char tmp[20]; |
|
29 |
|
30 int i, max; |
|
31 |
|
32 for(i =0; i <= 10; ++i) |
|
33 { |
|
34 tmp[i] = num_to_ia5char(n % 64); |
|
35 if (n < 64) |
|
36 break; |
|
37 n /= 64; |
|
38 } |
|
39 |
|
40 max = i; |
|
41 |
|
42 /* reverse the number */ |
|
43 for (i=0; i<=max; ++i) |
|
44 dest[i] = tmp[max-i]; |
|
45 |
|
46 /* Ending '\0' */ |
|
47 dest[max+1] = '\0'; |
|
48 return max; |
|
49 } |
|
50 |
|
51 static int write_dictionary_data(FILE *fdefs) |
|
52 { |
|
53 int i; |
|
54 |
|
55 int offset = 0; |
|
56 |
|
57 for (i=0; i < ndefs; ++i) |
|
58 { |
|
59 fwrite(defs[i].d, defs[i].length, 1, fdefs); |
|
60 defs[i].offset = offset; |
|
61 offset += defs[i].length; |
|
62 } |
|
63 } |
|
64 |
|
65 static void write_dictionary_index(FILE *index) |
|
66 { |
|
67 int i; |
|
68 |
|
69 for (i=0; i < nwords; ++i) |
|
70 { |
|
71 char offset_str[50]; |
|
72 char length_str[50]; |
|
73 |
|
74 num_to_ia5(offset_str, defs[words[i].def].offset); |
|
75 num_to_ia5(length_str, defs[words[i].def].length); |
|
76 fprintf(index, "%s\t%s\t%s\n", |
|
77 words[i].w, offset_str, length_str); |
|
78 } |
|
79 } |
|
80 |
|
81 void write_dictionary(const char *name) |
|
82 { |
|
83 FILE *i, *d; |
|
84 char tmpname[500]; |
|
85 |
|
86 strcpy(tmpname, name); |
|
87 strcat(tmpname, ".dict"); |
|
88 d = fopen(tmpname, "wb"); |
|
89 write_dictionary_data(d); |
|
90 fclose(d); |
|
91 |
|
92 strcpy(tmpname, name); |
|
93 strcat(tmpname, ".index"); |
|
94 i = fopen(tmpname, "wb"); |
|
95 write_dictionary_index(i); |
|
96 fclose(i); |
|
97 } |