2009-04-22 19:52:49 +04:00
|
|
|
/*
|
|
|
|
Search text engine.
|
2009-04-25 12:09:47 +04:00
|
|
|
Interface functions
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
Copyright (C) 2009 The Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009.
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "../src/global.h"
|
2009-04-25 12:09:47 +04:00
|
|
|
#include "../src/search/search.h"
|
|
|
|
#include "../src/search/internal.h"
|
2009-04-22 19:52:49 +04:00
|
|
|
#include "../src/strutil.h"
|
|
|
|
#include "../src/charsets.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2009-04-29 17:13:12 +04:00
|
|
|
static const mc_search_type_str_t mc_search__list_types[] = {
|
|
|
|
{N_("Normal"), MC_SEARCH_T_NORMAL},
|
2009-04-30 15:25:42 +04:00
|
|
|
{N_("&Regular expression"), MC_SEARCH_T_REGEX},
|
|
|
|
{N_("Hexadecimal"), MC_SEARCH_T_HEX},
|
|
|
|
{N_("Wildcard search"), MC_SEARCH_T_GLOB},
|
2009-04-29 17:13:12 +04:00
|
|
|
{NULL, -1}
|
|
|
|
};
|
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static mc_search_cond_t *
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search__cond_struct_new (mc_search_t * mc_search, const char *str,
|
|
|
|
gsize str_len, const char *charset)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
|
|
|
mc_search_cond_t *mc_search_cond;
|
|
|
|
mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
|
2009-04-25 12:09:47 +04:00
|
|
|
|
|
|
|
mc_search_cond->str = g_string_new_len (str, str_len);
|
|
|
|
mc_search_cond->len = str_len;
|
|
|
|
mc_search_cond->charset = g_strdup (charset);
|
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
switch (mc_search->search_type) {
|
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-04-30 17:06:24 +04:00
|
|
|
mc_search__cond_struct_new_init_glob (charset, mc_search, mc_search_cond);
|
|
|
|
break;
|
2009-04-25 11:15:56 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
2009-04-25 12:09:47 +04:00
|
|
|
mc_search__cond_struct_new_init_normal (charset, mc_search, mc_search_cond);
|
2009-04-25 11:15:56 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-04-25 12:09:47 +04:00
|
|
|
mc_search__cond_struct_new_init_regex (charset, mc_search, mc_search_cond);
|
2009-04-25 11:15:56 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_HEX:
|
2009-05-05 23:28:27 +04:00
|
|
|
mc_search__cond_struct_new_init_hex (charset, mc_search, mc_search_cond);
|
|
|
|
break;
|
2009-04-25 11:15:56 +04:00
|
|
|
default:
|
|
|
|
break;
|
2009-04-22 19:52:49 +04:00
|
|
|
}
|
|
|
|
return mc_search_cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static GPtrArray *
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search__conditions_new (mc_search_t * mc_search)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
|
|
|
GPtrArray *ret;
|
|
|
|
ret = g_ptr_array_new ();
|
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
if (mc_search->is_all_charsets) {
|
2009-04-23 17:16:00 +04:00
|
|
|
gsize loop1, recoded_str_len;
|
2009-04-22 19:52:49 +04:00
|
|
|
gchar *buffer;
|
2009-04-25 11:15:56 +04:00
|
|
|
for (loop1 = 0; loop1 < (gsize) n_codepages; loop1++) {
|
2009-04-30 17:58:28 +04:00
|
|
|
if (!g_ascii_strcasecmp (codepages[loop1].id, cp_source)) {
|
2009-04-22 19:52:49 +04:00
|
|
|
g_ptr_array_add (ret,
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search__cond_struct_new (mc_search, mc_search->original,
|
|
|
|
mc_search->original_len, cp_source));
|
2009-04-22 19:52:49 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer =
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search__recode_str (mc_search->original, mc_search->original_len, cp_source,
|
|
|
|
codepages[loop1].id, &recoded_str_len);
|
2009-04-22 19:52:49 +04:00
|
|
|
if (buffer == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
g_ptr_array_add (ret,
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search__cond_struct_new (mc_search, buffer,
|
|
|
|
recoded_str_len, codepages[loop1].id));
|
2009-04-22 19:52:49 +04:00
|
|
|
g_free (buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_ptr_array_add (ret,
|
2009-04-25 11:15:56 +04:00
|
|
|
(gpointer) mc_search__cond_struct_new (mc_search, mc_search->original,
|
|
|
|
mc_search->original_len,
|
|
|
|
cp_source));
|
2009-04-22 19:52:49 +04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
|
|
|
|
{
|
|
|
|
if (mc_search_cond->upper)
|
|
|
|
g_string_free (mc_search_cond->upper, TRUE);
|
|
|
|
|
|
|
|
if (mc_search_cond->lower)
|
|
|
|
g_string_free (mc_search_cond->lower, TRUE);
|
|
|
|
|
|
|
|
g_string_free (mc_search_cond->str, TRUE);
|
|
|
|
g_free (mc_search_cond->charset);
|
2009-04-25 11:15:56 +04:00
|
|
|
|
|
|
|
#if GLIB_CHECK_VERSION (2, 14, 0)
|
2009-05-05 17:19:32 +04:00
|
|
|
if (mc_search_cond->regex_handle)
|
|
|
|
g_regex_unref (mc_search_cond->regex_handle);
|
|
|
|
#else /* GLIB_CHECK_VERSION (2, 14, 0) */
|
|
|
|
#if HAVE_LIBPCRE
|
|
|
|
if (mc_search_cond->regex_handle)
|
|
|
|
free (mc_search_cond->regex_handle);
|
|
|
|
#else /* HAVE_LIBPCRE */
|
|
|
|
if (mc_search_cond->regex_handle)
|
|
|
|
regfree (mc_search_cond->regex_handle);
|
|
|
|
#endif /* HAVE_LIBPCRE */
|
|
|
|
#endif /* GLIB_CHECK_VERSION (2, 14, 0) */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
g_free (mc_search_cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
|
|
|
mc_search__conditions_free (GPtrArray * array)
|
|
|
|
{
|
2009-04-23 17:16:00 +04:00
|
|
|
gsize loop1;
|
2009-04-22 19:52:49 +04:00
|
|
|
mc_search_cond_t *mc_search;
|
|
|
|
|
|
|
|
for (loop1 = 0; loop1 < array->len; loop1++) {
|
|
|
|
mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1);
|
|
|
|
mc_search__cond_struct_free (mc_search);
|
|
|
|
}
|
|
|
|
g_ptr_array_free (array, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
|
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
|
|
|
|
mc_search_t *
|
2009-04-23 17:16:00 +04:00
|
|
|
mc_search_new (const gchar * original, gsize str_len)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
|
|
|
mc_search_t *mc_search;
|
|
|
|
if (!original)
|
|
|
|
return NULL;
|
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
if ((gssize) str_len == -1) {
|
2009-04-22 19:52:49 +04:00
|
|
|
str_len = strlen (original);
|
|
|
|
if (str_len == 0)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mc_search = g_malloc0 (sizeof (mc_search_t));
|
|
|
|
mc_search->original = g_strndup (original, str_len);
|
|
|
|
mc_search->original_len = str_len;
|
|
|
|
return mc_search;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
void
|
|
|
|
mc_search_free (mc_search_t * mc_search)
|
|
|
|
{
|
|
|
|
if (!mc_search)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (mc_search->original);
|
|
|
|
|
|
|
|
if (mc_search->error_str)
|
|
|
|
g_free (mc_search->error_str);
|
|
|
|
|
2009-04-23 15:30:14 +04:00
|
|
|
if (mc_search->conditions)
|
2009-04-23 17:26:14 +04:00
|
|
|
mc_search__conditions_free (mc_search->conditions);
|
2009-04-23 15:30:14 +04:00
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
#if GLIB_CHECK_VERSION (2, 14, 0)
|
|
|
|
if (mc_search->regex_match_info)
|
|
|
|
g_match_info_free (mc_search->regex_match_info);
|
2009-05-05 17:19:32 +04:00
|
|
|
#else /* GLIB_CHECK_VERSION (2, 14, 0) */
|
|
|
|
#if HAVE_LIBPCRE
|
|
|
|
if (mc_search->regex_match_info)
|
|
|
|
free (mc_search->regex_match_info);
|
|
|
|
#else /* HAVE_LIBPCRE */
|
|
|
|
if (mc_search->regex_match_info)
|
|
|
|
g_free (mc_search->regex_match_info);
|
|
|
|
#endif /* HAVE_LIBPCRE */
|
|
|
|
#endif /* GLIB_CHECK_VERSION (2, 14, 0) */
|
|
|
|
|
2009-04-30 11:32:45 +04:00
|
|
|
if (mc_search->regex_buffer != NULL)
|
|
|
|
g_string_free (mc_search->regex_buffer, TRUE);
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
g_free (mc_search);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
gboolean
|
2009-04-25 11:15:56 +04:00
|
|
|
mc_search_run (mc_search_t * mc_search, const void *user_data,
|
|
|
|
gsize start_search, gsize end_search, gsize * found_len)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
2009-04-23 17:26:14 +04:00
|
|
|
gboolean ret = FALSE;
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
if (!mc_search)
|
|
|
|
return FALSE;
|
2009-04-25 11:15:56 +04:00
|
|
|
if (!mc_search_is_type_avail (mc_search->search_type)) {
|
|
|
|
mc_search->error = MC_SEARCH_E_INPUT;
|
|
|
|
mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#if GLIB_CHECK_VERSION (2, 14, 0)
|
|
|
|
if (mc_search->regex_match_info) {
|
|
|
|
g_match_info_free (mc_search->regex_match_info);
|
|
|
|
mc_search->regex_match_info = NULL;
|
|
|
|
}
|
2009-05-05 17:19:32 +04:00
|
|
|
#endif /* GLIB_CHECK_VERSION (2, 14, 0) */
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
mc_search->error = MC_SEARCH_E_OK;
|
|
|
|
if (mc_search->error_str) {
|
|
|
|
g_free (mc_search->error_str);
|
|
|
|
mc_search->error_str = NULL;
|
|
|
|
}
|
|
|
|
|
2009-04-25 11:15:56 +04:00
|
|
|
if (!mc_search->conditions) {
|
|
|
|
mc_search->conditions = mc_search__conditions_new (mc_search);
|
|
|
|
|
|
|
|
if (mc_search->error != MC_SEARCH_E_OK)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
switch (mc_search->search_type) {
|
|
|
|
case MC_SEARCH_T_NORMAL:
|
2009-04-23 17:16:00 +04:00
|
|
|
ret = mc_search__run_normal (mc_search, user_data, start_search, end_search, found_len);
|
2009-04-22 19:52:49 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-04-25 11:15:56 +04:00
|
|
|
ret = mc_search__run_regex (mc_search, user_data, start_search, end_search, found_len);
|
|
|
|
break;
|
2009-04-22 19:52:49 +04:00
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-04-30 17:06:24 +04:00
|
|
|
ret = mc_search__run_glob (mc_search, user_data, start_search, end_search, found_len);
|
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_HEX:
|
2009-05-05 23:28:27 +04:00
|
|
|
ret = mc_search__run_hex (mc_search, user_data, start_search, end_search, found_len);
|
|
|
|
break;
|
2009-04-22 19:52:49 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
mc_search_is_type_avail (mc_search_type_t search_type)
|
|
|
|
{
|
|
|
|
switch (search_type) {
|
2009-04-30 17:06:24 +04:00
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-04-25 11:15:56 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-05-05 23:28:27 +04:00
|
|
|
case MC_SEARCH_T_HEX:
|
2009-04-25 11:15:56 +04:00
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-29 17:13:12 +04:00
|
|
|
|
|
|
|
mc_search_type_str_t *
|
|
|
|
mc_search_types_list_get (void)
|
|
|
|
{
|
|
|
|
return mc_search__list_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-29 19:45:21 +04:00
|
|
|
|
|
|
|
GString *
|
2009-04-30 11:32:45 +04:00
|
|
|
mc_search_prepare_replace_str (mc_search_t * mc_search, GString * replace_str)
|
2009-04-29 19:45:21 +04:00
|
|
|
{
|
|
|
|
GString *ret;
|
|
|
|
|
|
|
|
if (mc_search == NULL)
|
2009-04-30 11:32:45 +04:00
|
|
|
return g_string_new_len (replace_str->str, replace_str->len);
|
2009-04-29 19:45:21 +04:00
|
|
|
|
|
|
|
switch (mc_search->search_type) {
|
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-04-30 11:32:45 +04:00
|
|
|
ret = mc_search_regex_prepare_replace_str (mc_search, replace_str);
|
|
|
|
break;
|
2009-04-29 19:45:21 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
|
|
|
case MC_SEARCH_T_HEX:
|
|
|
|
case MC_SEARCH_T_GLOB:
|
|
|
|
default:
|
2009-04-30 11:32:45 +04:00
|
|
|
ret = g_string_new_len (replace_str->str, replace_str->len);
|
2009-04-29 19:45:21 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-30 15:21:39 +04:00
|
|
|
|
|
|
|
gboolean
|
2009-04-30 17:06:24 +04:00
|
|
|
mc_search_is_fixed_search_str (mc_search_t * mc_search)
|
2009-04-30 15:21:39 +04:00
|
|
|
{
|
|
|
|
if (mc_search == NULL)
|
|
|
|
return FALSE;
|
|
|
|
switch (mc_search->search_type) {
|
|
|
|
case MC_SEARCH_T_REGEX:
|
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-04-30 17:06:24 +04:00
|
|
|
return FALSE;
|
2009-04-30 15:21:39 +04:00
|
|
|
break;
|
|
|
|
default:
|
2009-04-30 17:06:24 +04:00
|
|
|
return TRUE;
|
2009-04-30 15:21:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|