-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileToTrie.c
407 lines (360 loc) · 10.2 KB
/
FileToTrie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright (C) 2010 University of Tartu
* Authors: Reina Käärik, Siim Orasmaa, Kristo Tammeoja, Jaak Vilo
* Contact: siim . orasmaa {at} ut . ee
*
* This file is part of Generalized Edit Distance Tool.
*
* Generalized Edit Distance Tool is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Generalized Edit Distance Tool is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Generalized Edit Distance Tool.
* If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "FileToTrie.h"
// Read file into memory
char *readFile(char *filename){
int fd;
char *data;
struct stat sbuf;
if (filename == NULL) {
fprintf(stderr, "Insert filename!\n");
exit(1);
}
if ((fd = open(filename, O_RDONLY)) == -1) {
perror("Error on opening file");
exit(1);
}
if (stat(filename, &sbuf) == -1) {
perror("Error on receiving stat");
exit(1);
}
if ((data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (caddr_t)(-1)) {
perror("Could not map file");
exit(1);
}
if (close(fd) == -1){
perror("Error on closing file");
exit(1);
}
return data;
}
// Read a snippet from data and convert into double
double findValue(char *data, int i, int j){
char *value;
char *err;
double rep;
value = malloc(j-i+1);
if(value == NULL){
perror("Memory");
exit(1);
}
value[j-i] = '\0';
strncpy(value, (data +i), (j-i));
rep = strtod(value, &err);
free(value);
return rep;
}
// Builds add, rep and rem tries from given content of transformations file
int trieFromFile(char *data){
char *string1;
char *string2;
wchar_t *wstr1;
wchar_t *wstr2;
int w1;
int w2;
double v;
int datalen;
int i;
int j;
datalen = strlen(data);
string1 = malloc(2);
string2 = malloc(2);
i = 0;
j = 0;
while(i < datalen){
/*
* Setting weights of default edit operations:
* the line must begin with character '>', followed
* by "add" for addition, "rep" for replacement and
* "del" for deletion operation; after the operation
* marker, ':' is placed and finally comes the new
* weight as double.
*/
if(data[i] == '>'){
i = i +1;
/* Change weight of default add operation */
if(strncmp((char *)(data+i), "add", 3) == 0){
i +=4;
j = i;
while(data[j] != '\n' && data[j] != '\r' && j < strlen(data))
j++;
add = findValue(data, i, j);
if(data[j] == '\r') /* In case we are under Windows */
j = j + 2;
else j = j+1;
i = j;
}
/* Change weight of default replace operation */
else if(strncmp((char *)(data+i), "rep", 3) == 0){
i +=4;
j = i;
while(j < strlen(data) && data[j] != '\n' && data[j] != '\r') j++;
rep = findValue(data, i, j);
if(data[j] == '\r') /* In case we are under Windows */
j = j + 2;
else j = j+1;
i = j;
}
/* Change weight of default remove operation */
else if(strncmp((char *)(data+i), "rem", 3) == 0){
i +=4;
j = i;
while(j < strlen(data) && data[j] != '\n' && data[j] != '\r') j++;
rem = findValue(data, i, j);
if(data[j] == '\r') /* In case we are under Windows */
j = j + 2;
else j = j+1;
i = j;
}
}
/*
* Setting weights of generalized edit distance transformations;
*/
else {
j = i;
/* Locate the left side string of transformation */
while(data[j] != ':') j++;
string1 = (char *)realloc(string1, (j-i+1));
if(string1 == NULL){
perror("Memory");
exit(1);
}
string1[j-i] ='\0';
strncpy(string1, (data +i), (j-i));
/* Locate the right side string of transformation */
j++; i = j;
while(data[j] != ':') j++;
string2 = (char *)realloc(NULL, (j-i+1));
if(string2 == NULL){
perror("Memory");
exit(1);
}
string2[(j-i)]='\0';
strncpy(string2, (data +i), (j-i));
j++; i = j;
while(data[j] != '\n' && data[j] != '\r' && j < datalen) j++;
/* Find the weight of transformation */
v = findValue(data, i, j);
/* According to the type of transformation, add into suitable trie */
if(strlen(string1) == 0){
/* Add to add-operations trie */
w2 = mbstowcs(NULL, string2, 0);
wstr2 = (wchar_t *)localeToWchar(string2);
if(caseInsensitiveMode)
wstr2 = makeStringToIgnoreCase(wstr2, w2);
addToARTrie(addT, wstr2, w2, v);
/* If corresponding tracing-trie also exists, reverse the string and
add it to the tracing tire. */
if (traceAddT != NULL){
wchar_t *reversedWstr2;
reversedWstr2 = reverseWchar(wstr2, w2);
addToARTrie(traceAddT, reversedWstr2, w2, v);
free(reversedWstr2);
}
free(wstr2);
free(string2);
} else if(strlen(string2) == 0) {
/* Add to remove-operations trie */
w1 = mbstowcs(NULL, string1, 0);
wstr1 = (wchar_t *)localeToWchar(string1);
if(caseInsensitiveMode)
wstr1 = makeStringToIgnoreCase(wstr1, w1);
addToARTrie(remT, wstr1, w1, v);
/* If corresponding tracing-trie also exists, reverse the string and
add it to the tracing tire. */
if (traceRemT != NULL){
wchar_t *reversedWstr1;
reversedWstr1 = reverseWchar(wstr1, w1);
addToARTrie(traceRemT, reversedWstr1, w1, v);
free(reversedWstr1);
}
}else{
/* Add to replace-operations trie */
wstr2 = (wchar_t *)localeToWchar(string2);
wstr1 = (wchar_t *)localeToWchar(string1);
w1 = mbstowcs(NULL, string1, 0);
w2 = mbstowcs(NULL, string2, 0);
if(caseInsensitiveMode){
wstr1 = makeStringToIgnoreCase(wstr1, w1);
wstr2 = makeStringToIgnoreCase(wstr2, w2);
}
addToTrie(t, wstr1, w1, wstr2, v);
/* If corresponding tracing-trie also exists, reverse the string and
add it to the tracing tire. */
if (traceT != NULL){
wchar_t *reversedWstr1;
reversedWstr1 = reverseWchar(wstr1, w1);
addToTrie(traceT, reversedWstr1, w1, wstr2, v);
free(reversedWstr1);
}
free(string2);
free(wstr1);
}
if(data[j] == '\r') /* In case we are under Windows */
j = j + 2;
else
j = j + 1;
i = j;
}
}
free(string1);
/* Release the memory under data. */
munmap(data, strlen(data));
return 0;
}
// Builds ignore case list according to given file content
int ignoreCaseListFromFile(char *data){
char *string1;
char *string2;
wchar_t *wstr1;
wchar_t *wstr2;
int datalen;
int i;
int j;
datalen = strlen(data);
i = 0;
j = 0;
while(i < datalen){
j = i;
/* Find left side of the transformation */
while(data[j] != ':') j++;
string1 = (char *)malloc(j-i+1);
if(string1 == NULL){
perror("Memory");
exit(1);
}
string1[j-i] ='\0';
strncpy(string1, (data +i), (j-i));
/* Find right side of the transformation */
j++; i = j;
while(data[j] != '\n' && data[j] != '\r' && j < datalen) j++;
string2 = (char *)malloc(j-i+1);
if(string2 == NULL){
perror("Memory");
exit(1);
}
string2[(j-i)]='\0';
strncpy(string2, (data +i), (j-i));
/* Transform into wide-char string */
wstr2 = (wchar_t *)localeToWchar(string2);
wstr1 = (wchar_t *)localeToWchar(string1);
insertIgnoreCaseElement(wstr1, wstr2);
free(string1);
free(string2);
free(wstr1);
free(wstr2);
if(data[j] == '\r') /* In case we are under Windows */
j = j + 2;
else j = j+1;
i = j;
}
/* Release the file content */
munmap(data, strlen(data));
return 0;
}
// Normalizes a case of a single character
wchar_t makeToIgnoreCase(wchar_t s){
IgnoreCaseListElement *caseList;
caseList = ignoreCase;
if(caseList == NULL){
if(debug)
puts("Ignore Case list was empty!");
return s;
}
while(caseList != NULL){
if(s == caseList->left[0]){ // selle tähe case saame ära muuta
return caseList->right[0];
}
caseList = caseList->next;
}
return s;
}
// Normalizes case of a string
wchar_t *makeStringToIgnoreCase(wchar_t *string, int len){
int i;
// Attempts to transform every letter/character of given string
for(i = 0; i < len; i++){
string[i] = makeToIgnoreCase(string[i]);
}
return string;
}
// Transforms wchar into multibyte char
char *wcharToLocale(wchar_t *str){
char * ptr;
size_t s;
/* calculate needed space */
s = wcstombs(NULL, str, 0);
/* there are characters that could not
be converted to current locale */
if(s == -1){
puts("Error: Could not convert some characters to multibyte");
exit(1);
}
/* malloc the necessary space */
if((ptr = (char *)malloc(s + 1)) == NULL){
puts("Error: Could not allocate memory");
exit(1);
}
/* really do it */
wcstombs(ptr, str, s);
/* ensure NULL-termination */
ptr[s] = '\0';
return(ptr);
}
// Transforms multibyte char into wchar string
wchar_t *localeToWchar(char *str){
wchar_t * ptr;
size_t s;
/* calculate needed space*/
s = mbstowcs(NULL, str, 0);
/* is there an error in encoding? */
if(s == -1){
puts("Error: could not convert to wchar");
exit(1);
}
/* malloc the necessary space */
if((ptr = (wchar_t *)malloc((s + 1) * sizeof(wchar_t))) == NULL){
puts("Error: Could not allocate memory");
exit(1);
}
/* really do it */
mbstowcs(ptr, str, s);
/* ensure NULL-termination */
ptr[s] = L'\0';
return(ptr);
}
// Returns a new string that is a reversed version of given wchar string
wchar_t *reverseWchar(wchar_t *str, int strLen){
wchar_t *s;
if ((s = (wchar_t *)malloc((strLen+1) * sizeof(wchar_t))) == NULL){
puts("Error: Could not allocate memory");
exit(1);
}
s[strLen] = L'\0';
int i = strLen-1;
while (i > -1){
s[i]=str[(strLen-1)-i];
i--;
}
return s;
}