This source file includes following definitions.
- mc_search__cond_struct_new
- mc_search__cond_struct_free
- mc_search_new
- mc_search_new_len
- mc_search_free
- mc_search_prepare
- mc_search_run
- mc_search_is_type_avail
- mc_search_types_list_get
- mc_search_prepare_replace_str
- mc_search_prepare_replace_str2
- mc_search_is_fixed_search_str
- mc_search
- mc_search_getstart_result_by_num
- mc_search_getend_result_by_num
- mc_search_set_error
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 #include <config.h>
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33
34 #include "lib/global.h"
35 #include "lib/strutil.h"
36 #include "lib/search.h"
37 #include "lib/util.h"
38 #include "lib/charsets.h"
39
40 #include "internal.h"
41
42
43
44
45
46
47
48
49
50
51
52 static const mc_search_type_str_t mc_search__list_types[] = {
53 { N_ ("No&rmal"), MC_SEARCH_T_NORMAL },
54 { N_ ("Re&gular expression"), MC_SEARCH_T_REGEX },
55 { N_ ("He&xadecimal"), MC_SEARCH_T_HEX },
56 { N_ ("Wil&dcard search"), MC_SEARCH_T_GLOB },
57 { NULL, MC_SEARCH_T_INVALID }
58 };
59
60
61
62
63
64 static mc_search_cond_t *
65 mc_search__cond_struct_new (mc_search_t *lc_mc_search, const GString *str, const char *charset)
66 {
67 mc_search_cond_t *mc_search_cond;
68
69 mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
70 mc_search_cond->str = mc_g_string_dup (str);
71 mc_search_cond->charset = g_strdup (charset);
72
73 switch (lc_mc_search->search_type)
74 {
75 case MC_SEARCH_T_GLOB:
76 mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
77 break;
78 case MC_SEARCH_T_NORMAL:
79 mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
80 break;
81 case MC_SEARCH_T_REGEX:
82 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
83 break;
84 case MC_SEARCH_T_HEX:
85 mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
86 break;
87 default:
88 break;
89 }
90 return mc_search_cond;
91 }
92
93
94
95 static void
96 mc_search__cond_struct_free (gpointer data)
97 {
98 mc_search_cond_t *mc_search_cond = (mc_search_cond_t *) data;
99
100 if (mc_search_cond->upper != NULL)
101 g_string_free (mc_search_cond->upper, TRUE);
102
103 if (mc_search_cond->lower != NULL)
104 g_string_free (mc_search_cond->lower, TRUE);
105
106 g_string_free (mc_search_cond->str, TRUE);
107 g_free (mc_search_cond->charset);
108
109 if (mc_search_cond->regex_handle != NULL)
110 g_regex_unref (mc_search_cond->regex_handle);
111
112 g_free (mc_search_cond);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 mc_search_t *
127 mc_search_new (const gchar *original, const gchar *original_charset)
128 {
129 if (original == NULL)
130 return NULL;
131
132 return mc_search_new_len (original, strlen (original), original_charset);
133 }
134
135
136
137
138
139
140
141
142
143
144
145 mc_search_t *
146 mc_search_new_len (const gchar *original, gsize original_len, const gchar *original_charset)
147 {
148 mc_search_t *lc_mc_search;
149
150 if (original == NULL || original_len == 0)
151 return NULL;
152
153 lc_mc_search = g_new0 (mc_search_t, 1);
154 lc_mc_search->original.str = g_string_new_len (original, original_len);
155 lc_mc_search->original.charset = g_strdup (
156 original_charset != NULL && *original_charset != '\0' ? original_charset : cp_display);
157
158 return lc_mc_search;
159 }
160
161
162
163 void
164 mc_search_free (mc_search_t *lc_mc_search)
165 {
166 if (lc_mc_search == NULL)
167 return;
168
169 g_string_free (lc_mc_search->original.str, TRUE);
170 g_free (lc_mc_search->original.charset);
171 g_free (lc_mc_search->error_str);
172
173 if (lc_mc_search->prepared.conditions != NULL)
174 g_ptr_array_free (lc_mc_search->prepared.conditions, TRUE);
175
176 if (lc_mc_search->regex_match_info != NULL)
177 g_match_info_free (lc_mc_search->regex_match_info);
178
179 if (lc_mc_search->regex_buffer != NULL)
180 g_string_free (lc_mc_search->regex_buffer, TRUE);
181
182 g_free (lc_mc_search);
183 }
184
185
186
187 gboolean
188 mc_search_prepare (mc_search_t *lc_mc_search)
189 {
190 GPtrArray *ret;
191
192 if (lc_mc_search->prepared.conditions != NULL)
193 return lc_mc_search->prepared.result;
194
195 ret = g_ptr_array_new_with_free_func (mc_search__cond_struct_free);
196 if (!lc_mc_search->is_all_charsets)
197 g_ptr_array_add (ret,
198 mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original.str,
199 lc_mc_search->original.charset));
200 else
201 {
202 gsize loop1;
203
204 for (loop1 = 0; loop1 < codepages->len; loop1++)
205 {
206 const char *id;
207
208 id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
209 if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
210 g_ptr_array_add (ret,
211 mc_search__cond_struct_new (lc_mc_search,
212 lc_mc_search->original.str,
213 lc_mc_search->original.charset));
214 else
215 {
216 GString *buffer;
217
218 buffer = mc_search__recode_str (lc_mc_search->original.str->str,
219 lc_mc_search->original.str->len,
220 lc_mc_search->original.charset, id);
221 g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, id));
222 g_string_free (buffer, TRUE);
223 }
224 }
225 }
226
227 lc_mc_search->prepared.conditions = ret;
228 lc_mc_search->prepared.result = (lc_mc_search->error == MC_SEARCH_E_OK);
229
230 return lc_mc_search->prepared.result;
231 }
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 gboolean
249 mc_search_run (mc_search_t *lc_mc_search, const void *user_data, off_t start_search,
250 off_t end_search, gsize *found_len)
251 {
252 gboolean ret = FALSE;
253
254 if (lc_mc_search == NULL || user_data == NULL)
255 return FALSE;
256 if (!mc_search_is_type_avail (lc_mc_search->search_type))
257 {
258 mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _ (STR_E_UNKNOWN_TYPE));
259 return FALSE;
260 }
261
262 if (lc_mc_search->regex_match_info != NULL)
263 {
264 g_match_info_free (lc_mc_search->regex_match_info);
265 lc_mc_search->regex_match_info = NULL;
266 }
267
268 mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
269
270 if (!mc_search_prepare (lc_mc_search))
271 return FALSE;
272
273 switch (lc_mc_search->search_type)
274 {
275 case MC_SEARCH_T_NORMAL:
276 ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
277 break;
278 case MC_SEARCH_T_REGEX:
279 ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
280 break;
281 case MC_SEARCH_T_GLOB:
282 ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
283 break;
284 case MC_SEARCH_T_HEX:
285 ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
286 break;
287 default:
288 break;
289 }
290 return ret;
291 }
292
293
294
295 gboolean
296 mc_search_is_type_avail (mc_search_type_t search_type)
297 {
298 switch (search_type)
299 {
300 case MC_SEARCH_T_GLOB:
301 case MC_SEARCH_T_NORMAL:
302 case MC_SEARCH_T_REGEX:
303 case MC_SEARCH_T_HEX:
304 return TRUE;
305 default:
306 break;
307 }
308 return FALSE;
309 }
310
311
312
313 const mc_search_type_str_t *
314 mc_search_types_list_get (size_t *num)
315 {
316
317 if (num != NULL)
318 *num = G_N_ELEMENTS (mc_search__list_types) - 1;
319
320 return mc_search__list_types;
321 }
322
323
324
325 GString *
326 mc_search_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
327 {
328 GString *ret;
329
330 if (replace_str == NULL || replace_str->len == 0)
331 return g_string_new ("");
332
333 if (lc_mc_search == NULL)
334 return mc_g_string_dup (replace_str);
335
336 switch (lc_mc_search->search_type)
337 {
338 case MC_SEARCH_T_REGEX:
339 ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
340 break;
341 case MC_SEARCH_T_GLOB:
342 ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
343 break;
344 case MC_SEARCH_T_NORMAL:
345 ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
346 break;
347 case MC_SEARCH_T_HEX:
348 ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
349 break;
350 default:
351 ret = mc_g_string_dup (replace_str);
352 break;
353 }
354 return ret;
355 }
356
357
358
359 char *
360 mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str)
361 {
362 GString *ret;
363 GString *replace_str2;
364
365 replace_str2 = g_string_new (replace_str);
366 ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
367 g_string_free (replace_str2, TRUE);
368 return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
369 }
370
371
372
373 gboolean
374 mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search)
375 {
376 if (lc_mc_search == NULL)
377 return FALSE;
378 switch (lc_mc_search->search_type)
379 {
380 case MC_SEARCH_T_REGEX:
381 case MC_SEARCH_T_GLOB:
382 return FALSE;
383 default:
384 return TRUE;
385 }
386 }
387
388
389
390
391
392
393
394
395
396
397
398
399 gboolean
400 mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
401 mc_search_type_t type)
402 {
403 gboolean ret;
404 mc_search_t *search;
405
406 if (str == NULL)
407 return FALSE;
408
409 search = mc_search_new (pattern, pattern_charset);
410 if (search == NULL)
411 return FALSE;
412
413 search->search_type = type;
414 search->is_case_sensitive = TRUE;
415
416 if (type == MC_SEARCH_T_GLOB)
417 search->is_entire_line = TRUE;
418
419 ret = mc_search_run (search, str, 0, strlen (str), NULL);
420 mc_search_free (search);
421 return ret;
422 }
423
424
425
426 int
427 mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index)
428 {
429 if (lc_mc_search == NULL)
430 return 0;
431 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
432 return 0;
433 {
434 gint start_pos;
435 gint end_pos;
436
437 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
438 return (int) start_pos;
439 }
440 }
441
442
443
444 int
445 mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index)
446 {
447 if (lc_mc_search == NULL)
448 return 0;
449 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
450 return 0;
451 {
452 gint start_pos;
453 gint end_pos;
454
455 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
456 return (int) end_pos;
457 }
458 }
459
460
461
462
463
464
465
466
467
468
469 void
470 mc_search_set_error (mc_search_t *lc_mc_search, mc_search_error_t code, const gchar *format, ...)
471 {
472 lc_mc_search->error = code;
473
474 MC_PTR_FREE (lc_mc_search->error_str);
475
476 if (format != NULL)
477 {
478 va_list args;
479
480 va_start (args, format);
481 lc_mc_search->error_str = g_strdup_vprintf (format, args);
482 va_end (args);
483 }
484 }
485
486