search.c 13 KB

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