3 Glob-style pattern matching
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
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/>.
29 #include "lib/global.h"
30 #include "lib/strutil.h"
31 #include "lib/search.h"
32 #include "lib/strescape.h"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 /*** file scope type declarations ****************************************************************/
42 /*** file scope variables ************************************************************************/
44 /*** file scope functions ************************************************************************/
47 mc_search__glob_translate_to_regex (const GString
* astr
)
49 const char *str
= astr
->str
;
52 gboolean inside_group
= FALSE
;
54 buff
= g_string_sized_new (32);
56 for (loop
= 0; loop
< astr
->len
; loop
++)
61 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
63 g_string_append (buff
, inside_group
? ".*" : "(.*)");
68 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
70 g_string_append (buff
, inside_group
? "." : "(.)");
75 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
77 g_string_append_c (buff
, inside_group
? '|' : ',');
82 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
84 g_string_append_c (buff
, '(');
90 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
92 g_string_append_c (buff
, ')');
103 g_string_append_c (buff
, '\\');
108 g_string_append_c (buff
, str
[loop
]);
113 /* --------------------------------------------------------------------------------------------- */
116 mc_search__translate_replace_glob_to_regex (const char *str
)
120 gboolean escaped_mode
= FALSE
;
122 buff
= g_string_sized_new (32);
134 g_string_append_c (buff
, '\\');
142 g_string_append_c (buff
, '\\');
148 g_string_append_c (buff
, '\\');
153 g_string_append_c (buff
, c
);
154 escaped_mode
= FALSE
;
159 /*** public functions ****************************************************************************/
162 mc_search__cond_struct_new_init_glob (const char *charset
, mc_search_t
* lc_mc_search
,
163 mc_search_cond_t
* mc_search_cond
)
167 tmp
= mc_search__glob_translate_to_regex (mc_search_cond
->str
);
168 g_string_free (mc_search_cond
->str
, TRUE
);
170 if (lc_mc_search
->is_entire_line
)
172 g_string_prepend_c (tmp
, '^');
173 g_string_append_c (tmp
, '$');
175 mc_search_cond
->str
= tmp
;
177 mc_search__cond_struct_new_init_regex (charset
, lc_mc_search
, mc_search_cond
);
180 /* --------------------------------------------------------------------------------------------- */
183 mc_search__run_glob (mc_search_t
* lc_mc_search
, const void *user_data
,
184 gsize start_search
, gsize end_search
, gsize
* found_len
)
186 return mc_search__run_regex (lc_mc_search
, user_data
, start_search
, end_search
, found_len
);
189 /* --------------------------------------------------------------------------------------------- */
192 mc_search_glob_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
194 GString
*repl
= mc_search__translate_replace_glob_to_regex (replace_str
->str
);
195 GString
*res
= mc_search_regex_prepare_replace_str (lc_mc_search
, repl
);
196 g_string_free (repl
, TRUE
);
200 /* --------------------------------------------------------------------------------------------- */