glob.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "lib/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 (const GString * astr)
  35. {
  36. const char *str = astr->str;
  37. GString *buff;
  38. gsize loop;
  39. gboolean inside_group = FALSE;
  40. buff = g_string_sized_new (32);
  41. for (loop = 0; loop < astr->len; loop++)
  42. switch (str[loop])
  43. {
  44. case '*':
  45. if (!strutils_is_char_escaped (str, &(str[loop])))
  46. g_string_append (buff, inside_group ? ".*" : "(.*)");
  47. break;
  48. case '?':
  49. if (!strutils_is_char_escaped (str, &(str[loop])))
  50. g_string_append (buff, inside_group ? "." : "(.)");
  51. break;
  52. case ',':
  53. if (!strutils_is_char_escaped (str, &(str[loop])))
  54. g_string_append_c (buff, '|');
  55. break;
  56. case '{':
  57. if (!strutils_is_char_escaped (str, &(str[loop])))
  58. {
  59. g_string_append_c (buff, '(');
  60. inside_group = TRUE;
  61. }
  62. break;
  63. case '}':
  64. if (!strutils_is_char_escaped (str, &(str[loop])))
  65. {
  66. g_string_append_c (buff, ')');
  67. inside_group = FALSE;
  68. }
  69. break;
  70. case '+':
  71. case '.':
  72. case '$':
  73. case '(':
  74. case ')':
  75. case '^':
  76. g_string_append_c (buff, '\\');
  77. /* fall through */
  78. default:
  79. g_string_append_c (buff, str[loop]);
  80. break;
  81. }
  82. return buff;
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. static GString *
  86. mc_search__translate_replace_glob_to_regex (gchar * str)
  87. {
  88. GString *buff;
  89. int cnt = '0';
  90. gboolean escaped_mode = FALSE;
  91. buff = g_string_sized_new (32);
  92. while (*str)
  93. {
  94. char c = *str++;
  95. switch (c)
  96. {
  97. case '\\':
  98. if (!escaped_mode)
  99. {
  100. escaped_mode = TRUE;
  101. }
  102. g_string_append_c (buff, c);
  103. continue;
  104. case '*':
  105. case '?':
  106. if (!escaped_mode)
  107. {
  108. g_string_append_c (buff, '\\');
  109. c = ++cnt;
  110. }
  111. break;
  112. case '&':
  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. /* --------------------------------------------------------------------------------------------- */