Added vfs_path_cmp() ans vfs_path_ncmp() functions
[midnight-commander.git] / lib / search / glob.c
blob3a929d2e77bb89f603ac810eaa09eadc625084d3
1 /*
2 Search text engine.
3 Glob-style pattern matching
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"
32 #include "lib/strescape.h"
34 #include "lib/charsets.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
48 static GString *
49 mc_search__glob_translate_to_regex (const GString * astr)
51 const char *str = astr->str;
52 GString *buff;
53 gsize loop;
54 gboolean inside_group = FALSE;
56 buff = g_string_sized_new (32);
58 for (loop = 0; loop < astr->len; loop++)
59 switch (str[loop])
61 case '*':
62 if (!strutils_is_char_escaped (str, &(str[loop])))
63 g_string_append (buff, inside_group ? ".*" : "(.*)");
64 break;
65 case '?':
66 if (!strutils_is_char_escaped (str, &(str[loop])))
67 g_string_append (buff, inside_group ? "." : "(.)");
68 break;
69 case ',':
70 if (!strutils_is_char_escaped (str, &(str[loop])))
71 g_string_append_c (buff, '|');
72 break;
73 case '{':
74 if (!strutils_is_char_escaped (str, &(str[loop])))
76 g_string_append_c (buff, '(');
77 inside_group = TRUE;
79 break;
80 case '}':
81 if (!strutils_is_char_escaped (str, &(str[loop])))
83 g_string_append_c (buff, ')');
84 inside_group = FALSE;
86 break;
87 case '+':
88 case '.':
89 case '$':
90 case '(':
91 case ')':
92 case '^':
93 g_string_append_c (buff, '\\');
94 /* fall through */
95 default:
96 g_string_append_c (buff, str[loop]);
97 break;
100 return buff;
103 /* --------------------------------------------------------------------------------------------- */
105 static GString *
106 mc_search__translate_replace_glob_to_regex (gchar * str)
108 GString *buff;
109 int cnt = '0';
110 gboolean escaped_mode = FALSE;
112 buff = g_string_sized_new (32);
114 while (*str)
116 char c = *str++;
117 switch (c)
119 case '\\':
120 if (!escaped_mode)
122 escaped_mode = TRUE;
124 g_string_append_c (buff, c);
125 continue;
126 case '*':
127 case '?':
128 if (!escaped_mode)
130 g_string_append_c (buff, '\\');
131 c = ++cnt;
133 break;
134 case '&':
135 g_string_append_c (buff, '\\');
136 break;
138 g_string_append_c (buff, c);
139 escaped_mode = FALSE;
141 return buff;
144 /*** public functions ****************************************************************************/
146 void
147 mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
148 mc_search_cond_t * mc_search_cond)
150 GString *tmp;
152 tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
153 g_string_free (mc_search_cond->str, TRUE);
155 if (lc_mc_search->is_entire_line)
157 g_string_prepend_c (tmp, '^');
158 g_string_append_c (tmp, '$');
160 mc_search_cond->str = tmp;
162 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
165 /* --------------------------------------------------------------------------------------------- */
167 gboolean
168 mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
169 gsize start_search, gsize end_search, gsize * found_len)
171 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
174 /* --------------------------------------------------------------------------------------------- */
176 GString *
177 mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
179 GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
180 GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
181 g_string_free (repl, TRUE);
182 return res;
185 /* --------------------------------------------------------------------------------------------- */