author | viric@mandarina |
Sat, 08 Sep 2007 09:13:15 +0200 | |
changeset 29 | 84abeba4ef3b |
parent 14 | a961bb8806b9 |
permissions | -rw-r--r-- |
0 | 1 |
#include <stdio.h> |
2 |
#include "dictre.h" |
|
3 |
||
4 |
static char * get_string(FILE *index, char *buffer) |
|
5 |
{ |
|
6 |
int c; |
|
7 |
int count; |
|
8 |
||
9 |
count = 0; |
|
10 |
do |
|
11 |
{ |
|
12 |
c = fgetc(index); |
|
13 |
if(c == EOF) |
|
14 |
{ |
|
15 |
count = 1; /* for next [count-1] */ |
|
16 |
break; |
|
17 |
} |
|
18 |
buffer[count++] = (char) c; |
|
19 |
} while(c != '\t' && c != '\n'); |
|
20 |
||
21 |
buffer[count-1] = '\0'; |
|
22 |
} |
|
23 |
||
24 |
char * get_word(FILE *index) |
|
25 |
{ |
|
26 |
char buffer[500]; |
|
27 |
char *out; |
|
28 |
int len; |
|
29 |
||
30 |
get_string(index, buffer); |
|
31 |
||
32 |
if ((len = strlen(buffer)) > 0) |
|
33 |
{ |
|
34 |
out = (char *) malloc(len + 1); |
|
35 |
strcpy(out, buffer); |
|
36 |
} |
|
37 |
else |
|
38 |
out = 0; |
|
39 |
||
40 |
return out; |
|
41 |
} |
|
42 |
||
43 |
static int char2val(char letter) |
|
44 |
{ |
|
45 |
switch(letter) |
|
46 |
{ |
|
47 |
case 'A': return 0; |
|
48 |
case 'B': return 1; |
|
49 |
case 'C': return 2; |
|
50 |
case 'D': return 3; |
|
51 |
case 'E': return 4; |
|
52 |
case 'F': return 5; |
|
53 |
case 'G': return 6; |
|
54 |
case 'H': return 7; |
|
55 |
case 'I': return 8; |
|
56 |
case 'J': return 9; |
|
57 |
case 'K': return 10; |
|
58 |
case 'L': return 11; |
|
59 |
case 'M': return 12; |
|
60 |
case 'N': return 13; |
|
61 |
case 'O': return 14; |
|
62 |
case 'P': return 15; |
|
63 |
case 'Q': return 16; |
|
64 |
case 'R': return 17; |
|
65 |
case 'S': return 18; |
|
66 |
case 'T': return 19; |
|
67 |
case 'U': return 20; |
|
68 |
case 'V': return 21; |
|
69 |
case 'W': return 22; |
|
70 |
case 'X': return 23; |
|
71 |
case 'Y': return 24; |
|
72 |
case 'Z': return 25; |
|
73 |
case 'a': return 26; |
|
74 |
case 'b': return 27; |
|
75 |
case 'c': return 28; |
|
76 |
case 'd': return 29; |
|
77 |
case 'e': return 30; |
|
78 |
case 'f': return 31; |
|
79 |
case 'g': return 32; |
|
80 |
case 'h': return 33; |
|
81 |
case 'i': return 34; |
|
82 |
case 'j': return 35; |
|
83 |
case 'k': return 36; |
|
84 |
case 'l': return 37; |
|
85 |
case 'm': return 38; |
|
86 |
case 'n': return 39; |
|
87 |
case 'o': return 40; |
|
88 |
case 'p': return 41; |
|
89 |
case 'q': return 42; |
|
90 |
case 'r': return 43; |
|
91 |
case 's': return 44; |
|
92 |
case 't': return 45; |
|
93 |
case 'u': return 46; |
|
94 |
case 'v': return 47; |
|
95 |
case 'w': return 48; |
|
96 |
case 'x': return 49; |
|
97 |
case 'y': return 50; |
|
98 |
case 'z': return 51; |
|
99 |
case '0': return 52; |
|
100 |
case '1': return 53; |
|
101 |
case '2': return 54; |
|
102 |
case '3': return 55; |
|
103 |
case '4': return 56; |
|
104 |
case '5': return 57; |
|
105 |
case '6': return 58; |
|
106 |
case '7': return 59; |
|
107 |
case '8': return 60; |
|
108 |
case '9': return 61; |
|
109 |
case '+': return 62; |
|
8
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
110 |
case '/': return 63; |
0 | 111 |
default: |
112 |
return 0; |
|
113 |
} |
|
114 |
} |
|
115 |
||
14 | 116 |
int str2int_len(const char *str, int length) |
0 | 117 |
{ |
118 |
int i = 0; |
|
119 |
int val = 0; |
|
120 |
||
121 |
while (i < length) |
|
122 |
{ |
|
123 |
val = char2val(str[i]) + val * 64; |
|
124 |
++i; |
|
125 |
} |
|
126 |
||
127 |
return val; |
|
128 |
} |
|
129 |
||
14 | 130 |
int str2int(const char *str) |
131 |
{ |
|
132 |
int length = strlen(str); |
|
133 |
return str2int_len(str, length); |
|
134 |
} |
|
135 |
||
0 | 136 |
int get_int(FILE *index) |
137 |
{ |
|
138 |
char buffer[500]; |
|
139 |
int val; |
|
140 |
||
141 |
get_string(index, buffer); |
|
142 |
||
143 |
if (strlen(buffer) > 0) |
|
144 |
{ |
|
145 |
val = str2int(buffer); |
|
146 |
} else |
|
147 |
val = -1; |
|
148 |
||
149 |
return val; |
|
150 |
} |
|
151 |
||
152 |
char * get_def(FILE *fdefs, int offset, int length) |
|
153 |
{ |
|
154 |
char *out; |
|
155 |
fseek(fdefs, offset, SEEK_SET); |
|
156 |
||
10 | 157 |
out = (char *) malloc(length+1); |
0 | 158 |
fread(out, length, 1, fdefs); |
10 | 159 |
out[length] = 0; |
0 | 160 |
return out; |
161 |
} |
|
8
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
162 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
163 |
static char num_to_ia5char(int n) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
164 |
{ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
165 |
/* From RFC 1421 */ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
166 |
if (n >= 0 && n <= 25) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
167 |
return 'A' + n; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
168 |
else if (n >= 26 && n <= 51) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
169 |
return 'a' + (n - 26); |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
170 |
else if (n >= 52 && n <= 61) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
171 |
return '0' + (n - 52); |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
172 |
else if (n == 62) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
173 |
return '+'; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
174 |
else if (n == 63) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
175 |
return '/'; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
176 |
else |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
177 |
return '!'; /* Error */ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
178 |
} |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
179 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
180 |
int num_to_ia5(char *dest, int n) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
181 |
{ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
182 |
char tmp[20]; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
183 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
184 |
int i, max; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
185 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
186 |
for(i =0; i <= 10; ++i) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
187 |
{ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
188 |
tmp[i] = num_to_ia5char(n % 64); |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
189 |
if (n < 64) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
190 |
break; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
191 |
n /= 64; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
192 |
} |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
193 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
194 |
max = i; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
195 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
196 |
/* reverse the number */ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
197 |
for (i=0; i<=max; ++i) |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
198 |
dest[i] = tmp[max-i]; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
199 |
|
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
200 |
/* Ending '\0' */ |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
201 |
dest[max+1] = '\0'; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
202 |
return max; |
09ec33061ff3
Added 'ia5' binary, and fixed the ia5 functions.
viric@llimona
parents:
6
diff
changeset
|
203 |
} |