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 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 = get_codepage_id (loop1);
207
208 if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
209 g_ptr_array_add (ret,
210 mc_search__cond_struct_new (lc_mc_search,
211 lc_mc_search->original.str,
212 lc_mc_search->original.charset));
213 else
214 {
215 GString *buffer;
216
217 buffer = mc_search__recode_str (lc_mc_search->original.str->str,
218 lc_mc_search->original.str->len,
219 lc_mc_search->original.charset, id);
220 g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, id));
221 g_string_free (buffer, TRUE);
222 }
223 }
224 }
225
226 lc_mc_search->prepared.conditions = ret;
227 lc_mc_search->prepared.result = (lc_mc_search->error == MC_SEARCH_E_OK);
228
229 return lc_mc_search->prepared.result;
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247 gboolean
248 mc_search_run (mc_search_t *lc_mc_search, const void *user_data, off_t start_search,
249 off_t end_search, gsize *found_len)
250 {
251 gboolean ret = FALSE;
252
253 if (lc_mc_search == NULL || user_data == NULL)
254 return FALSE;
255 if (!mc_search_is_type_avail (lc_mc_search->search_type))
256 {
257 mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _ (STR_E_UNKNOWN_TYPE));
258 return FALSE;
259 }
260
261 if (lc_mc_search->regex_match_info != NULL)
262 {
263 g_match_info_free (lc_mc_search->regex_match_info);
264 lc_mc_search->regex_match_info = NULL;
265 }
266
267 mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
268
269 if (!mc_search_prepare (lc_mc_search))
270 return FALSE;
271
272 switch (lc_mc_search->search_type)
273 {
274 case MC_SEARCH_T_NORMAL:
275 ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
276 break;
277 case MC_SEARCH_T_REGEX:
278 ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
279 break;
280 case MC_SEARCH_T_GLOB:
281 ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
282 break;
283 case MC_SEARCH_T_HEX:
284 ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
285 break;
286 default:
287 break;
288 }
289 return ret;
290 }
291
292
293
294 gboolean
295 mc_search_is_type_avail (mc_search_type_t search_type)
296 {
297 switch (search_type)
298 {
299 case MC_SEARCH_T_GLOB:
300 case MC_SEARCH_T_NORMAL:
301 case MC_SEARCH_T_REGEX:
302 case MC_SEARCH_T_HEX:
303 return TRUE;
304 default:
305 break;
306 }
307 return FALSE;
308 }
309
310
311
312 const mc_search_type_str_t *
313 mc_search_types_list_get (size_t *num)
314 {
315 #ifdef ENABLE_NLS
316 static gboolean i18n_flag = FALSE;
317
318 if (!i18n_flag)
319 {
320 for (size_t i = 0; i < G_N_ELEMENTS (mc_search__list_types); i++)
321 if (mc_search__list_types[i].str != NULL)
322 mc_search__list_types[i].str = _ (mc_search__list_types[i].str);
323 i18n_flag = TRUE;
324 }
325 #endif
326
327
328 if (num != NULL)
329 *num = G_N_ELEMENTS (mc_search__list_types) - 1;
330
331 return mc_search__list_types;
332 }
333
334
335
336 GString *
337 mc_search_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
338 {
339 GString *ret;
340
341 if (replace_str == NULL || replace_str->len == 0)
342 return g_string_new ("");
343
344 if (lc_mc_search == NULL)
345 return mc_g_string_dup (replace_str);
346
347 switch (lc_mc_search->search_type)
348 {
349 case MC_SEARCH_T_REGEX:
350 ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
351 break;
352 case MC_SEARCH_T_GLOB:
353 ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
354 break;
355 case MC_SEARCH_T_NORMAL:
356 ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
357 break;
358 case MC_SEARCH_T_HEX:
359 ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
360 break;
361 default:
362 ret = mc_g_string_dup (replace_str);
363 break;
364 }
365 return ret;
366 }
367
368
369
370 char *
371 mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str)
372 {
373 GString *ret;
374 GString *replace_str2;
375
376 replace_str2 = g_string_new (replace_str);
377 ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
378 g_string_free (replace_str2, TRUE);
379 return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
380 }
381
382
383
384 gboolean
385 mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search)
386 {
387 if (lc_mc_search == NULL)
388 return FALSE;
389 switch (lc_mc_search->search_type)
390 {
391 case MC_SEARCH_T_REGEX:
392 case MC_SEARCH_T_GLOB:
393 return FALSE;
394 default:
395 return TRUE;
396 }
397 }
398
399
400
401
402
403
404
405
406
407
408
409
410 gboolean
411 mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
412 mc_search_type_t type)
413 {
414 gboolean ret;
415 mc_search_t *search;
416
417 if (str == NULL)
418 return FALSE;
419
420 search = mc_search_new (pattern, pattern_charset);
421 if (search == NULL)
422 return FALSE;
423
424 search->search_type = type;
425 search->is_case_sensitive = TRUE;
426
427 if (type == MC_SEARCH_T_GLOB)
428 search->is_entire_line = TRUE;
429
430 ret = mc_search_run (search, str, 0, strlen (str), NULL);
431 mc_search_free (search);
432 return ret;
433 }
434
435
436
437 int
438 mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index)
439 {
440 if (lc_mc_search == NULL)
441 return 0;
442 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
443 return 0;
444 {
445 gint start_pos;
446 gint end_pos;
447
448 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
449 return (int) start_pos;
450 }
451 }
452
453
454
455 int
456 mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index)
457 {
458 if (lc_mc_search == NULL)
459 return 0;
460 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
461 return 0;
462 {
463 gint start_pos;
464 gint end_pos;
465
466 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
467 return (int) end_pos;
468 }
469 }
470
471
472
473
474
475
476
477
478
479
480 void
481 mc_search_set_error (mc_search_t *lc_mc_search, mc_search_error_t code, const gchar *format, ...)
482 {
483 lc_mc_search->error = code;
484
485 MC_PTR_FREE (lc_mc_search->error_str);
486
487 if (format != NULL)
488 {
489 va_list args;
490
491 va_start (args, format);
492 lc_mc_search->error_str = g_strdup_vprintf (format, args);
493 va_end (args);
494 }
495 }
496
497