|
1 #include <stdio.h> |
|
2 #include <netinet/in.h> |
|
3 |
|
4 /* |
|
5 * idx2index - Part of the flow to convert a StarDict index to a dictd index. |
|
6 * Author: LluĂs Batlle |
|
7 * In order to convert a StarDict idx file to a dictd index file, pass: |
|
8 * ./idx2index < file.idx | LC_ALL=POSIX sort > file.index |
|
9 * */ |
|
10 |
|
11 static char num_to_ia5char(int n) |
|
12 { |
|
13 /* From RFC 1421 */ |
|
14 if (n >= 0 && n <= 25) |
|
15 return 'A' + n; |
|
16 else if (n >= 26 && n <= 51) |
|
17 return 'a' + (n - 26); |
|
18 else if (n >= 52 && n <= 61) |
|
19 return '0' + (n - 52); |
|
20 else if (n == 62) |
|
21 return '+'; |
|
22 else if (n == 63) |
|
23 return '-'; |
|
24 else |
|
25 return '!'; /* Error */ |
|
26 } |
|
27 |
|
28 static int num_to_ia5(char *dest, int n) |
|
29 { |
|
30 char tmp[20]; |
|
31 |
|
32 int i, max; |
|
33 |
|
34 for(i =0; i <= 10; ++i) |
|
35 { |
|
36 tmp[i] = num_to_ia5char(n % 64); |
|
37 if (n < 64) |
|
38 break; |
|
39 n /= 64; |
|
40 } |
|
41 |
|
42 max = i; |
|
43 |
|
44 /* reverse the number */ |
|
45 for (i=0; i<=max; ++i) |
|
46 dest[i] = tmp[max-i]; |
|
47 |
|
48 /* Ending '\0' */ |
|
49 dest[max+1] = '\0'; |
|
50 return max; |
|
51 } |
|
52 |
|
53 static int get_int() |
|
54 { |
|
55 int i; |
|
56 fread(&i, sizeof(int) , 1, stdin); |
|
57 i = ntohl(i); /* Network to Host order */ |
|
58 return i; |
|
59 } |
|
60 |
|
61 static int get_word(char * word) |
|
62 { |
|
63 int c; |
|
64 int count = 0; |
|
65 |
|
66 do |
|
67 { |
|
68 c = getchar(); |
|
69 if (c == EOF) |
|
70 break; |
|
71 word[count] = (char) c; |
|
72 ++count; |
|
73 } while (c != 0); |
|
74 return count; |
|
75 } |
|
76 |
|
77 int main() |
|
78 { |
|
79 char word[256]; |
|
80 |
|
81 do |
|
82 { |
|
83 int offset, length; |
|
84 int res; |
|
85 char c_offset[20], c_length[20]; |
|
86 |
|
87 res = get_word(word); |
|
88 if (res == 0) |
|
89 break; |
|
90 offset = get_int(); |
|
91 num_to_ia5(c_offset, offset); |
|
92 length = get_int(); |
|
93 num_to_ia5(c_length, length); |
|
94 printf("%s\t%s\t%s\n", word, c_offset, c_length); |
|
95 } while(1); |
|
96 return 0; |
|
97 } |