lib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Search text engine.
  3. Common share code for module.
  4. Copyright (C) 2009-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009, 2011
  8. Andrew Borodin <aborodin@vmail.ru>, 2013
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #include "lib/global.h"
  25. #include "lib/strutil.h"
  26. #include "lib/search.h"
  27. #ifdef HAVE_CHARSET
  28. #include "lib/charsets.h"
  29. #endif
  30. #include "internal.h"
  31. /*** global variables ****************************************************************************/
  32. /* *INDENT-OFF* */
  33. const char *STR_E_NOTFOUND = N_("Search string not found");
  34. const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
  35. const char *STR_E_RPL_NOT_EQ_TO_FOUND =
  36. N_("Num of replace tokens not equal to num of found tokens");
  37. const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
  38. /* *INDENT-ON* */
  39. /*** file scope macro definitions ****************************************************************/
  40. /*** file scope type declarations ****************************************************************/
  41. typedef gboolean (*case_conv_fn) (const char *ch, char **out, size_t *remain);
  42. /*** forward declarations (file scope functions) *************************************************/
  43. /*** file scope variables ************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. /*** file scope functions ************************************************************************/
  46. /* --------------------------------------------------------------------------------------------- */
  47. static GString *
  48. mc_search__change_case_str (const char *charset, const GString *str, case_conv_fn case_conv)
  49. {
  50. GString *ret;
  51. const char *src_ptr;
  52. gchar *dst_str;
  53. gchar *dst_ptr;
  54. gsize dst_len;
  55. #ifdef HAVE_CHARSET
  56. GString *converted_str;
  57. if (charset == NULL)
  58. charset = cp_source;
  59. converted_str = mc_search__recode_str (str->str, str->len, charset, cp_display);
  60. dst_len = converted_str->len + 1; /* +1 is required for str_toupper/str_tolower */
  61. dst_str = g_malloc (dst_len);
  62. for (src_ptr = converted_str->str, dst_ptr = dst_str;
  63. case_conv (src_ptr, &dst_ptr, &dst_len); src_ptr += str_length_char (src_ptr))
  64. ;
  65. *dst_ptr = '\0';
  66. dst_len = converted_str->len;
  67. g_string_free (converted_str, TRUE);
  68. ret = mc_search__recode_str (dst_str, dst_len, cp_display, charset);
  69. g_free (dst_str);
  70. #else
  71. (void) charset;
  72. dst_len = str->len + 1; /* +1 is required for str_toupper/str_tolower */
  73. dst_str = g_malloc (dst_len);
  74. for (src_ptr = str->str, dst_ptr = dst_str;
  75. case_conv (src_ptr, &dst_ptr, &dst_len); src_ptr += str_length_char (src_ptr))
  76. ;
  77. *dst_ptr = '\0';
  78. ret = g_string_new_len (dst_str, dst_len);
  79. g_free (dst_str);
  80. #endif
  81. return ret;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /*** public functions ****************************************************************************/
  85. /* --------------------------------------------------------------------------------------------- */
  86. GString *
  87. mc_search__recode_str (const char *str, gsize str_len, const char *charset_from,
  88. const char *charset_to)
  89. {
  90. GString *ret = NULL;
  91. if (charset_from != NULL && charset_to != NULL
  92. && g_ascii_strcasecmp (charset_to, charset_from) != 0)
  93. {
  94. GIConv conv;
  95. conv = g_iconv_open (charset_to, charset_from);
  96. if (conv != INVALID_CONV)
  97. {
  98. gchar *val;
  99. gsize bytes_read = 0;
  100. gsize bytes_written = 0;
  101. val = g_convert_with_iconv (str, str_len, conv, &bytes_read, &bytes_written, NULL);
  102. g_iconv_close (conv);
  103. if (val != NULL)
  104. {
  105. ret = g_string_new_len (val, bytes_written);
  106. g_free (val);
  107. }
  108. }
  109. }
  110. if (ret == NULL)
  111. ret = g_string_new_len (str, str_len);
  112. return ret;
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. GString *
  116. mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
  117. gboolean *just_letters)
  118. {
  119. GString *converted_str;
  120. const gchar *next_char;
  121. #ifdef HAVE_CHARSET
  122. GString *converted_str2;
  123. if (charset == NULL)
  124. charset = cp_source;
  125. converted_str = mc_search__recode_str (str, str_len, charset, cp_display);
  126. #else
  127. (void) charset;
  128. converted_str = g_string_new_len (str, str_len);
  129. #endif
  130. next_char = str_cget_next_char (converted_str->str);
  131. g_string_set_size (converted_str, (gsize) (next_char - converted_str->str));
  132. #ifdef HAVE_CHARSET
  133. converted_str2 =
  134. mc_search__recode_str (converted_str->str, converted_str->len, cp_display, charset);
  135. #endif
  136. if (just_letters != NULL)
  137. *just_letters = str_isalnum (converted_str->str) && !str_isdigit (converted_str->str);
  138. #ifdef HAVE_CHARSET
  139. g_string_free (converted_str, TRUE);
  140. return converted_str2;
  141. #else
  142. return converted_str;
  143. #endif
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. GString *
  147. mc_search__tolower_case_str (const char *charset, const GString *str)
  148. {
  149. return mc_search__change_case_str (charset, str, str_tolower);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. GString *
  153. mc_search__toupper_case_str (const char *charset, const GString *str)
  154. {
  155. return mc_search__change_case_str (charset, str, str_toupper);
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. gchar **
  159. mc_search_get_types_strings_array (size_t *num)
  160. {
  161. gchar **ret;
  162. int lc_index;
  163. size_t n;
  164. const mc_search_type_str_t *type_str;
  165. const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
  166. ret = g_try_new0 (char *, n + 1);
  167. if (ret == NULL)
  168. return NULL;
  169. for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
  170. ret[lc_index] = g_strdup (type_str->str);
  171. /* don't count last NULL item */
  172. if (num != NULL)
  173. *num = (size_t) lc_index;
  174. return ret;
  175. }
  176. /* --------------------------------------------------------------------------------------------- */