lib.c 7.8 KB

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