glob.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 (const char *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. g_string_append_c (buff, '\\');
  100. continue;
  101. }
  102. break;
  103. case '*':
  104. case '?':
  105. if (!escaped_mode)
  106. {
  107. g_string_append_c (buff, '\\');
  108. c = ++cnt;
  109. }
  110. break;
  111. case '&':
  112. if (!escaped_mode)
  113. g_string_append_c (buff, '\\');
  114. break;
  115. }
  116. g_string_append_c (buff, c);
  117. escaped_mode = FALSE;
  118. }
  119. return buff;
  120. }
  121. /*** public functions ****************************************************************************/
  122. void
  123. mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
  124. mc_search_cond_t * mc_search_cond)
  125. {
  126. GString *tmp;
  127. tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
  128. g_string_free (mc_search_cond->str, TRUE);
  129. if (lc_mc_search->is_entire_line)
  130. {
  131. g_string_prepend_c (tmp, '^');
  132. g_string_append_c (tmp, '$');
  133. }
  134. mc_search_cond->str = tmp;
  135. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. gboolean
  139. mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
  140. gsize start_search, gsize end_search, gsize * found_len)
  141. {
  142. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  143. }
  144. /* --------------------------------------------------------------------------------------------- */
  145. GString *
  146. mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  147. {
  148. GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
  149. GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
  150. g_string_free (repl, TRUE);
  151. return res;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */