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 "lib/charsets.h"
  26. #include "internal.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /*** file scope functions ************************************************************************/
  32. static GString *
  33. mc_search__glob_translate_to_regex (const GString * astr)
  34. {
  35. const char *str = astr->str;
  36. GString *buff;
  37. gsize loop;
  38. gboolean inside_group = FALSE;
  39. buff = g_string_sized_new (32);
  40. for (loop = 0; loop < astr->len; loop++)
  41. switch (str[loop])
  42. {
  43. case '*':
  44. if (!strutils_is_char_escaped (str, &(str[loop])))
  45. g_string_append (buff, inside_group ? ".*" : "(.*)");
  46. break;
  47. case '?':
  48. if (!strutils_is_char_escaped (str, &(str[loop])))
  49. g_string_append (buff, inside_group ? "." : "(.)");
  50. break;
  51. case ',':
  52. if (!strutils_is_char_escaped (str, &(str[loop])))
  53. g_string_append_c (buff, '|');
  54. break;
  55. case '{':
  56. if (!strutils_is_char_escaped (str, &(str[loop])))
  57. {
  58. g_string_append_c (buff, '(');
  59. inside_group = TRUE;
  60. }
  61. break;
  62. case '}':
  63. if (!strutils_is_char_escaped (str, &(str[loop])))
  64. {
  65. g_string_append_c (buff, ')');
  66. inside_group = FALSE;
  67. }
  68. break;
  69. case '+':
  70. case '.':
  71. case '$':
  72. case '(':
  73. case ')':
  74. case '^':
  75. g_string_append_c (buff, '\\');
  76. /* fall through */
  77. default:
  78. g_string_append_c (buff, str[loop]);
  79. break;
  80. }
  81. return buff;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. static GString *
  85. mc_search__translate_replace_glob_to_regex (gchar * str)
  86. {
  87. GString *buff;
  88. int cnt = '0';
  89. gboolean escaped_mode = FALSE;
  90. buff = g_string_sized_new (32);
  91. while (*str)
  92. {
  93. char c = *str++;
  94. switch (c)
  95. {
  96. case '\\':
  97. if (!escaped_mode)
  98. {
  99. escaped_mode = TRUE;
  100. }
  101. g_string_append_c (buff, c);
  102. continue;
  103. case '*':
  104. case '?':
  105. if (!escaped_mode)
  106. {
  107. g_string_append_c (buff, '\\');
  108. c = ++cnt;
  109. }
  110. break;
  111. case '&':
  112. g_string_append_c (buff, '\\');
  113. break;
  114. }
  115. g_string_append_c (buff, c);
  116. escaped_mode = FALSE;
  117. }
  118. return buff;
  119. }
  120. /*** public functions ****************************************************************************/
  121. void
  122. mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t * lc_mc_search,
  123. mc_search_cond_t * mc_search_cond)
  124. {
  125. GString *tmp;
  126. tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
  127. g_string_free (mc_search_cond->str, TRUE);
  128. if (lc_mc_search->is_entire_line)
  129. {
  130. g_string_prepend_c (tmp, '^');
  131. g_string_append_c (tmp, '$');
  132. }
  133. mc_search_cond->str = tmp;
  134. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. gboolean
  138. mc_search__run_glob (mc_search_t * lc_mc_search, const void *user_data,
  139. gsize start_search, gsize end_search, gsize * found_len)
  140. {
  141. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  142. }
  143. /* --------------------------------------------------------------------------------------------- */
  144. GString *
  145. mc_search_glob_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  146. {
  147. GString *repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
  148. GString *res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
  149. g_string_free (repl, TRUE);
  150. return res;
  151. }
  152. /* --------------------------------------------------------------------------------------------- */