Manual pages: mcmcdiffmceditmcview

root/lib/search/search.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_search__cond_struct_new
  2. mc_search__cond_struct_free
  3. mc_search_new
  4. mc_search_new_len
  5. mc_search_free
  6. mc_search_prepare
  7. mc_search_run
  8. mc_search_is_type_avail
  9. mc_search_types_list_get
  10. mc_search_prepare_replace_str
  11. mc_search_prepare_replace_str2
  12. mc_search_is_fixed_search_str
  13. mc_search
  14. mc_search_getstart_result_by_num
  15. mc_search_getend_result_by_num
  16. mc_search_set_error

   1 /*
   2    Search text engine.
   3    Interface functions
   4 
   5    Copyright (C) 2009-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009
  10    Andrew Borodin <aborodin@vmail.ru>, 2013
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  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 /*** global variables ****************************************************************************/
  43 
  44 /*** file scope macro definitions ****************************************************************/
  45 
  46 /*** file scope type declarations ****************************************************************/
  47 
  48 /*** forward declarations (file scope functions) *************************************************/
  49 
  50 /*** file scope variables ************************************************************************/
  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 /*** file scope functions ************************************************************************/
  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)
     /* [previous][next][first][last][top][bottom][index][help]  */
  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)
     /* [previous][next][first][last][top][bottom][index][help]  */
  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 /*** public functions ****************************************************************************/
 117 /* --------------------------------------------------------------------------------------------- */
 118 /* Init search descriptor.
 119  *
 120  * @param original pattern to search
 121  * @param original_charset charset of #original. If NULL then cp_display will be used
 122  *
 123  * @return new mc_search_t object. Use #mc_search_free() to free it.
 124  */
 125 
 126 mc_search_t *
 127 mc_search_new (const gchar *original, const gchar *original_charset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 128 {
 129     if (original == NULL)
 130         return NULL;
 131 
 132     return mc_search_new_len (original, strlen (original), original_charset);
 133 }
 134 
 135 /* --------------------------------------------------------------------------------------------- */
 136 /* Init search descriptor.
 137  *
 138  * @param original pattern to search
 139  * @param original_len length of #original or -1 if #original is NULL-terminated
 140  * @param original_charset charset of #original. If NULL then cp_display will be used
 141  *
 142  * @return new mc_search_t object. Use #mc_search_free() to free it.
 143  */
 144 
 145 mc_search_t *
 146 mc_search_new_len (const gchar *original, gsize original_len, const gchar *original_charset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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;
 207 
 208             id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
 209             if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
 210                 g_ptr_array_add (ret,
 211                                  mc_search__cond_struct_new (lc_mc_search,
 212                                                              lc_mc_search->original.str,
 213                                                              lc_mc_search->original.charset));
 214             else
 215             {
 216                 GString *buffer;
 217 
 218                 buffer = mc_search__recode_str (lc_mc_search->original.str->str,
 219                                                 lc_mc_search->original.str->len,
 220                                                 lc_mc_search->original.charset, id);
 221                 g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, id));
 222                 g_string_free (buffer, TRUE);
 223             }
 224         }
 225     }
 226 
 227     lc_mc_search->prepared.conditions = ret;
 228     lc_mc_search->prepared.result = (lc_mc_search->error == MC_SEARCH_E_OK);
 229 
 230     return lc_mc_search->prepared.result;
 231 }
 232 
 233 /* --------------------------------------------------------------------------------------------- */
 234 
 235 /**
 236  * Carries out the search.
 237  *
 238  * Returns TRUE if found.
 239  *
 240  * Returns FALSE if not found. In this case, lc_mc_search->error reveals
 241  * the reason:
 242  *
 243  *   - MC_SEARCH_E_NOTFOUND: the pattern isn't in the subject string.
 244  *   - MC_SEARCH_E_ABORT: the user aborted the search.
 245  *   - For any other reason (but not for the above two!): the description
 246  *     is in lc_mc_search->error_str.
 247  */
 248 gboolean
 249 mc_search_run (mc_search_t *lc_mc_search, const void *user_data, off_t start_search,
     /* [previous][next][first][last][top][bottom][index][help]  */
 250                off_t end_search, gsize *found_len)
 251 {
 252     gboolean ret = FALSE;
 253 
 254     if (lc_mc_search == NULL || user_data == NULL)
 255         return FALSE;
 256     if (!mc_search_is_type_avail (lc_mc_search->search_type))
 257     {
 258         mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _ (STR_E_UNKNOWN_TYPE));
 259         return FALSE;
 260     }
 261 
 262     if (lc_mc_search->regex_match_info != NULL)
 263     {
 264         g_match_info_free (lc_mc_search->regex_match_info);
 265         lc_mc_search->regex_match_info = NULL;
 266     }
 267 
 268     mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
 269 
 270     if (!mc_search_prepare (lc_mc_search))
 271         return FALSE;
 272 
 273     switch (lc_mc_search->search_type)
 274     {
 275     case MC_SEARCH_T_NORMAL:
 276         ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
 277         break;
 278     case MC_SEARCH_T_REGEX:
 279         ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
 280         break;
 281     case MC_SEARCH_T_GLOB:
 282         ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
 283         break;
 284     case MC_SEARCH_T_HEX:
 285         ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
 286         break;
 287     default:
 288         break;
 289     }
 290     return ret;
 291 }
 292 
 293 /* --------------------------------------------------------------------------------------------- */
 294 
 295 gboolean
 296 mc_search_is_type_avail (mc_search_type_t search_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
 297 {
 298     switch (search_type)
 299     {
 300     case MC_SEARCH_T_GLOB:
 301     case MC_SEARCH_T_NORMAL:
 302     case MC_SEARCH_T_REGEX:
 303     case MC_SEARCH_T_HEX:
 304         return TRUE;
 305     default:
 306         break;
 307     }
 308     return FALSE;
 309 }
 310 
 311 /* --------------------------------------------------------------------------------------------- */
 312 
 313 const mc_search_type_str_t *
 314 mc_search_types_list_get (size_t *num)
     /* [previous][next][first][last][top][bottom][index][help]  */
 315 {
 316     // don't count last NULL item
 317     if (num != NULL)
 318         *num = G_N_ELEMENTS (mc_search__list_types) - 1;
 319 
 320     return mc_search__list_types;
 321 }
 322 
 323 /* --------------------------------------------------------------------------------------------- */
 324 
 325 GString *
 326 mc_search_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 327 {
 328     GString *ret;
 329 
 330     if (replace_str == NULL || replace_str->len == 0)
 331         return g_string_new ("");
 332 
 333     if (lc_mc_search == NULL)
 334         return mc_g_string_dup (replace_str);
 335 
 336     switch (lc_mc_search->search_type)
 337     {
 338     case MC_SEARCH_T_REGEX:
 339         ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
 340         break;
 341     case MC_SEARCH_T_GLOB:
 342         ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
 343         break;
 344     case MC_SEARCH_T_NORMAL:
 345         ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
 346         break;
 347     case MC_SEARCH_T_HEX:
 348         ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
 349         break;
 350     default:
 351         ret = mc_g_string_dup (replace_str);
 352         break;
 353     }
 354     return ret;
 355 }
 356 
 357 /* --------------------------------------------------------------------------------------------- */
 358 
 359 char *
 360 mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 361 {
 362     GString *ret;
 363     GString *replace_str2;
 364 
 365     replace_str2 = g_string_new (replace_str);
 366     ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
 367     g_string_free (replace_str2, TRUE);
 368     return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
 369 }
 370 
 371 /* --------------------------------------------------------------------------------------------- */
 372 
 373 gboolean
 374 mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search)
     /* [previous][next][first][last][top][bottom][index][help]  */
 375 {
 376     if (lc_mc_search == NULL)
 377         return FALSE;
 378     switch (lc_mc_search->search_type)
 379     {
 380     case MC_SEARCH_T_REGEX:
 381     case MC_SEARCH_T_GLOB:
 382         return FALSE;
 383     default:
 384         return TRUE;
 385     }
 386 }
 387 
 388 /* --------------------------------------------------------------------------------------------- */
 389 /* Search specified pattern in specified string.
 390  *
 391  * @param pattern string to search
 392  * @param pattern_charset charset of #pattern. If NULL then cp_display will be used
 393  * @param str string where search #pattern
 394  * @param search type (normal, regex, hex or glob)
 395  *
 396  * @return TRUE if found is successful, FALSE otherwise.
 397  */
 398 
 399 gboolean
 400 mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
     /* [previous][next][first][last][top][bottom][index][help]  */
 401            mc_search_type_t type)
 402 {
 403     gboolean ret;
 404     mc_search_t *search;
 405 
 406     if (str == NULL)
 407         return FALSE;
 408 
 409     search = mc_search_new (pattern, pattern_charset);
 410     if (search == NULL)
 411         return FALSE;
 412 
 413     search->search_type = type;
 414     search->is_case_sensitive = TRUE;
 415 
 416     if (type == MC_SEARCH_T_GLOB)
 417         search->is_entire_line = TRUE;
 418 
 419     ret = mc_search_run (search, str, 0, strlen (str), NULL);
 420     mc_search_free (search);
 421     return ret;
 422 }
 423 
 424 /* --------------------------------------------------------------------------------------------- */
 425 
 426 int
 427 mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 428 {
 429     if (lc_mc_search == NULL)
 430         return 0;
 431     if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
 432         return 0;
 433     {
 434         gint start_pos;
 435         gint end_pos;
 436 
 437         g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
 438         return (int) start_pos;
 439     }
 440 }
 441 
 442 /* --------------------------------------------------------------------------------------------- */
 443 
 444 int
 445 mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 446 {
 447     if (lc_mc_search == NULL)
 448         return 0;
 449     if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
 450         return 0;
 451     {
 452         gint start_pos;
 453         gint end_pos;
 454 
 455         g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
 456         return (int) end_pos;
 457     }
 458 }
 459 
 460 /* --------------------------------------------------------------------------------------------- */
 461 /**
 462  * Replace an old error code and message of an mc_search_t object.
 463  *
 464  * @param mc_search mc_search_t object
 465  * @param code error code, one of mc_search_error_t values
 466  * @param format format of error message. If NULL, the old error string is free'd and become NULL
 467  */
 468 
 469 void
 470 mc_search_set_error (mc_search_t *lc_mc_search, mc_search_error_t code, const gchar *format, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 471 {
 472     lc_mc_search->error = code;
 473 
 474     MC_PTR_FREE (lc_mc_search->error_str);
 475 
 476     if (format != NULL)
 477     {
 478         va_list args;
 479 
 480         va_start (args, format);
 481         lc_mc_search->error_str = g_strdup_vprintf (format, args);
 482         va_end (args);
 483     }
 484 }
 485 
 486 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */