glob.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Search text engine.
  3. Glob-style pattern matching
  4. Copyright (C) 2009-2024
  5. 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 "internal.h"
  25. /*** global variables ****************************************************************************/
  26. /*** file scope macro definitions ****************************************************************/
  27. /*** file scope type declarations ****************************************************************/
  28. /*** forward declarations (file scope functions) *************************************************/
  29. /*** file scope variables ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. static GString *
  34. mc_search__glob_translate_to_regex (const GString *astr)
  35. {
  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. {
  42. const char *str = astr->str;
  43. gboolean not_escaped;
  44. not_escaped = !str_is_char_escaped (str, str + loop);
  45. switch (str[loop])
  46. {
  47. case '*':
  48. if (not_escaped)
  49. {
  50. g_string_append (buff, inside_group ? ".*" : "(.*)");
  51. continue;
  52. }
  53. break;
  54. case '?':
  55. if (not_escaped)
  56. {
  57. g_string_append (buff, inside_group ? "." : "(.)");
  58. continue;
  59. }
  60. break;
  61. case ',':
  62. if (not_escaped)
  63. {
  64. g_string_append_c (buff, inside_group ? '|' : ',');
  65. continue;
  66. }
  67. break;
  68. case '{':
  69. if (not_escaped)
  70. {
  71. g_string_append_c (buff, '(');
  72. inside_group = TRUE;
  73. continue;
  74. }
  75. break;
  76. case '}':
  77. if (not_escaped)
  78. {
  79. g_string_append_c (buff, ')');
  80. inside_group = FALSE;
  81. continue;
  82. }
  83. break;
  84. case '+':
  85. case '.':
  86. case '$':
  87. case '(':
  88. case ')':
  89. case '^':
  90. g_string_append_c (buff, '\\');
  91. break;
  92. default:
  93. break;
  94. }
  95. g_string_append_c (buff, str[loop]);
  96. }
  97. return buff;
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. static GString *
  101. mc_search__translate_replace_glob_to_regex (const char *str)
  102. {
  103. GString *buff;
  104. char cnt = '0';
  105. gboolean escaped_mode = FALSE;
  106. buff = g_string_sized_new (32);
  107. while (*str != '\0')
  108. {
  109. char c = *str++;
  110. switch (c)
  111. {
  112. case '\\':
  113. if (!escaped_mode)
  114. {
  115. escaped_mode = TRUE;
  116. g_string_append_c (buff, '\\');
  117. continue;
  118. }
  119. break;
  120. case '*':
  121. case '?':
  122. if (!escaped_mode)
  123. {
  124. g_string_append_c (buff, '\\');
  125. c = ++cnt;
  126. }
  127. break;
  128. case '&':
  129. if (!escaped_mode)
  130. g_string_append_c (buff, '\\');
  131. break;
  132. default:
  133. break;
  134. }
  135. g_string_append_c (buff, c);
  136. escaped_mode = FALSE;
  137. }
  138. return buff;
  139. }
  140. /*** public functions ****************************************************************************/
  141. void
  142. mc_search__cond_struct_new_init_glob (const char *charset, mc_search_t *lc_mc_search,
  143. mc_search_cond_t *mc_search_cond)
  144. {
  145. GString *tmp;
  146. tmp = mc_search__glob_translate_to_regex (mc_search_cond->str);
  147. g_string_free (mc_search_cond->str, TRUE);
  148. if (lc_mc_search->is_entire_line)
  149. {
  150. g_string_prepend_c (tmp, '^');
  151. g_string_append_c (tmp, '$');
  152. }
  153. mc_search_cond->str = tmp;
  154. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  155. }
  156. /* --------------------------------------------------------------------------------------------- */
  157. gboolean
  158. mc_search__run_glob (mc_search_t *lc_mc_search, const void *user_data,
  159. gsize start_search, gsize end_search, gsize *found_len)
  160. {
  161. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. GString *
  165. mc_search_glob_prepare_replace_str (mc_search_t *lc_mc_search, GString *replace_str)
  166. {
  167. GString *repl, *res;
  168. repl = mc_search__translate_replace_glob_to_regex (replace_str->str);
  169. res = mc_search_regex_prepare_replace_str (lc_mc_search, repl);
  170. g_string_free (repl, TRUE);
  171. return res;
  172. }
  173. /* --------------------------------------------------------------------------------------------- */