Remove extra check before mc_search free() call.
[pantumic.git] / lib / search / search.c
blob895df2184e08805fa43f26ed9c868dbba39a6ac7
1 /*
2 Search text engine.
3 Interface functions
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
37 #include "src/charsets.h"
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_("Normal"), MC_SEARCH_T_NORMAL},
51 {N_("&Regular expression"), MC_SEARCH_T_REGEX},
52 {N_("Hexadecimal"), MC_SEARCH_T_HEX},
53 {N_("Wildcard 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) {
70 case MC_SEARCH_T_GLOB:
71 mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
72 break;
73 case MC_SEARCH_T_NORMAL:
74 mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
75 break;
76 case MC_SEARCH_T_REGEX:
77 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
78 break;
79 case MC_SEARCH_T_HEX:
80 mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
81 break;
82 default:
83 break;
85 return mc_search_cond;
88 /* --------------------------------------------------------------------------------------------- */
90 static void
91 mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
93 if (mc_search_cond->upper)
94 g_string_free (mc_search_cond->upper, TRUE);
96 if (mc_search_cond->lower)
97 g_string_free (mc_search_cond->lower, TRUE);
99 g_string_free (mc_search_cond->str, TRUE);
100 g_free (mc_search_cond->charset);
102 #ifdef SEARCH_TYPE_GLIB
103 if (mc_search_cond->regex_handle)
104 g_regex_unref (mc_search_cond->regex_handle);
105 #else /* SEARCH_TYPE_GLIB */
106 g_free (mc_search_cond->regex_handle);
107 #endif /* SEARCH_TYPE_GLIB */
109 g_free (mc_search_cond);
112 /* --------------------------------------------------------------------------------------------- */
114 static void
115 mc_search__conditions_free (GPtrArray * array)
117 gsize loop1;
118 mc_search_cond_t *lc_mc_search;
120 for (loop1 = 0; loop1 < array->len; loop1++) {
121 lc_mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1);
122 mc_search__cond_struct_free (lc_mc_search);
124 g_ptr_array_free (array, TRUE);
127 /* --------------------------------------------------------------------------------------------- */
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) {
141 str_len = strlen (original);
142 if (str_len == 0)
143 return NULL;
146 lc_mc_search = g_malloc0 (sizeof (mc_search_t));
147 lc_mc_search->original = g_strndup (original, str_len);
148 lc_mc_search->original_len = str_len;
149 return lc_mc_search;
152 /* --------------------------------------------------------------------------------------------- */
154 void
155 mc_search_free (mc_search_t * lc_mc_search)
157 if (lc_mc_search == NULL)
158 return;
160 g_free (lc_mc_search->original);
161 g_free (lc_mc_search->error_str);
163 if (lc_mc_search->conditions)
164 mc_search__conditions_free (lc_mc_search->conditions);
166 #ifdef SEARCH_TYPE_GLIB
167 if (lc_mc_search->regex_match_info)
168 g_match_info_free (lc_mc_search->regex_match_info);
169 #else /* SEARCH_TYPE_GLIB */
170 g_free (lc_mc_search->regex_match_info);
171 #endif /* SEARCH_TYPE_GLIB */
173 if (lc_mc_search->regex_buffer != NULL)
174 g_string_free (lc_mc_search->regex_buffer, TRUE);
176 g_free (lc_mc_search);
179 /* --------------------------------------------------------------------------------------------- */
181 gboolean
182 mc_search_prepare (mc_search_t * lc_mc_search)
184 GPtrArray *ret;
185 ret = g_ptr_array_new ();
186 #ifdef HAVE_CHARSET
187 if (lc_mc_search->is_all_charsets) {
188 gsize loop1, recoded_str_len;
189 gchar *buffer;
190 for (loop1 = 0; loop1 < codepages->len; loop1++) {
191 const char *id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
192 if (!g_ascii_strcasecmp (id, cp_source)) {
193 g_ptr_array_add (ret,
194 mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
195 lc_mc_search->original_len, cp_source));
196 continue;
199 buffer =
200 mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len, cp_source,
201 id, &recoded_str_len);
203 g_ptr_array_add (ret,
204 mc_search__cond_struct_new (lc_mc_search, buffer,
205 recoded_str_len, id));
206 g_free (buffer);
208 } else {
209 g_ptr_array_add (ret,
210 (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
211 lc_mc_search->original_len,
212 cp_source));
214 #else
215 g_ptr_array_add (ret,
216 (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
217 lc_mc_search->original_len,
218 str_detect_termencoding ()));
219 #endif
220 lc_mc_search->conditions = ret;
222 return (lc_mc_search->error == MC_SEARCH_E_OK);
225 /* --------------------------------------------------------------------------------------------- */
227 gboolean
228 mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
229 gsize start_search, gsize end_search, gsize * found_len)
231 gboolean ret = FALSE;
233 if (!lc_mc_search)
234 return FALSE;
235 if (!mc_search_is_type_avail (lc_mc_search->search_type)) {
236 lc_mc_search->error = MC_SEARCH_E_INPUT;
237 lc_mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
238 return FALSE;
240 #ifdef SEARCH_TYPE_GLIB
241 if (lc_mc_search->regex_match_info) {
242 g_match_info_free (lc_mc_search->regex_match_info);
243 lc_mc_search->regex_match_info = NULL;
245 #endif /* SEARCH_TYPE_GLIB */
247 lc_mc_search->error = MC_SEARCH_E_OK;
248 g_free (lc_mc_search->error_str);
249 lc_mc_search->error_str = NULL;
251 if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search))
252 return FALSE;
255 switch (lc_mc_search->search_type) {
256 case MC_SEARCH_T_NORMAL:
257 ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
258 break;
259 case MC_SEARCH_T_REGEX:
260 ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
261 break;
262 case MC_SEARCH_T_GLOB:
263 ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
264 break;
265 case MC_SEARCH_T_HEX:
266 ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
267 break;
268 default:
269 break;
271 return ret;
274 /* --------------------------------------------------------------------------------------------- */
276 gboolean
277 mc_search_is_type_avail (mc_search_type_t search_type)
279 switch (search_type) {
280 case MC_SEARCH_T_GLOB:
281 case MC_SEARCH_T_NORMAL:
282 case MC_SEARCH_T_REGEX:
283 case MC_SEARCH_T_HEX:
284 return TRUE;
285 default:
286 break;
288 return FALSE;
291 /* --------------------------------------------------------------------------------------------- */
293 const mc_search_type_str_t *
294 mc_search_types_list_get (size_t *num)
296 /* don't count last NULL item */
297 if (num != NULL)
298 *num = sizeof (mc_search__list_types) / sizeof (mc_search__list_types[0]) - 1;
300 return mc_search__list_types;
303 /* --------------------------------------------------------------------------------------------- */
305 GString *
306 mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
308 GString *ret;
310 if (lc_mc_search == NULL)
311 return g_string_new_len (replace_str->str, replace_str->len);
313 if (replace_str == NULL || replace_str->str == NULL || replace_str->len == 0)
314 return g_string_new ("");
316 switch (lc_mc_search->search_type) {
317 case MC_SEARCH_T_REGEX:
318 ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
319 break;
320 case MC_SEARCH_T_GLOB:
321 ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
322 break;
323 case MC_SEARCH_T_NORMAL:
324 ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
325 break;
326 case MC_SEARCH_T_HEX:
327 ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
328 break;
329 default:
330 ret = g_string_new_len (replace_str->str, replace_str->len);
331 break;
333 return ret;
336 /* --------------------------------------------------------------------------------------------- */
338 char *
339 mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, char *replace_str)
341 GString *ret;
342 GString *replace_str2;
344 replace_str2 = g_string_new (replace_str);
345 ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
346 g_string_free (replace_str2, TRUE);
347 return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
350 /* --------------------------------------------------------------------------------------------- */
352 gboolean
353 mc_search_is_fixed_search_str (mc_search_t * lc_mc_search)
355 if (lc_mc_search == NULL)
356 return FALSE;
357 switch (lc_mc_search->search_type) {
358 case MC_SEARCH_T_REGEX:
359 case MC_SEARCH_T_GLOB:
360 return FALSE;
361 default:
362 return TRUE;
366 /* --------------------------------------------------------------------------------------------- */
368 gboolean
369 mc_search (const gchar * pattern, const gchar * str, mc_search_type_t type)
371 gboolean ret;
372 mc_search_t *search = mc_search_new (pattern, -1);
373 if (search == NULL)
374 return FALSE;
375 search->search_type = type;
376 search->is_case_sensitive = TRUE;
378 if (type == MC_SEARCH_T_GLOB)
379 search->is_entire_line = TRUE;
381 ret = mc_search_run (search, str, 0, strlen (str), NULL);
382 mc_search_free (search);
383 return ret;
386 /* --------------------------------------------------------------------------------------------- */
389 mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
391 if (!lc_mc_search)
392 return 0;
393 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
394 return 0;
395 #ifdef SEARCH_TYPE_GLIB
397 gint start_pos;
398 gint end_pos;
399 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
400 return (int) start_pos;
402 #else /* SEARCH_TYPE_GLIB */
403 return lc_mc_search->iovector[lc_index * 2];
404 #endif /* SEARCH_TYPE_GLIB */
408 /* --------------------------------------------------------------------------------------------- */
411 mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
413 if (!lc_mc_search)
414 return 0;
415 if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
416 return 0;
417 #ifdef SEARCH_TYPE_GLIB
419 gint start_pos;
420 gint end_pos;
421 g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
422 return (int) end_pos;
424 #else /* SEARCH_TYPE_GLIB */
425 return lc_mc_search->iovector[lc_index * 2 + 1];
426 #endif /* SEARCH_TYPE_GLIB */
430 /* --------------------------------------------------------------------------------------------- */