lib.c 7.9 KB

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