Indentation.
[midnight-commander.git] / lib / search / search.c
blob4f30f26ab9e2f7107a8efffb2bc4efc82717a83b
1 /*
2 Search text engine.
3 Interface functions
5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #ifdef HAVE_CHARSET
36 #include "lib/charsets.h"
37 #endif
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static const mc_search_type_str_t mc_search__list_types[] = {
50 {N_("No&rmal"), MC_SEARCH_T_NORMAL},
51 {N_("Re&gular expression"), MC_SEARCH_T_REGEX},
52 {N_("He&xadecimal"), MC_SEARCH_T_HEX},
53 {N_("Wil&dcard search"), MC_SEARCH_T_GLOB},
54 {NULL, -1}
57 /*** file scope functions ************************************************************************/
59 static mc_search_cond_t *
60 mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str,
61 gsize str_len, const char *charset)
63 mc_search_cond_t *mc_search_cond;
64 mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
66 mc_search_cond->str = g_string_new_len (str, str_len);
67 mc_search_cond->charset = g_strdup (charset);
69 switch (lc_mc_search->search_type)
71 case MC_SEARCH_T_GLOB:
72 mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
73 break;
74 case MC_SEARCH_T_NORMAL:
75 mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
76 break;
77 case MC_SEARCH_T_REGEX:
78 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
79 break;
80 case MC_SEARCH_T_HEX:
81 mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
82 break;
83 default:
84 break;
86 return mc_search_cond;
89 /* --------------------------------------------------------------------------------------------- */
91 static void
92 mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
94 if (mc_search_cond->upper)
95 g_string_free (mc_search_cond->upper, TRUE);
97 if (mc_search_cond->lower)
98 g_string_free (mc_search_cond->lower, TRUE);
100 g_string_free (mc_search_cond->str, TRUE);
101 g_free (mc_search_cond->charset);
103 #ifdef SEARCH_TYPE_GLIB
104 if (mc_search_cond->regex_handle)
105 g_regex_unref (mc_search_cond->regex_handle);
106 #else /* SEARCH_TYPE_GLIB */
107 g_free (mc_search_cond->regex_handle);
108 #endif /* SEARCH_TYPE_GLIB */
110 g_free (mc_search_cond);
113 /* --------------------------------------------------------------------------------------------- */
115 static void
116 mc_search__conditions_free (GPtrArray * array)
118 gsize loop1;
119 mc_search_cond_t *lc_mc_search;
121 for (loop1 = 0; loop1 < array->len; loop1++)
123 lc_mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1);
124 mc_search__cond_struct_free (lc_mc_search);
126 g_ptr_array_free (array, TRUE);
129 /* --------------------------------------------------------------------------------------------- */
131 /*** public functions ****************************************************************************/
133 mc_search_t *
134 mc_search_new (const gchar * original, gsize str_len)
136 mc_search_t *lc_mc_search;
137 if (!original)
138 return NULL;
140 if ((gssize) str_len == -1)
142 str_len = strlen (original);
143 if (str_len == 0)
144 return NULL;
147 lc_mc_search = g_malloc0 (sizeof (mc_search_t));
148 lc_mc_search->original = g_strndup (original, str_len);
149 lc_mc_search->original_len = str_len;
150 return lc_mc_search;
153 /* --------------------------------------------------------------------------------------------- */
155 void
156 mc_search_free (mc_search_t * lc_mc_search)
158 if (lc_mc_search == NULL)
159 return;
161 g_free (lc_mc_search->original);
162 g_free (lc_mc_search->error_str);
164 if (lc_mc_search->conditions)
165 mc_search__conditions_free (lc_mc_search->conditions);
167 #ifdef SEARCH_TYPE_GLIB
168 if (lc_mc_search->regex_match_info)
169 g_match_info_free (lc_mc_search->regex_match_info);
170 #else /* SEARCH_TYPE_GLIB */
171 g_free (lc_mc_search->regex_match_info);
172 #endif /* SEARCH_TYPE_GLIB */
174 if (lc_mc_search->regex_buffer != NULL)
175 g_string_free (lc_mc_search->regex_buffer, TRUE);
177 g_free (lc_mc_search);
180 /* --------------------------------------------------------------------------------------------- */
182 gboolean
183 mc_search_prepare (mc_search_t * lc_mc_search)
185 GPtrArray *ret;
186 ret = g_ptr_array_new ();
187 #ifdef HAVE_CHARSET
188 if (lc_mc_search->is_all_charsets)
190 gsize loop1, recoded_str_len;
191 gchar *buffer;
192 for (loop1 = 0; loop1 < codepages->len; loop1++)
194 const char *id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
195 if (!g_ascii_strcasecmp (id, cp_source))
197 g_ptr_array_add (ret,
198 mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
199 lc_mc_search->original_len,
200 cp_source));
201 continue;
204 buffer =
205 mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len,
206 cp_source, id, &recoded_str_len);
208 g_ptr_array_add (ret,
209 mc_search__cond_struct_new (lc_mc_search, buffer,
210 recoded_str_len, id));
211 g_free (buffer);
214 else
216 g_ptr_array_add (ret,
217 (gpointer) mc_search__cond_struct_new (lc_mc_search,
218 lc_mc_search->original,
219 lc_mc_search->original_len,
220 cp_source));
222 #else
223 g_ptr_array_add (ret,
224 (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
225 lc_mc_search->original_len,
226 str_detect_termencoding ()));
227 #endif
228 lc_mc_search->conditions = ret;
230 return (lc_mc_search->error == MC_SEARCH_E_OK);
233 /* --------------------------------------------------------------------------------------------- */
235 gboolean
236 mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
237 gsize start_search, gsize end_search, gsize * found_len)
239 gboolean ret = FALSE;
241 if (lc_mc_search == NULL || user_data == NULL)
242 return FALSE;
243 if (!mc_search_is_type_avail (lc_mc_search->search_type))
245 lc_mc_search->error = MC_SEARCH_E_INPUT;
246 lc_mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
247 return FALSE;
249 #ifdef SEARCH_TYPE_GLIB
250 if (lc_mc_search->regex_match_info)
252 g_match_info_free (lc_mc_search->regex_match_info);
253 lc_mc_search->regex_match_info = NULL;
255 #endif /* SEARCH_TYPE_GLIB */
257 lc_mc_search->error = MC_SEARCH_E_OK;
258 g_free (lc_mc_search->error_str);
259 lc_mc_search->error_str = NULL;
261 if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search))
262 return FALSE;
264 switch (lc_mc_search->search_type)
266 case MC_SEARCH_T_NORMAL:
267 ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
268 break;
269 case MC_SEARCH_T_REGEX:
270 ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
271 break;
272 case MC_SEARCH_T_GLOB:
273 ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
274 break;
275 case MC_SEARCH_T_HEX:
276 ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
277 break;
278 default:
279 break;
281 return ret;
284 /* --------------------------------------------------------------------------------------------- */
286 gboolean
287 mc_search_is_type_avail (mc_search_type_t search_type)
289 switch (search_type)
291 case MC_SEARCH_T_GLOB:
292 case MC_SEARCH_T_NORMAL:
293 case MC_SEARCH_T_REGEX:
294 case MC_SEARCH_T_HEX:
295 return TRUE;
296 default:
297 break;
299 return FALSE;
302 /* --------------------------------------------------------------------------------------------- */
304 const mc_search_type_str_t *
305 mc_search_types_list_get (size_t * num)
307 /* don't count last NULL item */
308 if (num != NULL)
309 *num = sizeof (mc_search__list_types) / sizeof (mc_search__list_types[0]) - 1;
311 return mc_search__list_types;
314 /* --------------------------------------------------------------------------------------------- */
316 GString *
317 mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
319 GString *ret;
321 if (lc_mc_search == NULL)
322 return g_string_new_len (replace_str->str, replace_str->len);
324 if (replace_str == NULL || replace_str->str == NULL || replace_str->len == 0)
325 return g_string_new ("");
327 switch (lc_mc_search->search_type)
329 case MC_SEARCH_T_REGEX:
330 ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
331 break;
332 case MC_SEARCH_T_GLOB:
333 ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
334 break;
335 case MC_SEARCH_T_NORMAL:
336 ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
337 break;
338 case MC_SEARCH_T_HEX:
339 ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
340 break;
341 default:
342 ret = g_string_new_len (replace_str->str, replace_str->len);
343 break;
345 return ret;
348 /* --------------------------------------------------------------------------------------------- */
350 char *
351 mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, char *replace_str)
353 GString *ret;
354 GString *replace_str2;
356 replace_str2 = g_string_new (replace_str);
357 ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
358 g_string_free (replace_str2, TRUE);
359 return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
362 /* --------------------------------------------------------------------------------------------- */
364 gboolean
365 mc_search_is_fixed_search_str (mc_search_t * lc_mc_search)
367 if (lc_mc_search == NULL)
368 return FALSE;
369 switch (lc_mc_search->search_type)
371 case MC_SEARCH_T_REGEX:
372 case MC_SEARCH_T_GLOB:
373 return FALSE;
374 default:
375 return TRUE;
379 /* --------------------------------------------------------------------------------------------- */
381 gboolean
382 mc_search (const gchar * pattern, const gchar * str, mc_search_type_t type)
384 gboolean ret;
385 mc_search_t *search;
387 if (str == NULL)
388 return FALSE;
390 search = mc_search_new (pattern, -1);
391 if (search == NULL)
392 return FALSE;
394 search->search_type = type;
395 search->is_case_sensitive = TRUE;
397 if (type == MC_SEARCH_T_GLOB)
398 search->is_entire_line = TRUE;
400 ret = mc_search_run (search, str, 0, strlen (str), NULL);
401 mc_search_free (search);
402 return ret;
405 /* --------------------------------------------------------------------------------------------- */
408 mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
410 if (!lc_mc_search)
411 return 0;
412 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
413 return 0;
414 #ifdef SEARCH_TYPE_GLIB
416 gint start_pos;
417 gint end_pos;
418 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
419 return (int) start_pos;
421 #else /* SEARCH_TYPE_GLIB */
422 return lc_mc_search->iovector[lc_index * 2];
423 #endif /* SEARCH_TYPE_GLIB */
426 /* --------------------------------------------------------------------------------------------- */
429 mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
431 if (!lc_mc_search)
432 return 0;
433 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
434 return 0;
435 #ifdef SEARCH_TYPE_GLIB
437 gint start_pos;
438 gint end_pos;
439 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
440 return (int) end_pos;
442 #else /* SEARCH_TYPE_GLIB */
443 return lc_mc_search->iovector[lc_index * 2 + 1];
444 #endif /* SEARCH_TYPE_GLIB */
447 /* --------------------------------------------------------------------------------------------- */