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
|
|
|
|
2019-01-01 08:48:53 +03:00
|
|
|
Copyright (C) 2009-2019
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
Written by:
|
2013-06-25 20:59:40 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009
|
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2013
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2009-04-22 19:52:49 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-04-22 19:52:49 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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.
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-22 19:52:49 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2016-04-27 10:26:38 +03:00
|
|
|
#include <stdarg.h>
|
2009-09-22 13:35:09 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2009-04-22 19:52:49 +04:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-21 15:17:26 +03:00
|
|
|
#include "lib/strutil.h"
|
2010-01-21 16:06:15 +03:00
|
|
|
#include "lib/search.h"
|
2014-08-05 11:18:30 +04:00
|
|
|
#include "lib/util.h"
|
2012-04-30 15:52:39 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2010-11-18 11:31:09 +03:00
|
|
|
#include "lib/charsets.h"
|
2012-04-30 15:52:39 +04:00
|
|
|
#endif
|
2010-01-21 15:17:26 +03:00
|
|
|
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "internal.h"
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
/*** 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[] = {
|
2012-07-08 16:27:48 +04:00
|
|
|
{N_("No&rmal"), MC_SEARCH_T_NORMAL},
|
2011-12-27 22:03:43 +04:00
|
|
|
{N_("Re&gular expression"), MC_SEARCH_T_REGEX},
|
|
|
|
{N_("He&xadecimal"), MC_SEARCH_T_HEX},
|
|
|
|
{N_("Wil&dcard search"), MC_SEARCH_T_GLOB},
|
2015-11-20 23:54:52 +03:00
|
|
|
{NULL, MC_SEARCH_T_INVALID}
|
2009-04-29 17:13:12 +04:00
|
|
|
};
|
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
|
|
|
|
static mc_search_cond_t *
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str,
|
2009-04-25 11:15:56 +04:00
|
|
|
gsize str_len, const char *charset)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
|
|
|
mc_search_cond_t *mc_search_cond;
|
2009-04-25 12:09:47 +04:00
|
|
|
|
2013-06-25 21:09:49 +04:00
|
|
|
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->charset = g_strdup (charset);
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
switch (lc_mc_search->search_type)
|
|
|
|
{
|
2009-04-25 11:15:56 +04:00
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
|
2009-04-30 17:06:24 +04:00
|
|
|
break;
|
2009-04-25 11:15:56 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
|
2009-04-25 11:15:56 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
|
2009-04-25 11:15:56 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_HEX:
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
|
2009-05-05 23:28:27 +04:00
|
|
|
break;
|
2009-04-25 11:15:56 +04:00
|
|
|
default:
|
|
|
|
break;
|
2009-04-22 19:52:49 +04:00
|
|
|
}
|
|
|
|
return mc_search_cond;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-07-07 12:11:29 +04:00
|
|
|
#ifdef SEARCH_TYPE_GLIB
|
2009-05-05 17:19:32 +04:00
|
|
|
if (mc_search_cond->regex_handle)
|
|
|
|
g_regex_unref (mc_search_cond->regex_handle);
|
2009-07-07 12:11:29 +04:00
|
|
|
#else /* SEARCH_TYPE_GLIB */
|
2009-06-22 21:46:34 +04:00
|
|
|
g_free (mc_search_cond->regex_handle);
|
2009-07-07 12:11:29 +04:00
|
|
|
#endif /* SEARCH_TYPE_GLIB */
|
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)
|
|
|
|
{
|
2013-08-09 11:39:11 +04:00
|
|
|
g_ptr_array_foreach (array, (GFunc) mc_search__cond_struct_free, NULL);
|
2009-04-22 19:52:49 +04:00
|
|
|
g_ptr_array_free (array, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
2015-11-15 12:43:03 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/* Init search descriptor.
|
|
|
|
*
|
|
|
|
* @param original pattern to search
|
|
|
|
* @param original_charset charset of #original. If NULL then cp_display will be used
|
|
|
|
*
|
|
|
|
* @return new mc_search_t object. Use #mc_search_free() to free it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mc_search_t *
|
|
|
|
mc_search_new (const gchar * original, const gchar * original_charset)
|
|
|
|
{
|
|
|
|
if (original == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mc_search_new_len (original, strlen (original), original_charset);
|
|
|
|
}
|
|
|
|
|
2013-09-06 12:59:54 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/* Init search descriptor.
|
|
|
|
*
|
|
|
|
* @param original pattern to search
|
|
|
|
* @param original_len length of #original or -1 if #original is NULL-terminated
|
|
|
|
* @param original_charset charset of #original. If NULL then cp_display will be used
|
|
|
|
*
|
|
|
|
* @return new mc_search_t object. Use #mc_search_free() to free it.
|
|
|
|
*/
|
2009-04-22 19:52:49 +04:00
|
|
|
|
|
|
|
mc_search_t *
|
2015-11-15 12:43:03 +03:00
|
|
|
mc_search_new_len (const gchar * original, gsize original_len, const gchar * original_charset)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_t *lc_mc_search;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2015-11-15 12:43:03 +03:00
|
|
|
if (original == NULL || original_len == 0)
|
2009-04-22 19:52:49 +04:00
|
|
|
return NULL;
|
|
|
|
|
2013-09-06 12:59:54 +04:00
|
|
|
lc_mc_search = g_new0 (mc_search_t, 1);
|
|
|
|
lc_mc_search->original = g_strndup (original, original_len);
|
|
|
|
lc_mc_search->original_len = original_len;
|
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
lc_mc_search->original_charset =
|
|
|
|
g_strdup (original_charset != NULL
|
|
|
|
&& *original_charset != '\0' ? original_charset : cp_display);
|
|
|
|
#else
|
|
|
|
(void) original_charset;
|
|
|
|
#endif
|
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
return lc_mc_search;
|
2009-04-22 19:52:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-04-22 19:52:49 +04:00
|
|
|
void
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_free (mc_search_t * lc_mc_search)
|
2009-04-22 19:52:49 +04:00
|
|
|
{
|
2010-11-04 16:35:47 +03:00
|
|
|
if (lc_mc_search == NULL)
|
2009-04-22 19:52:49 +04:00
|
|
|
return;
|
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
g_free (lc_mc_search->original);
|
2013-09-06 12:59:54 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
|
|
|
g_free (lc_mc_search->original_charset);
|
|
|
|
#endif
|
2009-10-30 04:12:04 +03:00
|
|
|
g_free (lc_mc_search->error_str);
|
2009-04-22 19:52:49 +04:00
|
|
|
|
2013-06-25 21:09:49 +04:00
|
|
|
if (lc_mc_search->conditions != NULL)
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__conditions_free (lc_mc_search->conditions);
|
2009-04-23 15:30:14 +04:00
|
|
|
|
2009-07-07 12:11:29 +04:00
|
|
|
#ifdef SEARCH_TYPE_GLIB
|
2013-06-25 21:09:49 +04:00
|
|
|
if (lc_mc_search->regex_match_info != NULL)
|
2009-10-30 04:12:04 +03:00
|
|
|
g_match_info_free (lc_mc_search->regex_match_info);
|
2009-07-07 12:11:29 +04:00
|
|
|
#else /* SEARCH_TYPE_GLIB */
|
2009-10-30 04:12:04 +03:00
|
|
|
g_free (lc_mc_search->regex_match_info);
|
2009-07-07 12:11:29 +04:00
|
|
|
#endif /* SEARCH_TYPE_GLIB */
|
2009-05-05 17:19:32 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
if (lc_mc_search->regex_buffer != NULL)
|
|
|
|
g_string_free (lc_mc_search->regex_buffer, TRUE);
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
g_free (lc_mc_search);
|
2009-04-22 19:52:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2009-06-22 21:46:34 +04:00
|
|
|
gboolean
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_prepare (mc_search_t * lc_mc_search)
|
2009-06-22 21:46:34 +04:00
|
|
|
{
|
|
|
|
GPtrArray *ret;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2009-06-22 21:46:34 +04:00
|
|
|
ret = g_ptr_array_new ();
|
|
|
|
#ifdef HAVE_CHARSET
|
2010-11-08 13:21:45 +03:00
|
|
|
if (lc_mc_search->is_all_charsets)
|
|
|
|
{
|
2013-09-06 12:59:54 +04:00
|
|
|
gsize loop1;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
for (loop1 = 0; loop1 < codepages->len; loop1++)
|
|
|
|
{
|
2013-09-06 12:59:54 +04:00
|
|
|
const char *id;
|
|
|
|
gsize recoded_str_len;
|
|
|
|
gchar *buffer;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2013-09-06 12:59:54 +04:00
|
|
|
id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
|
|
|
|
if (g_ascii_strcasecmp (id, lc_mc_search->original_charset) == 0)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2009-06-22 21:46:34 +04:00
|
|
|
g_ptr_array_add (ret,
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
|
2010-11-08 13:21:45 +03:00
|
|
|
lc_mc_search->original_len,
|
2013-09-06 12:59:54 +04:00
|
|
|
lc_mc_search->original_charset));
|
2009-06-22 21:46:34 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer =
|
2010-11-08 13:21:45 +03:00
|
|
|
mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len,
|
2013-09-06 12:59:54 +04:00
|
|
|
lc_mc_search->original_charset, id, &recoded_str_len);
|
2009-06-22 21:46:34 +04:00
|
|
|
|
|
|
|
g_ptr_array_add (ret,
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search__cond_struct_new (lc_mc_search, buffer,
|
2010-09-16 13:39:29 +04:00
|
|
|
recoded_str_len, id));
|
2009-06-22 21:46:34 +04:00
|
|
|
g_free (buffer);
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-22 21:46:34 +04:00
|
|
|
g_ptr_array_add (ret,
|
2013-09-06 12:59:54 +04:00
|
|
|
mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
|
|
|
|
lc_mc_search->original_len,
|
|
|
|
lc_mc_search->original_charset));
|
2009-06-22 21:46:34 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
g_ptr_array_add (ret,
|
2013-06-25 21:09:49 +04:00
|
|
|
mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
|
|
|
|
lc_mc_search->original_len,
|
|
|
|
str_detect_termencoding ()));
|
2009-06-22 21:46:34 +04:00
|
|
|
#endif
|
2009-10-30 04:12:04 +03:00
|
|
|
lc_mc_search->conditions = ret;
|
2009-06-22 21:46:34 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
return (lc_mc_search->error == MC_SEARCH_E_OK);
|
2009-06-22 21:46:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-11-10 23:26:54 +03:00
|
|
|
/**
|
|
|
|
* Carries out the search.
|
|
|
|
*
|
|
|
|
* Returns TRUE if found.
|
|
|
|
*
|
|
|
|
* Returns FALSE if not found. In this case, lc_mc_search->error reveals
|
|
|
|
* the reason:
|
|
|
|
*
|
|
|
|
* - MC_SEARCH_E_NOTFOUND: the pattern isn't in the subject string.
|
|
|
|
* - MC_SEARCH_E_ABORT: the user aborted the search.
|
|
|
|
* - For any other reason (but not for the above two!): the description
|
|
|
|
* is in lc_mc_search->error_str.
|
|
|
|
*/
|
2009-04-22 19:52:49 +04:00
|
|
|
gboolean
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
|
2009-04-25 11:15:56 +04:00
|
|
|
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
|
|
|
|
2012-12-07 12:56:59 +04:00
|
|
|
if (lc_mc_search == NULL || user_data == NULL)
|
2009-04-22 19:52:49 +04:00
|
|
|
return FALSE;
|
2010-11-08 13:21:45 +03:00
|
|
|
if (!mc_search_is_type_avail (lc_mc_search->search_type))
|
|
|
|
{
|
2016-04-27 10:26:38 +03:00
|
|
|
mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _(STR_E_UNKNOWN_TYPE));
|
2009-04-25 11:15:56 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-07-07 12:11:29 +04:00
|
|
|
#ifdef SEARCH_TYPE_GLIB
|
2013-06-25 21:09:49 +04:00
|
|
|
if (lc_mc_search->regex_match_info != NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2009-10-30 04:12:04 +03:00
|
|
|
g_match_info_free (lc_mc_search->regex_match_info);
|
|
|
|
lc_mc_search->regex_match_info = NULL;
|
2009-04-25 11:15:56 +04:00
|
|
|
}
|
2009-07-07 12:11:29 +04:00
|
|
|
#endif /* SEARCH_TYPE_GLIB */
|
2009-04-22 19:52:49 +04:00
|
|
|
|
2016-04-27 10:26:38 +03:00
|
|
|
mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
|
2009-04-22 19:52:49 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search))
|
2009-06-22 21:46:34 +04:00
|
|
|
return FALSE;
|
2009-04-25 11:15:56 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
switch (lc_mc_search->search_type)
|
|
|
|
{
|
2009-04-22 19:52:49 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search__run_normal (lc_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-10-30 04:12:04 +03:00
|
|
|
ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
|
2009-04-25 11:15:56 +04:00
|
|
|
break;
|
2009-04-22 19:52:49 +04:00
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
|
2009-04-30 17:06:24 +04:00
|
|
|
break;
|
|
|
|
case MC_SEARCH_T_HEX:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
|
2009-05-05 23:28:27 +04:00
|
|
|
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)
|
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-29 17:13:12 +04:00
|
|
|
|
2009-05-08 16:23:38 +04:00
|
|
|
const mc_search_type_str_t *
|
2010-11-08 13:21:45 +03:00
|
|
|
mc_search_types_list_get (size_t * num)
|
2009-04-29 17:13:12 +04:00
|
|
|
{
|
2009-09-22 13:35:09 +04:00
|
|
|
/* don't count last NULL item */
|
|
|
|
if (num != NULL)
|
2013-04-12 14:36:56 +04:00
|
|
|
*num = G_N_ELEMENTS (mc_search__list_types) - 1;
|
2009-09-22 13:35:09 +04:00
|
|
|
|
2009-04-29 17:13:12 +04:00
|
|
|
return mc_search__list_types;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-04-29 19:45:21 +04:00
|
|
|
|
|
|
|
GString *
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
|
2009-04-29 19:45:21 +04:00
|
|
|
{
|
|
|
|
GString *ret;
|
|
|
|
|
2016-10-23 09:08:05 +03:00
|
|
|
if (replace_str == NULL || replace_str->len == 0)
|
|
|
|
return g_string_new ("");
|
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
if (lc_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
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
switch (lc_mc_search->search_type)
|
|
|
|
{
|
2009-04-29 19:45:21 +04:00
|
|
|
case MC_SEARCH_T_REGEX:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
|
2009-04-30 11:32:45 +04:00
|
|
|
break;
|
2009-06-08 14:54:35 +04:00
|
|
|
case MC_SEARCH_T_GLOB:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
|
2009-06-08 14:54:35 +04:00
|
|
|
break;
|
2009-04-29 19:45:21 +04:00
|
|
|
case MC_SEARCH_T_NORMAL:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
|
2009-06-08 14:54:35 +04:00
|
|
|
break;
|
2009-04-29 19:45:21 +04:00
|
|
|
case MC_SEARCH_T_HEX:
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
|
2009-06-08 14:54:35 +04:00
|
|
|
break;
|
2009-04-29 19:45:21 +04:00
|
|
|
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
|
|
|
|
2009-06-12 18:10:38 +04:00
|
|
|
char *
|
2014-12-30 17:34:17 +03:00
|
|
|
mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, const char *replace_str)
|
2009-06-12 18:10:38 +04:00
|
|
|
{
|
|
|
|
GString *ret;
|
2010-10-21 17:18:18 +04:00
|
|
|
GString *replace_str2;
|
|
|
|
|
|
|
|
replace_str2 = g_string_new (replace_str);
|
2009-10-30 04:12:04 +03:00
|
|
|
ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
|
2009-06-12 18:10:38 +04:00
|
|
|
g_string_free (replace_str2, TRUE);
|
2010-10-21 17:18:18 +04:00
|
|
|
return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
|
2009-06-12 18:10:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-04-30 15:21:39 +04:00
|
|
|
gboolean
|
2009-10-30 04:12:04 +03:00
|
|
|
mc_search_is_fixed_search_str (mc_search_t * lc_mc_search)
|
2009-04-30 15:21:39 +04:00
|
|
|
{
|
2009-10-30 04:12:04 +03:00
|
|
|
if (lc_mc_search == NULL)
|
2009-04-30 15:21:39 +04:00
|
|
|
return FALSE;
|
2010-11-08 13:21:45 +03:00
|
|
|
switch (lc_mc_search->search_type)
|
|
|
|
{
|
2009-04-30 15:21:39 +04:00
|
|
|
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
|
|
|
default:
|
2009-04-30 17:06:24 +04:00
|
|
|
return TRUE;
|
2009-04-30 15:21:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2013-09-06 12:59:54 +04:00
|
|
|
/* Search specified pattern in specified string.
|
|
|
|
*
|
|
|
|
* @param pattern string to search
|
|
|
|
* @param pattern_charset charset of #pattern. If NULL then cp_display will be used
|
|
|
|
* @param str string where search #pattern
|
|
|
|
* @param search type (normal, regex, hex or glob)
|
|
|
|
*
|
|
|
|
* @return TRUE if found is successful, FALSE otherwise.
|
|
|
|
*/
|
2009-05-06 20:12:32 +04:00
|
|
|
|
|
|
|
gboolean
|
2013-09-06 12:59:54 +04:00
|
|
|
mc_search (const gchar * pattern, const gchar * pattern_charset, const gchar * str,
|
|
|
|
mc_search_type_t type)
|
2009-05-06 20:12:32 +04:00
|
|
|
{
|
|
|
|
gboolean ret;
|
2012-12-07 12:56:59 +04:00
|
|
|
mc_search_t *search;
|
|
|
|
|
|
|
|
if (str == NULL)
|
2009-05-06 20:12:32 +04:00
|
|
|
return FALSE;
|
2012-12-07 12:56:59 +04:00
|
|
|
|
2015-11-15 12:43:03 +03:00
|
|
|
search = mc_search_new (pattern, pattern_charset);
|
2012-12-20 16:18:32 +04:00
|
|
|
if (search == NULL)
|
2012-12-07 12:56:59 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2009-05-06 20:12:32 +04:00
|
|
|
search->search_type = type;
|
2010-03-31 11:48:53 +04:00
|
|
|
search->is_case_sensitive = TRUE;
|
2009-06-22 15:10:17 +04:00
|
|
|
|
|
|
|
if (type == MC_SEARCH_T_GLOB)
|
|
|
|
search->is_entire_line = TRUE;
|
|
|
|
|
2009-06-08 14:54:35 +04:00
|
|
|
ret = mc_search_run (search, str, 0, strlen (str), NULL);
|
|
|
|
mc_search_free (search);
|
2009-05-06 20:12:32 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
2010-03-31 11:48:53 +04:00
|
|
|
mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
|
2009-05-06 20:12:32 +04:00
|
|
|
{
|
2013-06-25 21:09:49 +04:00
|
|
|
if (lc_mc_search == NULL)
|
2009-06-08 14:54:35 +04:00
|
|
|
return 0;
|
2009-10-30 04:12:04 +03:00
|
|
|
if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
|
2009-06-08 14:54:35 +04:00
|
|
|
return 0;
|
2009-07-07 12:11:29 +04:00
|
|
|
#ifdef SEARCH_TYPE_GLIB
|
2009-07-15 22:01:52 +04:00
|
|
|
{
|
|
|
|
gint start_pos;
|
|
|
|
gint end_pos;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
|
2009-07-15 22:01:52 +04:00
|
|
|
return (int) start_pos;
|
|
|
|
}
|
2009-07-07 12:11:29 +04:00
|
|
|
#else /* SEARCH_TYPE_GLIB */
|
2009-10-30 04:12:04 +03:00
|
|
|
return lc_mc_search->iovector[lc_index * 2];
|
2009-07-07 12:11:29 +04:00
|
|
|
#endif /* SEARCH_TYPE_GLIB */
|
2009-05-06 20:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
2010-03-31 11:48:53 +04:00
|
|
|
mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
|
2009-05-06 20:12:32 +04:00
|
|
|
{
|
2013-06-25 21:09:49 +04:00
|
|
|
if (lc_mc_search == NULL)
|
2009-06-08 14:54:35 +04:00
|
|
|
return 0;
|
2009-10-30 04:12:04 +03:00
|
|
|
if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
|
2009-06-08 14:54:35 +04:00
|
|
|
return 0;
|
2009-07-07 12:11:29 +04:00
|
|
|
#ifdef SEARCH_TYPE_GLIB
|
2009-07-15 22:01:52 +04:00
|
|
|
{
|
|
|
|
gint start_pos;
|
|
|
|
gint end_pos;
|
2013-06-25 21:09:49 +04:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
|
2009-07-15 22:01:52 +04:00
|
|
|
return (int) end_pos;
|
|
|
|
}
|
2009-07-07 12:11:29 +04:00
|
|
|
#else /* SEARCH_TYPE_GLIB */
|
2009-10-30 04:12:04 +03:00
|
|
|
return lc_mc_search->iovector[lc_index * 2 + 1];
|
2009-07-07 12:11:29 +04:00
|
|
|
#endif /* SEARCH_TYPE_GLIB */
|
2009-05-06 20:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2016-04-27 10:26:38 +03:00
|
|
|
/**
|
|
|
|
* Replace an old error code and message of an mc_search_t object.
|
|
|
|
*
|
|
|
|
* @param mc_search mc_search_t object
|
|
|
|
* @param code error code, one of mc_search_error_t values
|
|
|
|
* @param format format of error message. If NULL, the old error string is free'd and become NULL
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2016-05-28 12:40:49 +03:00
|
|
|
mc_search_set_error (mc_search_t * lc_mc_search, mc_search_error_t code, const gchar * format, ...)
|
2016-04-27 10:26:38 +03:00
|
|
|
{
|
2016-05-28 12:40:49 +03:00
|
|
|
lc_mc_search->error = code;
|
2016-04-27 10:26:38 +03:00
|
|
|
|
2016-05-28 12:40:49 +03:00
|
|
|
MC_PTR_FREE (lc_mc_search->error_str);
|
2016-04-27 10:26:38 +03:00
|
|
|
|
|
|
|
if (format != NULL)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, format);
|
2016-05-28 12:40:49 +03:00
|
|
|
lc_mc_search->error_str = g_strdup_vprintf (format, args);
|
2016-04-27 10:26:38 +03:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|