lib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Search text engine.
  3. Common share code for module.
  4. Copyright (C) 2009-2021
  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. const char *STR_E_NOTFOUND = N_("Search string not found");
  33. const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
  34. const char *STR_E_RPL_NOT_EQ_TO_FOUND =
  35. N_("Num of replace tokens not equal to num of found tokens");
  36. const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** file scope variables ************************************************************************/
  40. /*** file scope functions ************************************************************************/
  41. /*** public functions ****************************************************************************/
  42. gchar *
  43. mc_search__recode_str (const char *str, gsize str_len,
  44. const char *charset_from, const char *charset_to, gsize * bytes_written)
  45. {
  46. gchar *ret = NULL;
  47. if (charset_from != NULL && charset_to != NULL
  48. && g_ascii_strcasecmp (charset_to, charset_from) != 0)
  49. {
  50. GIConv conv;
  51. conv = g_iconv_open (charset_to, charset_from);
  52. if (conv != INVALID_CONV)
  53. {
  54. gsize bytes_read;
  55. ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
  56. g_iconv_close (conv);
  57. }
  58. }
  59. if (ret == NULL)
  60. {
  61. *bytes_written = str_len;
  62. ret = g_strndup (str, str_len);
  63. }
  64. return ret;
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. gchar *
  68. mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
  69. gboolean * just_letters)
  70. {
  71. gchar *converted_str;
  72. const gchar *next_char;
  73. gsize tmp_len;
  74. #ifdef HAVE_CHARSET
  75. gsize converted_str_len;
  76. gchar *converted_str2;
  77. if (charset == NULL)
  78. charset = cp_source;
  79. converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  80. #else
  81. (void) charset;
  82. converted_str = g_strndup (str, str_len);
  83. #endif
  84. next_char = str_cget_next_char (converted_str);
  85. tmp_len = next_char - converted_str;
  86. converted_str[tmp_len] = '\0';
  87. #ifdef HAVE_CHARSET
  88. converted_str2 =
  89. mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
  90. #endif
  91. if (just_letters != NULL)
  92. *just_letters = str_isalnum (converted_str) && !str_isdigit (converted_str);
  93. #ifdef HAVE_CHARSET
  94. g_free (converted_str);
  95. return converted_str2;
  96. #else
  97. return converted_str;
  98. #endif
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. GString *
  102. mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
  103. {
  104. GString *ret;
  105. #ifdef HAVE_CHARSET
  106. gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
  107. gsize converted_str_len;
  108. gsize tmp_len;
  109. if (charset == NULL)
  110. charset = cp_source;
  111. tmp_str2 = converted_str =
  112. mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  113. tmp_len = converted_str_len + 1;
  114. tmp_str3 = tmp_str1 = g_strdup (converted_str);
  115. while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
  116. tmp_str1 += str_length_char (tmp_str1);
  117. g_free (tmp_str3);
  118. tmp_str2 =
  119. mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
  120. g_free (converted_str);
  121. ret = g_string_new_len (tmp_str2, tmp_len);
  122. g_free (tmp_str2);
  123. return ret;
  124. #else
  125. const gchar *tmp_str1 = str;
  126. gchar *converted_str, *tmp_str2;
  127. gsize converted_str_len = str_len + 1;
  128. (void) charset;
  129. tmp_str2 = converted_str = g_strndup (str, str_len);
  130. while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
  131. tmp_str1 += str_length_char (tmp_str1);
  132. ret = g_string_new_len (converted_str, str_len);
  133. g_free (converted_str);
  134. return ret;
  135. #endif
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. GString *
  139. mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
  140. {
  141. GString *ret;
  142. #ifdef HAVE_CHARSET
  143. gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
  144. gsize converted_str_len;
  145. gsize tmp_len;
  146. if (charset == NULL)
  147. charset = cp_source;
  148. tmp_str2 = converted_str =
  149. mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  150. tmp_len = converted_str_len + 1;
  151. tmp_str3 = tmp_str1 = g_strdup (converted_str);
  152. while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
  153. tmp_str1 += str_length_char (tmp_str1);
  154. g_free (tmp_str3);
  155. tmp_str2 =
  156. mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
  157. g_free (converted_str);
  158. ret = g_string_new_len (tmp_str2, tmp_len);
  159. g_free (tmp_str2);
  160. return ret;
  161. #else
  162. const gchar *tmp_str1 = str;
  163. gchar *converted_str, *tmp_str2;
  164. gsize converted_str_len = str_len + 1;
  165. (void) charset;
  166. tmp_str2 = converted_str = g_strndup (str, str_len);
  167. while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
  168. tmp_str1 += str_length_char (tmp_str1);
  169. ret = g_string_new_len (converted_str, str_len);
  170. g_free (converted_str);
  171. return ret;
  172. #endif
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. gchar **
  176. mc_search_get_types_strings_array (size_t * num)
  177. {
  178. gchar **ret;
  179. int lc_index;
  180. size_t n;
  181. const mc_search_type_str_t *type_str;
  182. const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
  183. ret = g_try_new0 (char *, n + 1);
  184. if (ret == NULL)
  185. return NULL;
  186. for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
  187. ret[lc_index] = g_strdup (type_str->str);
  188. /* don't count last NULL item */
  189. if (num != NULL)
  190. *num = (size_t) lc_index;
  191. return ret;
  192. }
  193. /* --------------------------------------------------------------------------------------------- */