lib.c 7.8 KB

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