Ticket #2834: Fixed bug 'FTBFS with --disable-vfs'
[midnight-commander.git] / lib / search / normal.c
blob00f87c85599b4b299812a881e3aed4da2e06eb87
1 /*
2 Search text engine.
3 Plain search
5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
29 #include "lib/global.h"
30 #include "lib/strutil.h"
31 #include "lib/search.h"
33 #include "internal.h"
35 /*** global variables ****************************************************************************/
37 /*** file scope macro definitions ****************************************************************/
39 /*** file scope type declarations ****************************************************************/
41 /*** file scope variables ************************************************************************/
43 /*** file scope functions ************************************************************************/
45 static GString *
46 mc_search__normal_translate_to_regex (const GString * astr)
48 const char *str = astr->str;
49 GString *buff;
50 gsize loop;
52 buff = g_string_sized_new (32);
54 for (loop = 0; loop < astr->len; loop++)
55 switch (str[loop])
57 case '*':
58 case '?':
59 case ',':
60 case '{':
61 case '}':
62 case '[':
63 case ']':
64 case '\\':
65 case '+':
66 case '.':
67 case '$':
68 case '(':
69 case ')':
70 case '^':
71 case '-':
72 case '|':
73 g_string_append_c (buff, '\\');
74 /* fall through */
75 default:
76 g_string_append_c (buff, str[loop]);
77 break;
80 return buff;
83 /*** public functions ****************************************************************************/
85 void
86 mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc_search,
87 mc_search_cond_t * mc_search_cond)
89 GString *tmp;
91 tmp = mc_search__normal_translate_to_regex (mc_search_cond->str);
92 g_string_free (mc_search_cond->str, TRUE);
94 if (lc_mc_search->whole_words)
96 /* NOTE: \b as word boundary doesn't allow search
97 * whole words with non-ASCII symbols */
98 g_string_prepend (tmp, "(^|[^\\p{L}\\p{N}_])(");
99 g_string_append (tmp, ")([^\\p{L}\\p{N}_]|$)");
102 mc_search_cond->str = tmp;
103 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
106 /* --------------------------------------------------------------------------------------------- */
108 gboolean
109 mc_search__run_normal (mc_search_t * lc_mc_search, const void *user_data,
110 gsize start_search, gsize end_search, gsize * found_len)
112 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
115 /* --------------------------------------------------------------------------------------------- */
116 GString *
117 mc_search_normal_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
119 (void) lc_mc_search;
120 return g_string_new_len (replace_str->str, replace_str->len);