search.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Search text engine.
  3. Interface functions
  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 "src/charsets.h"
  28. #include "internal.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. static const mc_search_type_str_t mc_search__list_types[] = {
  34. {N_("Normal"), MC_SEARCH_T_NORMAL},
  35. {N_("&Regular expression"), MC_SEARCH_T_REGEX},
  36. {N_("Hexadecimal"), MC_SEARCH_T_HEX},
  37. {N_("Wildcard search"), MC_SEARCH_T_GLOB},
  38. {NULL, -1}
  39. };
  40. /*** file scope functions ************************************************************************/
  41. static mc_search_cond_t *
  42. mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str,
  43. gsize str_len, const char *charset)
  44. {
  45. mc_search_cond_t *mc_search_cond;
  46. mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
  47. mc_search_cond->str = g_string_new_len (str, str_len);
  48. mc_search_cond->charset = g_strdup (charset);
  49. switch (lc_mc_search->search_type) {
  50. case MC_SEARCH_T_GLOB:
  51. mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
  52. break;
  53. case MC_SEARCH_T_NORMAL:
  54. mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
  55. break;
  56. case MC_SEARCH_T_REGEX:
  57. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  58. break;
  59. case MC_SEARCH_T_HEX:
  60. mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
  61. break;
  62. default:
  63. break;
  64. }
  65. return mc_search_cond;
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. static void
  69. mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
  70. {
  71. if (mc_search_cond->upper)
  72. g_string_free (mc_search_cond->upper, TRUE);
  73. if (mc_search_cond->lower)
  74. g_string_free (mc_search_cond->lower, TRUE);
  75. g_string_free (mc_search_cond->str, TRUE);
  76. g_free (mc_search_cond->charset);
  77. #ifdef SEARCH_TYPE_GLIB
  78. if (mc_search_cond->regex_handle)
  79. g_regex_unref (mc_search_cond->regex_handle);
  80. #else /* SEARCH_TYPE_GLIB */
  81. g_free (mc_search_cond->regex_handle);
  82. #endif /* SEARCH_TYPE_GLIB */
  83. g_free (mc_search_cond);
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. static void
  87. mc_search__conditions_free (GPtrArray * array)
  88. {
  89. gsize loop1;
  90. mc_search_cond_t *lc_mc_search;
  91. for (loop1 = 0; loop1 < array->len; loop1++) {
  92. lc_mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1);
  93. mc_search__cond_struct_free (lc_mc_search);
  94. }
  95. g_ptr_array_free (array, TRUE);
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. /*** public functions ****************************************************************************/
  99. mc_search_t *
  100. mc_search_new (const gchar * original, gsize str_len)
  101. {
  102. mc_search_t *lc_mc_search;
  103. if (!original)
  104. return NULL;
  105. if ((gssize) str_len == -1) {
  106. str_len = strlen (original);
  107. if (str_len == 0)
  108. return NULL;
  109. }
  110. lc_mc_search = g_malloc0 (sizeof (mc_search_t));
  111. lc_mc_search->original = g_strndup (original, str_len);
  112. lc_mc_search->original_len = str_len;
  113. return lc_mc_search;
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. void
  117. mc_search_free (mc_search_t * lc_mc_search)
  118. {
  119. if (!lc_mc_search)
  120. return;
  121. g_free (lc_mc_search->original);
  122. g_free (lc_mc_search->error_str);
  123. if (lc_mc_search->conditions)
  124. mc_search__conditions_free (lc_mc_search->conditions);
  125. #ifdef SEARCH_TYPE_GLIB
  126. if (lc_mc_search->regex_match_info)
  127. g_match_info_free (lc_mc_search->regex_match_info);
  128. #else /* SEARCH_TYPE_GLIB */
  129. g_free (lc_mc_search->regex_match_info);
  130. #endif /* SEARCH_TYPE_GLIB */
  131. if (lc_mc_search->regex_buffer != NULL)
  132. g_string_free (lc_mc_search->regex_buffer, TRUE);
  133. g_free (lc_mc_search);
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. gboolean
  137. mc_search_prepare (mc_search_t * lc_mc_search)
  138. {
  139. GPtrArray *ret;
  140. ret = g_ptr_array_new ();
  141. #ifdef HAVE_CHARSET
  142. if (lc_mc_search->is_all_charsets) {
  143. gsize loop1, recoded_str_len;
  144. gchar *buffer;
  145. for (loop1 = 0; loop1 < codepages->len; loop1++) {
  146. const char *id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
  147. if (!g_ascii_strcasecmp (id, cp_source)) {
  148. g_ptr_array_add (ret,
  149. mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
  150. lc_mc_search->original_len, cp_source));
  151. continue;
  152. }
  153. buffer =
  154. mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len, cp_source,
  155. id, &recoded_str_len);
  156. g_ptr_array_add (ret,
  157. mc_search__cond_struct_new (lc_mc_search, buffer,
  158. recoded_str_len, id));
  159. g_free (buffer);
  160. }
  161. } else {
  162. g_ptr_array_add (ret,
  163. (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
  164. lc_mc_search->original_len,
  165. cp_source));
  166. }
  167. #else
  168. g_ptr_array_add (ret,
  169. (gpointer) mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
  170. lc_mc_search->original_len,
  171. str_detect_termencoding ()));
  172. #endif
  173. lc_mc_search->conditions = ret;
  174. return (lc_mc_search->error == MC_SEARCH_E_OK);
  175. }
  176. /* --------------------------------------------------------------------------------------------- */
  177. gboolean
  178. mc_search_run (mc_search_t * lc_mc_search, const void *user_data,
  179. gsize start_search, gsize end_search, gsize * found_len)
  180. {
  181. gboolean ret = FALSE;
  182. if (!lc_mc_search)
  183. return FALSE;
  184. if (!mc_search_is_type_avail (lc_mc_search->search_type)) {
  185. lc_mc_search->error = MC_SEARCH_E_INPUT;
  186. lc_mc_search->error_str = g_strdup (_(STR_E_UNKNOWN_TYPE));
  187. return FALSE;
  188. }
  189. #ifdef SEARCH_TYPE_GLIB
  190. if (lc_mc_search->regex_match_info) {
  191. g_match_info_free (lc_mc_search->regex_match_info);
  192. lc_mc_search->regex_match_info = NULL;
  193. }
  194. #endif /* SEARCH_TYPE_GLIB */
  195. lc_mc_search->error = MC_SEARCH_E_OK;
  196. g_free (lc_mc_search->error_str);
  197. lc_mc_search->error_str = NULL;
  198. if ((lc_mc_search->conditions == NULL) && !mc_search_prepare (lc_mc_search))
  199. return FALSE;
  200. switch (lc_mc_search->search_type) {
  201. case MC_SEARCH_T_NORMAL:
  202. ret = mc_search__run_normal (lc_mc_search, user_data, start_search, end_search, found_len);
  203. break;
  204. case MC_SEARCH_T_REGEX:
  205. ret = mc_search__run_regex (lc_mc_search, user_data, start_search, end_search, found_len);
  206. break;
  207. case MC_SEARCH_T_GLOB:
  208. ret = mc_search__run_glob (lc_mc_search, user_data, start_search, end_search, found_len);
  209. break;
  210. case MC_SEARCH_T_HEX:
  211. ret = mc_search__run_hex (lc_mc_search, user_data, start_search, end_search, found_len);
  212. break;
  213. default:
  214. break;
  215. }
  216. return ret;
  217. }
  218. /* --------------------------------------------------------------------------------------------- */
  219. gboolean
  220. mc_search_is_type_avail (mc_search_type_t search_type)
  221. {
  222. switch (search_type) {
  223. case MC_SEARCH_T_GLOB:
  224. case MC_SEARCH_T_NORMAL:
  225. case MC_SEARCH_T_REGEX:
  226. case MC_SEARCH_T_HEX:
  227. return TRUE;
  228. default:
  229. break;
  230. }
  231. return FALSE;
  232. }
  233. /* --------------------------------------------------------------------------------------------- */
  234. const mc_search_type_str_t *
  235. mc_search_types_list_get (size_t *num)
  236. {
  237. /* don't count last NULL item */
  238. if (num != NULL)
  239. *num = sizeof (mc_search__list_types) / sizeof (mc_search__list_types[0]) - 1;
  240. return mc_search__list_types;
  241. }
  242. /* --------------------------------------------------------------------------------------------- */
  243. GString *
  244. mc_search_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  245. {
  246. GString *ret;
  247. if (lc_mc_search == NULL)
  248. return g_string_new_len (replace_str->str, replace_str->len);
  249. if (replace_str == NULL || replace_str->str == NULL || replace_str->len == 0)
  250. return g_string_new ("");
  251. switch (lc_mc_search->search_type) {
  252. case MC_SEARCH_T_REGEX:
  253. ret = mc_search_regex_prepare_replace_str (lc_mc_search, replace_str);
  254. break;
  255. case MC_SEARCH_T_GLOB:
  256. ret = mc_search_glob_prepare_replace_str (lc_mc_search, replace_str);
  257. break;
  258. case MC_SEARCH_T_NORMAL:
  259. ret = mc_search_normal_prepare_replace_str (lc_mc_search, replace_str);
  260. break;
  261. case MC_SEARCH_T_HEX:
  262. ret = mc_search_hex_prepare_replace_str (lc_mc_search, replace_str);
  263. break;
  264. default:
  265. ret = g_string_new_len (replace_str->str, replace_str->len);
  266. break;
  267. }
  268. return ret;
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. char *
  272. mc_search_prepare_replace_str2 (mc_search_t * lc_mc_search, char *replace_str)
  273. {
  274. GString *ret;
  275. GString *replace_str2;
  276. replace_str2 = g_string_new (replace_str);
  277. ret = mc_search_prepare_replace_str (lc_mc_search, replace_str2);
  278. g_string_free (replace_str2, TRUE);
  279. return (ret != NULL) ? g_string_free (ret, FALSE) : NULL;
  280. }
  281. /* --------------------------------------------------------------------------------------------- */
  282. gboolean
  283. mc_search_is_fixed_search_str (mc_search_t * lc_mc_search)
  284. {
  285. if (lc_mc_search == NULL)
  286. return FALSE;
  287. switch (lc_mc_search->search_type) {
  288. case MC_SEARCH_T_REGEX:
  289. case MC_SEARCH_T_GLOB:
  290. return FALSE;
  291. default:
  292. return TRUE;
  293. }
  294. }
  295. /* --------------------------------------------------------------------------------------------- */
  296. gboolean
  297. mc_search (const gchar * pattern, const gchar * str, mc_search_type_t type)
  298. {
  299. gboolean ret;
  300. mc_search_t *search = mc_search_new (pattern, -1);
  301. if (search == NULL)
  302. return FALSE;
  303. search->search_type = type;
  304. search->is_case_sensitive = TRUE;
  305. if (type == MC_SEARCH_T_GLOB)
  306. search->is_entire_line = TRUE;
  307. ret = mc_search_run (search, str, 0, strlen (str), NULL);
  308. mc_search_free (search);
  309. return ret;
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. int
  313. mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
  314. {
  315. if (!lc_mc_search)
  316. return 0;
  317. if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
  318. return 0;
  319. #ifdef SEARCH_TYPE_GLIB
  320. {
  321. gint start_pos;
  322. gint end_pos;
  323. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
  324. return (int) start_pos;
  325. }
  326. #else /* SEARCH_TYPE_GLIB */
  327. return lc_mc_search->iovector[lc_index * 2];
  328. #endif /* SEARCH_TYPE_GLIB */
  329. }
  330. /* --------------------------------------------------------------------------------------------- */
  331. int
  332. mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
  333. {
  334. if (!lc_mc_search)
  335. return 0;
  336. if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
  337. return 0;
  338. #ifdef SEARCH_TYPE_GLIB
  339. {
  340. gint start_pos;
  341. gint end_pos;
  342. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
  343. return (int) end_pos;
  344. }
  345. #else /* SEARCH_TYPE_GLIB */
  346. return lc_mc_search->iovector[lc_index * 2 + 1];
  347. #endif /* SEARCH_TYPE_GLIB */
  348. }
  349. /* --------------------------------------------------------------------------------------------- */