3 Glob-style pattern matching
5 Copyright (C) 2009 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 #include "lib/global.h"
32 #include "lib/strutil.h"
33 #include "lib/search.h"
34 #include "lib/strescape.h"
36 #include "src/charsets.h"
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
51 mc_search__glob_translate_to_regex (gchar
* str
, gsize
* len
)
53 GString
*buff
= g_string_new ("");
54 gsize orig_len
= *len
;
56 gboolean inside_group
= FALSE
;
57 while (loop
< orig_len
) {
60 if (!strutils_is_char_escaped (str
, &(str
[loop
]))) {
61 g_string_append (buff
, (inside_group
) ? ".*" : "(.*)");
67 if (!strutils_is_char_escaped (str
, &(str
[loop
]))) {
68 g_string_append (buff
, (inside_group
) ? "." : "(.)");
74 if (!strutils_is_char_escaped (str
, &(str
[loop
]))) {
75 g_string_append (buff
, "|");
81 if (!strutils_is_char_escaped (str
, &(str
[loop
]))) {
82 g_string_append (buff
, "(");
89 if (!strutils_is_char_escaped (str
, &(str
[loop
]))) {
90 g_string_append (buff
, ")");
102 g_string_append_c (buff
, '\\');
103 g_string_append_c (buff
, str
[loop
]);
108 g_string_append_c (buff
, str
[loop
]);
115 /* --------------------------------------------------------------------------------------------- */
118 mc_search__translate_replace_glob_to_regex (gchar
*str
)
120 GString
*buff
= g_string_new ("");
128 g_string_append_c (buff
, '\\');
131 /* breaks copying: mc uses "\0" internally, it must not be changed */
134 g_string_append_c (buff
, '\\');
137 g_string_append_c (buff
, c
);
142 /*** public functions ****************************************************************************/
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
)
149 mc_search__glob_translate_to_regex (mc_search_cond
->str
->str
, &mc_search_cond
->len
);
151 g_string_free (mc_search_cond
->str
, TRUE
);
153 if (lc_mc_search
->is_entire_line
) {
154 g_string_prepend_c (tmp
, '^');
155 g_string_append_c (tmp
, '$');
157 mc_search_cond
->str
= tmp
;
159 mc_search__cond_struct_new_init_regex (charset
, lc_mc_search
, mc_search_cond
);
163 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
176 mc_search_glob_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
178 GString
*repl
= mc_search__translate_replace_glob_to_regex(replace_str
->str
);
179 GString
*res
= mc_search_regex_prepare_replace_str (lc_mc_search
, repl
);
180 g_string_free (repl
, TRUE
);
184 /* --------------------------------------------------------------------------------------------- */