Add useful macros for widget type cast.
[midnight-commander.git] / lib / search / glob.c
blobdd28c52b8983c7ed5a9c0e096c7c4d73b37e1cfd
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 "internal.h"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 /*** file scope type declarations ****************************************************************/
42 /*** file scope variables ************************************************************************/
44 /*** file scope functions ************************************************************************/
46 static GString *
47 mc_search__glob_translate_to_regex (const GString * astr)
49 const char *str = astr->str;
50 GString *buff;
51 gsize loop;
52 gboolean inside_group = FALSE;
54 buff = g_string_sized_new (32);
56 for (loop = 0; loop < astr->len; loop++)
57 switch (str[loop])
59 case '*':
60 if (!strutils_is_char_escaped (str, &(str[loop])))
61 g_string_append (buff, inside_group ? ".*" : "(.*)");
62 break;
63 case '?':
64 if (!strutils_is_char_escaped (str, &(str[loop])))
65 g_string_append (buff, inside_group ? "." : "(.)");
66 break;
67 case ',':
68 if (!strutils_is_char_escaped (str, &(str[loop])))
69 g_string_append_c (buff, '|');
70 break;
71 case '{':
72 if (!strutils_is_char_escaped (str, &(str[loop])))
74 g_string_append_c (buff, '(');
75 inside_group = TRUE;
77 break;
78 case '}':
79 if (!strutils_is_char_escaped (str, &(str[loop])))
81 g_string_append_c (buff, ')');
82 inside_group = FALSE;
84 break;
85 case '+':
86 case '.':
87 case '$':
88 case '(':
89 case ')':
90 case '^':
91 g_string_append_c (buff, '\\');
92 /* fall through */
93 default:
94 g_string_append_c (buff, str[loop]);
95 break;
98 return buff;
101 /* --------------------------------------------------------------------------------------------- */
103 static GString *
104 mc_search__translate_replace_glob_to_regex (gchar * str)
106 GString *buff;
107 int cnt = '0';
108 gboolean escaped_mode = FALSE;
110 buff = g_string_sized_new (32);
112 while (*str)
114 char c = *str++;
115 switch (c)
117 case '\\':
118 if (!escaped_mode)
120 escaped_mode = TRUE;
122 g_string_append_c (buff, c);
123 continue;
124 case '*':
125 case '?':
126 if (!escaped_mode)
128 g_string_append_c (buff, '\\');
129 c = ++cnt;
131 break;
132 case '&':
133 g_string_append_c (buff, '\\');
134 break;
136 g_string_append_c (buff, c);
137 escaped_mode = FALSE;
139 return buff;
142 /*** public functions ****************************************************************************/
144 void
145 mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
146 mc_search_cond_t * mc_search_cond)
148 GString *tmp;
150 tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
151 g_string_free (mc_search_cond->str, TRUE);
153 if (lc_mc_search->is_entire_line)
155 g_string_prepend_c (tmp, '^');
156 g_string_append_c (tmp, '$');
158 mc_search_cond->str = tmp;
160 mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
163 /* --------------------------------------------------------------------------------------------- */
165 gboolean
166 mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
167 gsize start_search, gsize end_search, gsize * found_len)
169 return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
172 /* --------------------------------------------------------------------------------------------- */
174 GString *
175 mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
177 GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
178 GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
179 g_string_free (repl, TRUE);
180 return res;
183 /* --------------------------------------------------------------------------------------------- */