lib.c 7.5 KB

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