lib.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include "internal.h"
  27. #include "lib/charsets.h"
  28. /*** global variables ****************************************************************************/
  29. const char *STR_E_NOTFOUND = N_("Search string not found");
  30. const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
  31. const char *STR_E_RPL_NOT_EQ_TO_FOUND =
  32. 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. {
  48. *bytes_written = str_len;
  49. return g_strndup (str, str_len);
  50. }
  51. conv = g_iconv_open (charset_to, charset_from);
  52. if (conv == INVALID_CONV)
  53. {
  54. *bytes_written = str_len;
  55. return g_strndup (str, str_len);
  56. }
  57. ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
  58. g_iconv_close (conv);
  59. if (ret == NULL)
  60. {
  61. *bytes_written = str_len;
  62. return 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, *next_char;
  72. gsize tmp_len;
  73. #ifdef HAVE_CHARSET
  74. gsize converted_str_len;
  75. gchar *converted_str2;
  76. if (charset == NULL)
  77. charset = cp_source;
  78. converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  79. #else
  80. (void) charset;
  81. converted_str = g_strndup (str, str_len);
  82. #endif
  83. next_char = (char *) str_cget_next_char (converted_str);
  84. tmp_len = next_char - converted_str;
  85. converted_str[tmp_len] = '\0';
  86. #ifdef HAVE_CHARSET
  87. converted_str2 =
  88. mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
  89. #endif
  90. if (just_letters)
  91. {
  92. if (str_isalnum (converted_str) && !str_isdigit (converted_str))
  93. *just_letters = TRUE;
  94. else
  95. *just_letters = FALSE;
  96. }
  97. #ifdef HAVE_CHARSET
  98. g_free (converted_str);
  99. return converted_str2;
  100. #else
  101. return converted_str;
  102. #endif
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. int
  106. mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos)
  107. {
  108. char *data;
  109. if (lc_mc_search->search_fn)
  110. return (lc_mc_search->search_fn) (user_data, current_pos);
  111. data = (char *) user_data;
  112. return (int) (unsigned char) data[current_pos];
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. GString *
  116. mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
  117. {
  118. GString *ret;
  119. #ifdef HAVE_CHARSET
  120. gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
  121. gsize converted_str_len;
  122. gsize tmp_len;
  123. if (charset == NULL)
  124. charset = cp_source;
  125. tmp_str2 = converted_str =
  126. mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  127. tmp_len = converted_str_len + 1;
  128. tmp_str3 = tmp_str1 = g_strdup (converted_str);
  129. while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
  130. tmp_str1 += str_length_char (tmp_str1);
  131. g_free (tmp_str3);
  132. tmp_str2 =
  133. mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
  134. g_free (converted_str);
  135. ret = g_string_new_len (tmp_str2, tmp_len);
  136. g_free (tmp_str2);
  137. return ret;
  138. #else
  139. const gchar *tmp_str1 = str;
  140. gchar *converted_str, *tmp_str2;
  141. gsize converted_str_len = str_len + 1;
  142. (void) charset;
  143. tmp_str2 = converted_str = g_strndup (str, str_len);
  144. while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
  145. tmp_str1 += str_length_char (tmp_str1);
  146. ret = g_string_new_len (converted_str, str_len);
  147. g_free (converted_str);
  148. return ret;
  149. #endif
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. GString *
  153. mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
  154. {
  155. GString *ret;
  156. #ifdef HAVE_CHARSET
  157. gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
  158. gsize converted_str_len;
  159. gsize tmp_len;
  160. if (charset == NULL)
  161. charset = cp_source;
  162. tmp_str2 = converted_str =
  163. mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
  164. tmp_len = converted_str_len + 1;
  165. tmp_str3 = tmp_str1 = g_strdup (converted_str);
  166. while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
  167. tmp_str1 += str_length_char (tmp_str1);
  168. g_free (tmp_str3);
  169. tmp_str2 =
  170. mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
  171. g_free (converted_str);
  172. ret = g_string_new_len (tmp_str2, tmp_len);
  173. g_free (tmp_str2);
  174. return ret;
  175. #else
  176. const gchar *tmp_str1 = str;
  177. gchar *converted_str, *tmp_str2;
  178. gsize converted_str_len = str_len + 1;
  179. (void) charset;
  180. tmp_str2 = converted_str = g_strndup (str, str_len);
  181. while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
  182. tmp_str1 += str_length_char (tmp_str1);
  183. ret = g_string_new_len (converted_str, str_len);
  184. g_free (converted_str);
  185. return ret;
  186. #endif
  187. }
  188. /* --------------------------------------------------------------------------------------------- */
  189. gchar **
  190. mc_search_get_types_strings_array (size_t * num)
  191. {
  192. gchar **ret;
  193. int lc_index;
  194. size_t n;
  195. const mc_search_type_str_t *type_str;
  196. const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
  197. ret = g_try_new0 (char *, n + 1);
  198. if (ret == NULL)
  199. return NULL;
  200. for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
  201. ret[lc_index] = g_strdup (type_str->str);
  202. /* don't count last NULL item */
  203. if (num != NULL)
  204. *num = (size_t) lc_index;
  205. return ret;
  206. }
  207. /* --------------------------------------------------------------------------------------------- */