glob.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Search text engine.
  3. Glob-style pattern matching
  4. Copyright (C) 2009, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009.
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include "lib/global.h"
  22. #include "lib/strutil.h"
  23. #include "lib/search.h"
  24. #include "lib/strescape.h"
  25. #include "internal.h"
  26. /*** global variables ****************************************************************************/
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** file scope variables ************************************************************************/
  30. /*** file scope functions ************************************************************************/
  31. static GString *
  32. mc_search__glob_translate_to_regex (const GString * astr)
  33. {
  34. const char *str = astr->str;
  35. GString *buff;
  36. gsize loop;
  37. gboolean inside_group = FALSE;
  38. buff = g_string_sized_new (32);
  39. for (loop = 0; loop < astr->len; loop++)
  40. switch (str[loop])
  41. {
  42. case '*':
  43. if (!strutils_is_char_escaped (str, &(str[loop])))
  44. g_string_append (buff, inside_group ? ".*" : "(.*)");
  45. break;
  46. case '?':
  47. if (!strutils_is_char_escaped (str, &(str[loop])))
  48. g_string_append (buff, inside_group ? "." : "(.)");
  49. break;
  50. case ',':
  51. if (!strutils_is_char_escaped (str, &(str[loop])))
  52. g_string_append_c (buff, '|');
  53. break;
  54. case '{':
  55. if (!strutils_is_char_escaped (str, &(str[loop])))
  56. {
  57. g_string_append_c (buff, '(');
  58. inside_group = TRUE;
  59. }
  60. break;
  61. case '}':
  62. if (!strutils_is_char_escaped (str, &(str[loop])))
  63. {
  64. g_string_append_c (buff, ')');
  65. inside_group = FALSE;
  66. }
  67. break;
  68. case '+':
  69. case '.':
  70. case '$':
  71. case '(':
  72. case ')':
  73. case '^':
  74. g_string_append_c (buff, '\\');
  75. /* fall through */
  76. default:
  77. g_string_append_c (buff, str[loop]);
  78. break;
  79. }
  80. return buff;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. static GString *
  84. mc_search__translate_replace_glob_to_regex (gchar * str)
  85. {
  86. GString *buff;
  87. int cnt = '0';
  88. gboolean escaped_mode = FALSE;
  89. buff = g_string_sized_new (32);
  90. while (*str)
  91. {
  92. char c = *str++;
  93. switch (c)
  94. {
  95. case '\\':
  96. if (!escaped_mode)
  97. {
  98. escaped_mode = TRUE;
  99. }
  100. g_string_append_c (buff, c);
  101. continue;
  102. case '*':
  103. case '?':
  104. if (!escaped_mode)
  105. {
  106. g_string_append_c (buff, '\\');
  107. c = ++cnt;
  108. }
  109. break;
  110. case '&':
  111. g_string_append_c (buff, '\\');
  112. break;
  113. }
  114. g_string_append_c (buff, c);
  115. escaped_mode = FALSE;
  116. }
  117. return buff;
  118. }
  119. /*** public functions ****************************************************************************/
  120. void
  121. mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
  122. mc_search_cond_t * mc_search_cond)
  123. {
  124. GString *tmp;
  125. tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
  126. g_string_free (mc_search_cond->str, TRUE);
  127. if (lc_mc_search->is_entire_line)
  128. {
  129. g_string_prepend_c (tmp, '^');
  130. g_string_append_c (tmp, '$');
  131. }
  132. mc_search_cond->str = tmp;
  133. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. gboolean
  137. mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
  138. gsize start_search, gsize end_search, gsize * found_len)
  139. {
  140. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  141. }
  142. /* --------------------------------------------------------------------------------------------- */
  143. GString *
  144. mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  145. {
  146. GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
  147. GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
  148. g_string_free (repl, TRUE);
  149. return res;
  150. }
  151. /* --------------------------------------------------------------------------------------------- */