glob.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. switch (str[loop]) {
  42. case '*':
  43. if (!strutils_is_char_escaped (str, &(str[loop]))) {
  44. g_string_append (buff, (inside_group) ? ".*" : "(.*)");
  45. loop++;
  46. continue;
  47. }
  48. break;
  49. case '?':
  50. if (!strutils_is_char_escaped (str, &(str[loop]))) {
  51. g_string_append (buff, (inside_group) ? "." : "(.)");
  52. loop++;
  53. continue;
  54. }
  55. break;
  56. case ',':
  57. if (!strutils_is_char_escaped (str, &(str[loop]))) {
  58. g_string_append (buff, "|");
  59. loop++;
  60. continue;
  61. }
  62. break;
  63. case '{':
  64. if (!strutils_is_char_escaped (str, &(str[loop]))) {
  65. g_string_append (buff, "(");
  66. inside_group = TRUE;
  67. loop++;
  68. continue;
  69. }
  70. break;
  71. case '}':
  72. if (!strutils_is_char_escaped (str, &(str[loop]))) {
  73. g_string_append (buff, ")");
  74. inside_group = FALSE;
  75. loop++;
  76. continue;
  77. }
  78. break;
  79. case '+':
  80. case '.':
  81. case '$':
  82. case '(':
  83. case ')':
  84. case '^':
  85. g_string_append_c (buff, '\\');
  86. g_string_append_c (buff, str[loop]);
  87. loop++;
  88. continue;
  89. break;
  90. }
  91. g_string_append_c (buff, str[loop]);
  92. loop++;
  93. }
  94. *len = buff->len;
  95. return buff;
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. static GString *
  99. mc_search__translate_replace_glob_to_regex (gchar *str)
  100. {
  101. GString *buff = g_string_new ("");
  102. int cnt = '0';
  103. while (*str) {
  104. char c = *str++;
  105. switch (c) {
  106. case '*':
  107. case '?':
  108. g_string_append_c (buff, '\\');
  109. c = ++cnt;
  110. break;
  111. /* breaks copying: mc uses "\0" internally, it must not be changed */
  112. /*case '\\':*/
  113. case '&':
  114. g_string_append_c (buff, '\\');
  115. break;
  116. }
  117. g_string_append_c (buff, c);
  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. mc_search__glob_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len);
  128. g_string_free (mc_search_cond->str, TRUE);
  129. if (lc_mc_search->is_entire_line) {
  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. /* --------------------------------------------------------------------------------------------- */