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 = 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
316 if (num != NULL)
317 *num = G_N_ELEMENTS (mc_search__list_types) - 1;
318
319 return mc_search__list_types;
320 }
321
322
323
324 GString *
325 mc_search_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
326 {
327 GString *ret;
328
329 if (replace_str == NULL || replace_str->len == 0)
330 return g_string_new ("");
331
332 if (lc_mc_search == NULL)
333 return mc_g_string_dup (replace_str);
334
335 switch (lc_mc_search->search_type)
336 {
337 case MC_SEARCH_T_REGEX:
338 ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
339 break;
340 case MC_SEARCH_T_GLOB:
341 ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
342 break;
343 case MC_SEARCH_T_NORMAL:
344 ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
345 break;
346 case MC_SEARCH_T_HEX:
347 ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
348 break;
349 default:
350 ret = mc_g_string_dup (replace_str);
351 break;
352 }
353 return ret;
354 }
355
356
357
358 char *
359 mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str)
360 {
361 GString *ret;
362 GString *replace_str2;
363
364 replace_str2 = g_string_new (replace_str);
365 ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
366 g_string_free (replace_str2, TRUE);
367 return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
368 }
369
370
371
372 gboolean
373 mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search)
374 {
375 if (lc_mc_search == NULL)
376 return FALSE;
377 switch (lc_mc_search->search_type)
378 {
379 case MC_SEARCH_T_REGEX:
380 case MC_SEARCH_T_GLOB:
381 return FALSE;
382 default:
383 return TRUE;
384 }
385 }
386
387
388
389
390
391
392
393
394
395
396
397
398 gboolean
399 mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
400 mc_search_type_t type)
401 {
402 gboolean ret;
403 mc_search_t *search;
404
405 if (str == NULL)
406 return FALSE;
407
408 search = mc_search_new (pattern, pattern_charset);
409 if (search == NULL)
410 return FALSE;
411
412 search->search_type = type;
413 search->is_case_sensitive = TRUE;
414
415 if (type == MC_SEARCH_T_GLOB)
416 search->is_entire_line = TRUE;
417
418 ret = mc_search_run (search, str, 0, strlen (str), NULL);
419 mc_search_free (search);
420 return ret;
421 }
422
423
424
425 int
426 mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index)
427 {
428 if (lc_mc_search == NULL)
429 return 0;
430 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
431 return 0;
432 {
433 gint start_pos;
434 gint end_pos;
435
436 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
437 return (int) start_pos;
438 }
439 }
440
441
442
443 int
444 mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index)
445 {
446 if (lc_mc_search == NULL)
447 return 0;
448 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
449 return 0;
450 {
451 gint start_pos;
452 gint end_pos;
453
454 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
455 return (int) end_pos;
456 }
457 }
458
459
460
461
462
463
464
465
466
467
468 void
469 mc_search_set_error (mc_search_t *lc_mc_search, mc_search_error_t code, const gchar *format, ...)
470 {
471 lc_mc_search->error = code;
472
473 MC_PTR_FREE (lc_mc_search->error_str);
474
475 if (format != NULL)
476 {
477 va_list args;
478
479 va_start (args, format);
480 lc_mc_search->error_str = g_strdup_vprintf (format, args);
481 va_end (args);
482 }
483 }
484
485