search.c 13 KB

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