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 (const GString
* astr
)
53 const char *str
= astr
->str
;
56 gboolean inside_group
= FALSE
;
58 buff
= g_string_sized_new (32);
60 while (loop
< astr
->len
)
65 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
67 g_string_append (buff
, (inside_group
) ? ".*" : "(.*)");
73 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
75 g_string_append (buff
, (inside_group
) ? "." : "(.)");
81 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
83 g_string_append (buff
, "|");
89 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
91 g_string_append (buff
, "(");
98 if (!strutils_is_char_escaped (str
, &(str
[loop
])))
100 g_string_append (buff
, ")");
101 inside_group
= FALSE
;
112 g_string_append_c (buff
, '\\');
113 g_string_append_c (buff
, str
[loop
]);
117 g_string_append_c (buff
, str
[loop
]);
124 /* --------------------------------------------------------------------------------------------- */
127 mc_search__translate_replace_glob_to_regex (gchar
* str
)
131 gboolean escaped_mode
= FALSE
;
133 buff
= g_string_sized_new (32);
145 g_string_append_c (buff
, c
);
151 g_string_append_c (buff
, '\\');
156 g_string_append_c (buff
, '\\');
159 g_string_append_c (buff
, c
);
160 escaped_mode
= FALSE
;
165 /*** public functions ****************************************************************************/
168 mc_search__cond_struct_new_init_glob (const char *charset
, mc_search_t
* lc_mc_search
,
169 mc_search_cond_t
* mc_search_cond
)
173 tmp
= mc_search__glob_translate_to_regex (mc_search_cond
->str
);
174 g_string_free (mc_search_cond
->str
, TRUE
);
176 if (lc_mc_search
->is_entire_line
)
178 g_string_prepend_c (tmp
, '^');
179 g_string_append_c (tmp
, '$');
181 mc_search_cond
->str
= tmp
;
183 mc_search__cond_struct_new_init_regex (charset
, lc_mc_search
, mc_search_cond
);
186 /* --------------------------------------------------------------------------------------------- */
189 mc_search__run_glob (mc_search_t
* lc_mc_search
, const void *user_data
,
190 gsize start_search
, gsize end_search
, gsize
* found_len
)
192 return mc_search__run_regex (lc_mc_search
, user_data
, start_search
, end_search
, found_len
);
195 /* --------------------------------------------------------------------------------------------- */
198 mc_search_glob_prepare_replace_str (mc_search_t
* lc_mc_search
, GString
* replace_str
)
200 GString
*repl
= mc_search__translate_replace_glob_to_regex (replace_str
->str
);
201 GString
*res
= mc_search_regex_prepare_replace_str (lc_mc_search
, repl
);
202 g_string_free (repl
, TRUE
);
206 /* --------------------------------------------------------------------------------------------- */