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
|
/*
EPG2VDR Levenshtein Distance UDF for MySQL
Supports UTF-8 i throught iconv.
Copyright (C) 2013 Jörg Wendel
*/
#include "epglv.h"
//***************************************************************************
// Declarations
//***************************************************************************
struct workspace_t // structure to allocate memory in init and use it in core functions
{
char* str1; // internal buffer to store 1st string
char* str2; // internal buffer to store 2nd string
int* row0; // round buffer for levenshtein_core function
int* row1; // round buffer for levenshtein_core function
int* row2; // round buffer for levenshtein_core function
mbstate_t* mbstate; // buffer for mbsnrtowcs
iconv_t ic; // buffer for iconv
char iconv_init; // flag indicating if iconv has been inited before
};
//***************************************************************************
// Function Declarations
//***************************************************************************
int epglv_core(struct workspace_t *ws,
const char *str1, int len1,
const char *str2, int len2,
int w, int s, int a, int d, int limit);
char* epglv_utf8toiso(const char *str_src, long long *len_src,
struct workspace_t * ws, char *str_dst, int limit);
//***************************************************************************
// The Work
//***************************************************************************
//***************************************************************************
// init - check parameters and allocate memory for MySql
//***************************************************************************
my_bool base_epglv_init(UDF_INIT* init, UDF_ARGS* args, char* message)
{
struct workspace_t *ws;
// make sure both arguments are ok
if (args->arg_count != 2 || args->arg_type[0] != STRING_RESULT || args->arg_type[1] != STRING_RESULT)
{
strncpy(message, "EPGLV() requires arguments (string, string)", 80);
return 1;
}
init->maybe_null = 0; // EPGLV() will not be returning null
// attempt to allocate memory in which to calculate distance
ws = (struct workspace_t*)malloc(sizeof(struct workspace_t));
ws->mbstate = (mbstate_t*)malloc(sizeof(mbstate_t));
ws->str1 = (char*)malloc(sizeof(char)*(LENGTH_MAX+2)); // max allocated for UTF-8 complex string
ws->str2 = (char*)malloc(sizeof(char)*(LENGTH_MAX+2));
ws->row0 = (int*)malloc(sizeof(int)*(LENGTH_MAX+2));
ws->row1 = (int*)malloc(sizeof(int)*(LENGTH_MAX+2));
ws->row2 = (int*)malloc(sizeof(int)*(LENGTH_MAX+2));
ws->iconv_init = 0;
if (!ws || !ws->mbstate || !ws->str1 || !ws->str2 || !ws->row0 || !ws->row1 || !ws->row2)
{
free(ws->row2); free(ws->row1); free(ws->row0);
free(ws->str2); free(ws->str1);
free(ws->mbstate); free(ws);
strncpy(message, "EPGLV() failed to allocate memory", 80);
return 1;
}
if (!setlocale(LC_CTYPE, "de_DE.UTF-8"))
{
free(ws->row2); free(ws->row1); free(ws->row0);
free(ws->str2); free(ws->str1);
free(ws->mbstate); free(ws);
strncpy(message, "EPGLV() failed to change locale", 80);
return 1;
}
init->ptr = (char *) ws;
debug_print("%s", "init successful");
return 0;
}
//***************************************************************************
// deallocate memory, clean and close
//***************************************************************************
void base_epglv_deinit(UDF_INIT* init)
{
if (init->ptr)
{
struct workspace_t* ws = (struct workspace_t*)init->ptr;
if (ws->iconv_init)
iconv_close(ws->ic);
free(ws->row2);
free(ws->row1);
free(ws->row0);
free(ws->str2);
free(ws->str1);
free(ws->mbstate);
free(ws);
}
debug_print("%s", "bye");
}
//***************************************************************************
// check parameters and allocate memory for MySql
//***************************************************************************
long long base_epglv(UDF_INIT* init, UDF_ARGS* args, char* is_null, char* error,
long long* len1, long long* len2)
{
const char* str1 = args->args[0];
const char* str2 = args->args[1];
char* iso_str1 = 0;
char* iso_str2 = 0;
struct workspace_t* ws = (struct workspace_t*)init->ptr; // get a pointer to memory previously allocated
*error = 0;
*len1 = (!str1) ? 0 : args->lengths[0];
*len2 = (!str2) ? 0 : args->lengths[1];
long long max = *len1 > *len2 ? *len1 : *len2;
if (max >= LENGTH_MAX)
{
print("size(%lld) was bigger than %d, aborting", max, LENGTH_MAX);
return -1;
}
if (!*len1 && !*len2)
return 0;
if (!*len1 || !*len2)
return max;
if ((iso_str1 = epglv_utf8toiso(str1, len1, ws, ws->str1, max)) == 0)
return -1;
if ((iso_str2 = epglv_utf8toiso(str2, len2, ws, ws->str2, max)) == 0)
return -1;
return epglv_core(
ws,
iso_str1, *len1,
iso_str2, *len2,
/* swap */ 1,
/* substitution */ 1,
/* insertion */ 1,
/* deletion */ 1,
/* limit */ max);
}
//***************************************************************************
// core function
//***************************************************************************
int epglv_core(struct workspace_t* ws,
const char* str1, int len1,
const char* str2, int len2,
int w, int s, int a, int d, int limit)
{
int* row0 = ws->row0;
int* row1 = ws->row1;
int* row2 = ws->row2;
int i, j;
for (j = 0; j <= len2; j++)
row1[j] = j * a;
for (i = 0; i < len1; i++)
{
int* dummy;
row2[0] = (i + 1) * d;
for (j = 0; j < len2; j++)
{
// substitution
row2[j + 1] = row1[j] + s * (str1[i] != str2[j]);
// swap
if (i > 0 && j > 0 &&
str1[i - 1] == str2[j] &&
str1[i] == str2[j - 1] &&
row2[j + 1] > row0[j - 1] + w)
{
row2[j + 1] = row0[j - 1] + w;
}
// deletion
if (row2[j + 1] > row1[j + 1] + d)
row2[j + 1] = row1[j + 1] + d;
// insertion
if (row2[j + 1] > row2[j] + a)
row2[j + 1] = row2[j] + a;
}
dummy = row0;
row0 = row1;
row1 = row2;
row2 = dummy;
}
debug_print("returning(%d)", row1[len2]);
return row1[len2];
}
//**************************************************************************
// translates an UTF8 string to ISO with some error return codes
//**************************************************************************
char* epglv_utf8toiso(const char* str_src, long long* len_src,
struct workspace_t* ws, char* str_dst, int limit)
{
mbstate_t* mbstate = ws->mbstate;
size_t len_mbsnrtowcs, len_ret = LENGTH_MAX, len_min = LENGTH_MAX;
char* ret = str_dst;
char* in_s = (char*)str_src;
memset((void*)mbstate, '\0', sizeof(mbstate_t));
if ((len_mbsnrtowcs = mbsnrtowcs(0, &str_src, *len_src, 0, mbstate)) == -1)
{
print("mbsnrtowcs failed, str_src(%s): '%m'", str_src);
strncpy(str_dst, str_src, len_min);
str_dst[len_min] = 0; str_dst[len_min + 1] = 0;
*len_src = len_min;
return str_dst;
}
len_min = MIN(len_mbsnrtowcs, limit);
if (len_mbsnrtowcs == *len_src)
{
strncpy(str_dst, str_src, len_min);
str_dst[len_min] = 0; str_dst[len_min + 1] = 0;
*len_src = len_min;
return str_dst;
}
if (!ws->iconv_init)
{
if ((ws->ic = iconv_open("ISO8859-15", "UTF-8")) == (iconv_t)-1)
{
print("%s", "failed to initialize iconv '%m'");
return 0;
}
ws->iconv_init = 1;
}
if (iconv(ws->ic, &in_s, (size_t*)len_src, &ret, &len_ret) == -1)
{
if (errno != E2BIG)
{
print("in_s '%s' at '%s'; len_src (%lld) len_ret (%zu) '%m'", str_src, in_s, *len_src, len_ret);
strncpy(str_dst, str_src, len_min);
str_dst[len_min] = 0; str_dst[len_min + 1] = 0;
*len_src = len_min;
return str_dst;
}
print("inside E2BIG len_mbsnrtowcs(%zu) len_src(%lld)", len_mbsnrtowcs, *len_src);
len_mbsnrtowcs = len_min;
}
*len_src = len_min; // adjust converted length
str_dst[len_min] = 0; str_dst[len_min + 1] = 0;
iconv(ws->ic, 0, 0, 0, 0);
return str_dst;
}
|