lib.c 7.5 KB

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