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 = get_codepage_id (loop1);
 207 
 208             if (g_ascii_strcasecmp (id, lc_mc_search->original.charset) == 0)
 209                 g_ptr_array_add (ret,
 210                                  mc_search__cond_struct_new (lc_mc_search,
 211                                                              lc_mc_search->original.str,
 212                                                              lc_mc_search->original.charset));
 213             else
 214             {
 215                 GString *buffer;
 216 
 217                 buffer = mc_search__recode_str (lc_mc_search->original.str->str,
 218                                                 lc_mc_search->original.str->len,
 219                                                 lc_mc_search->original.charset, id);
 220                 g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer, id));
 221                 g_string_free (buffer, TRUE);
 222             }
 223         }
 224     }
 225 
 226     lc_mc_search->prepared.conditions = ret;
 227     lc_mc_search->prepared.result = (lc_mc_search->error == MC_SEARCH_E_OK);
 228 
 229     return lc_mc_search->prepared.result;
 230 }
 231 
 232 /* --------------------------------------------------------------------------------------------- */
 233 
 234 /**
 235  * Carries out the search.
 236  *
 237  * Returns TRUE if found.
 238  *
 239  * Returns FALSE if not found. In this case, lc_mc_search->error reveals
 240  * the reason:
 241  *
 242  *   - MC_SEARCH_E_NOTFOUND: the pattern isn't in the subject string.
 243  *   - MC_SEARCH_E_ABORT: the user aborted the search.
 244  *   - For any other reason (but not for the above two!): the description
 245  *     is in lc_mc_search->error_str.
 246  */
 247 gboolean
 248 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]  */
 249                off_t end_search, gsize *found_len)
 250 {
 251     gboolean ret = FALSE;
 252 
 253     if (lc_mc_search == NULL || user_data == NULL)
 254         return FALSE;
 255     if (!mc_search_is_type_avail (lc_mc_search->search_type))
 256     {
 257         mc_search_set_error (lc_mc_search, MC_SEARCH_E_INPUT, "%s", _ (STR_E_UNKNOWN_TYPE));
 258         return FALSE;
 259     }
 260 
 261     if (lc_mc_search->regex_match_info != NULL)
 262     {
 263         g_match_info_free (lc_mc_search->regex_match_info);
 264         lc_mc_search->regex_match_info = NULL;
 265     }
 266 
 267     mc_search_set_error (lc_mc_search, MC_SEARCH_E_OK, NULL);
 268 
 269     if (!mc_search_prepare (lc_mc_search))
 270         return FALSE;
 271 
 272     switch (lc_mc_search->search_type)
 273     {
 274     case MC_SEARCH_T_NORMAL:
 275         ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
 276         break;
 277     case MC_SEARCH_T_REGEX:
 278         ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
 279         break;
 280     case MC_SEARCH_T_GLOB:
 281         ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
 282         break;
 283     case MC_SEARCH_T_HEX:
 284         ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
 285         break;
 286     default:
 287         break;
 288     }
 289     return ret;
 290 }
 291 
 292 /* --------------------------------------------------------------------------------------------- */
 293 
 294 gboolean
 295 mc_search_is_type_avail (mc_search_type_t search_type)
     /* [previous][next][first][last][top][bottom][index][help]  */
 296 {
 297     switch (search_type)
 298     {
 299     case MC_SEARCH_T_GLOB:
 300     case MC_SEARCH_T_NORMAL:
 301     case MC_SEARCH_T_REGEX:
 302     case MC_SEARCH_T_HEX:
 303         return TRUE;
 304     default:
 305         break;
 306     }
 307     return FALSE;
 308 }
 309 
 310 /* --------------------------------------------------------------------------------------------- */
 311 
 312 const mc_search_type_str_t *
 313 mc_search_types_list_get (size_t *num)
     /* [previous][next][first][last][top][bottom][index][help]  */
 314 {
 315     // don't count last NULL item
 316     if (num != NULL)
 317         *num = G_N_ELEMENTS (mc_search__list_types) - 1;
 318 
 319     return mc_search__list_types;
 320 }
 321 
 322 /* --------------------------------------------------------------------------------------------- */
 323 
 324 GString *
 325 mc_search_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 326 {
 327     GString *ret;
 328 
 329     if (replace_str == NULL || replace_str->len == 0)
 330         return g_string_new ("");
 331 
 332     if (lc_mc_search == NULL)
 333         return mc_g_string_dup (replace_str);
 334 
 335     switch (lc_mc_search->search_type)
 336     {
 337     case MC_SEARCH_T_REGEX:
 338         ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
 339         break;
 340     case MC_SEARCH_T_GLOB:
 341         ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
 342         break;
 343     case MC_SEARCH_T_NORMAL:
 344         ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
 345         break;
 346     case MC_SEARCH_T_HEX:
 347         ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
 348         break;
 349     default:
 350         ret = mc_g_string_dup (replace_str);
 351         break;
 352     }
 353     return ret;
 354 }
 355 
 356 /* --------------------------------------------------------------------------------------------- */
 357 
 358 char *
 359 mc_search_prepare_replace_str2 (mc_search_t *lc_mc_search, const char *replace_str)
     /* [previous][next][first][last][top][bottom][index][help]  */
 360 {
 361     GString *ret;
 362     GString *replace_str2;
 363 
 364     replace_str2 = g_string_new (replace_str);
 365     ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
 366     g_string_free (replace_str2, TRUE);
 367     return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
 368 }
 369 
 370 /* --------------------------------------------------------------------------------------------- */
 371 
 372 gboolean
 373 mc_search_is_fixed_search_str (const mc_search_t *lc_mc_search)
     /* [previous][next][first][last][top][bottom][index][help]  */
 374 {
 375     if (lc_mc_search == NULL)
 376         return FALSE;
 377     switch (lc_mc_search->search_type)
 378     {
 379     case MC_SEARCH_T_REGEX:
 380     case MC_SEARCH_T_GLOB:
 381         return FALSE;
 382     default:
 383         return TRUE;
 384     }
 385 }
 386 
 387 /* --------------------------------------------------------------------------------------------- */
 388 /* Search specified pattern in specified string.
 389  *
 390  * @param pattern string to search
 391  * @param pattern_charset charset of #pattern. If NULL then cp_display will be used
 392  * @param str string where search #pattern
 393  * @param search type (normal, regex, hex or glob)
 394  *
 395  * @return TRUE if found is successful, FALSE otherwise.
 396  */
 397 
 398 gboolean
 399 mc_search (const gchar *pattern, const gchar *pattern_charset, const gchar *str,
     /* [previous][next][first][last][top][bottom][index][help]  */
 400            mc_search_type_t type)
 401 {
 402     gboolean ret;
 403     mc_search_t *search;
 404 
 405     if (str == NULL)
 406         return FALSE;
 407 
 408     search = mc_search_new (pattern, pattern_charset);
 409     if (search == NULL)
 410         return FALSE;
 411 
 412     search->search_type = type;
 413     search->is_case_sensitive = TRUE;
 414 
 415     if (type == MC_SEARCH_T_GLOB)
 416         search->is_entire_line = TRUE;
 417 
 418     ret = mc_search_run (search, str, 0, strlen (str), NULL);
 419     mc_search_free (search);
 420     return ret;
 421 }
 422 
 423 /* --------------------------------------------------------------------------------------------- */
 424 
 425 int
 426 mc_search_getstart_result_by_num (mc_search_t *lc_mc_search, int lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 427 {
 428     if (lc_mc_search == NULL)
 429         return 0;
 430     if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
 431         return 0;
 432     {
 433         gint start_pos;
 434         gint end_pos;
 435 
 436         g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
 437         return (int) start_pos;
 438     }
 439 }
 440 
 441 /* --------------------------------------------------------------------------------------------- */
 442 
 443 int
 444 mc_search_getend_result_by_num (mc_search_t *lc_mc_search, int lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 445 {
 446     if (lc_mc_search == NULL)
 447         return 0;
 448     if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
 449         return 0;
 450     {
 451         gint start_pos;
 452         gint end_pos;
 453 
 454         g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
 455         return (int) end_pos;
 456     }
 457 }
 458 
 459 /* --------------------------------------------------------------------------------------------- */
 460 /**
 461  * Replace an old error code and message of an mc_search_t object.
 462  *
 463  * @param mc_search mc_search_t object
 464  * @param code error code, one of mc_search_error_t values
 465  * @param format format of error message. If NULL, the old error string is free'd and become NULL
 466  */
 467 
 468 void
 469 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]  */
 470 {
 471     lc_mc_search->error = code;
 472 
 473     MC_PTR_FREE (lc_mc_search->error_str);
 474 
 475     if (format != NULL)
 476     {
 477         va_list args;
 478 
 479         va_start (args, format);
 480         lc_mc_search->error_str = g_strdup_vprintf (format, args);
 481         va_end (args);
 482     }
 483 }
 484 
 485 /* --------------------------------------------------------------------------------------------- */

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