normal.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Search text engine.
  3. Plain search
  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 "src/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__normal_translate_to_regex (gchar * str, gsize * len)
  34. {
  35. GString *buff = g_string_new ("");
  36. gsize orig_len = *len;
  37. gsize loop = 0;
  38. while (loop < orig_len) {
  39. switch (str[loop]) {
  40. case '*':
  41. case '?':
  42. case ',':
  43. case '{':
  44. case '}':
  45. case '[':
  46. case ']':
  47. case '\\':
  48. case '+':
  49. case '.':
  50. case '$':
  51. case '(':
  52. case ')':
  53. case '^':
  54. case '-':
  55. case '|':
  56. g_string_append_c (buff, '\\');
  57. g_string_append_c (buff, str[loop]);
  58. loop++;
  59. continue;
  60. }
  61. g_string_append_c (buff, str[loop]);
  62. loop++;
  63. }
  64. *len = buff->len;
  65. return buff;
  66. }
  67. /*** public functions ****************************************************************************/
  68. void
  69. mc_search__cond_struct_new_init_normal (const char *charset, mc_search_t * lc_mc_search,
  70. mc_search_cond_t * mc_search_cond)
  71. {
  72. GString *tmp =
  73. mc_search__normal_translate_to_regex (mc_search_cond->str->str, &mc_search_cond->len);
  74. g_string_free (mc_search_cond->str, TRUE);
  75. if (lc_mc_search->whole_words) {
  76. g_string_prepend (tmp, "\\b");
  77. g_string_append (tmp, "\\b");
  78. }
  79. mc_search_cond->str = tmp;
  80. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. gboolean
  84. mc_search__run_normal (mc_search_t * lc_mc_search, const void *user_data,
  85. gsize start_search, gsize end_search, gsize * found_len)
  86. {
  87. return mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. GString *
  91. mc_search_normal_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  92. {
  93. (void) lc_mc_search;
  94. return g_string_new_len (replace_str->str, replace_str->len);
  95. }