search.c 13 KB

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