glob.c 6.0 KB

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