search.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. Search text engine.
  3. Interface functions
  4. Copyright (C) 2009, 2011, 2013
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009
  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. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. static const mc_search_type_str_t mc_search__list_types[] = {
  36. {N_("No&rmal"), MC_SEARCH_T_NORMAL},
  37. {N_("Re&gular expression"), MC_SEARCH_T_REGEX},
  38. {N_("He&xadecimal"), MC_SEARCH_T_HEX},
  39. {N_("Wil&dcard search"), MC_SEARCH_T_GLOB},
  40. {NULL, -1}
  41. };
  42. /*** file scope functions ************************************************************************/
  43. static mc_search_cond_t *
  44. mc_search__cond_struct_new (mc_search_t * lc_mc_search, const char *str,
  45. gsize str_len, const char *charset)
  46. {
  47. mc_search_cond_t *mc_search_cond;
  48. mc_search_cond = g_malloc0 (sizeof (mc_search_cond_t));
  49. mc_search_cond->str = g_string_new_len (str, str_len);
  50. mc_search_cond->charset = g_strdup (charset);
  51. switch (lc_mc_search->search_type)
  52. {
  53. case MC_SEARCH_T_GLOB:
  54. mc_search__cond_struct_new_init_glob (charset, lc_mc_search, mc_search_cond);
  55. break;
  56. case MC_SEARCH_T_NORMAL:
  57. mc_search__cond_struct_new_init_normal (charset, lc_mc_search, mc_search_cond);
  58. break;
  59. case MC_SEARCH_T_REGEX:
  60. mc_search__cond_struct_new_init_regex (charset, lc_mc_search, mc_search_cond);
  61. break;
  62. case MC_SEARCH_T_HEX:
  63. mc_search__cond_struct_new_init_hex (charset, lc_mc_search, mc_search_cond);
  64. break;
  65. default:
  66. break;
  67. }
  68. return mc_search_cond;
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. static void
  72. mc_search__cond_struct_free (mc_search_cond_t * mc_search_cond)
  73. {
  74. if (mc_search_cond->upper)
  75. g_string_free (mc_search_cond->upper, TRUE);
  76. if (mc_search_cond->lower)
  77. g_string_free (mc_search_cond->lower, TRUE);
  78. g_string_free (mc_search_cond->str, TRUE);
  79. g_free (mc_search_cond->charset);
  80. #ifdef SEARCH_TYPE_GLIB
  81. if (mc_search_cond->regex_handle)
  82. g_regex_unref (mc_search_cond->regex_handle);
  83. #else /* SEARCH_TYPE_GLIB */
  84. g_free (mc_search_cond->regex_handle);
  85. #endif /* SEARCH_TYPE_GLIB */
  86. g_free (mc_search_cond);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static void
  90. mc_search__conditions_free (GPtrArray * array)
  91. {
  92. gsize loop1;
  93. for (loop1 = 0; loop1 < array->len; loop1++)
  94. {
  95. mc_search_cond_t *lc_mc_search;
  96. lc_mc_search = (mc_search_cond_t *) g_ptr_array_index (array, loop1);
  97. mc_search__cond_struct_free (lc_mc_search);
  98. }
  99. g_ptr_array_free (array, TRUE);
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. /*** public functions ****************************************************************************/
  103. mc_search_t *
  104. mc_search_new (const gchar * original, gsize str_len)
  105. {
  106. mc_search_t *lc_mc_search;
  107. if (original == NULL)
  108. return NULL;
  109. if ((gssize) str_len == -1)
  110. {
  111. str_len = strlen (original);
  112. if (str_len == 0)
  113. return NULL;
  114. }
  115. lc_mc_search = g_malloc0 (sizeof (mc_search_t));
  116. lc_mc_search->original = g_strndup (original, str_len);
  117. lc_mc_search->original_len = str_len;
  118. return lc_mc_search;
  119. }
  120. /* --------------------------------------------------------------------------------------------- */
  121. void
  122. mc_search_free (mc_search_t * lc_mc_search)
  123. {
  124. if (lc_mc_search == NULL)
  125. return;
  126. g_free (lc_mc_search->original);
  127. g_free (lc_mc_search->error_str);
  128. if (lc_mc_search->conditions != NULL)
  129. mc_search__conditions_free (lc_mc_search->conditions);
  130. #ifdef SEARCH_TYPE_GLIB
  131. if (lc_mc_search->regex_match_info != NULL)
  132. g_match_info_free (lc_mc_search->regex_match_info);
  133. #else /* SEARCH_TYPE_GLIB */
  134. g_free (lc_mc_search->regex_match_info);
  135. #endif /* SEARCH_TYPE_GLIB */
  136. if (lc_mc_search->regex_buffer != NULL)
  137. g_string_free (lc_mc_search->regex_buffer, TRUE);
  138. g_free (lc_mc_search);
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. gboolean
  142. mc_search_prepare (mc_search_t * lc_mc_search)
  143. {
  144. GPtrArray *ret;
  145. ret = g_ptr_array_new ();
  146. #ifdef HAVE_CHARSET
  147. if (lc_mc_search->is_all_charsets)
  148. {
  149. gsize loop1, recoded_str_len;
  150. gchar *buffer;
  151. for (loop1 = 0; loop1 < codepages->len; loop1++)
  152. {
  153. const char *id = ((codepage_desc *) g_ptr_array_index (codepages, loop1))->id;
  154. if (!g_ascii_strcasecmp (id, cp_display))
  155. {
  156. g_ptr_array_add (ret,
  157. mc_search__cond_struct_new (lc_mc_search, lc_mc_search->original,
  158. lc_mc_search->original_len,
  159. cp_display));
  160. continue;
  161. }
  162. buffer =
  163. mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len,
  164. cp_display, id, &recoded_str_len);
  165. g_ptr_array_add (ret,
  166. mc_search__cond_struct_new (lc_mc_search, buffer,
  167. recoded_str_len, id));
  168. g_free (buffer);
  169. }
  170. }
  171. else
  172. {
  173. g_ptr_array_add (ret,
  174. mc_search__cond_struct_new (lc_mc_search,
  175. lc_mc_search->original,
  176. lc_mc_search->original_len, cp_display));
  177. }
  178. #else
  179. g_ptr_array_add (ret,
  180. 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 == NULL || user_data == NULL)
  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 != NULL)
  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 = G_N_ELEMENTS (mc_search__list_types) - 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;
  318. if (str == NULL)
  319. return FALSE;
  320. search = mc_search_new (pattern, -1);
  321. if (search == NULL)
  322. return FALSE;
  323. search->search_type = type;
  324. search->is_case_sensitive = TRUE;
  325. if (type == MC_SEARCH_T_GLOB)
  326. search->is_entire_line = TRUE;
  327. ret = mc_search_run (search, str, 0, strlen (str), NULL);
  328. mc_search_free (search);
  329. return ret;
  330. }
  331. /* --------------------------------------------------------------------------------------------- */
  332. int
  333. mc_search_getstart_result_by_num (mc_search_t * lc_mc_search, int lc_index)
  334. {
  335. if (lc_mc_search == NULL)
  336. return 0;
  337. if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
  338. return 0;
  339. #ifdef SEARCH_TYPE_GLIB
  340. {
  341. gint start_pos;
  342. gint end_pos;
  343. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
  344. return (int) start_pos;
  345. }
  346. #else /* SEARCH_TYPE_GLIB */
  347. return lc_mc_search->iovector[lc_index * 2];
  348. #endif /* SEARCH_TYPE_GLIB */
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. int
  352. mc_search_getend_result_by_num (mc_search_t * lc_mc_search, int lc_index)
  353. {
  354. if (lc_mc_search == NULL)
  355. return 0;
  356. if (lc_mc_search->search_type == MC_SEARCH_T_NORMAL)
  357. return 0;
  358. #ifdef SEARCH_TYPE_GLIB
  359. {
  360. gint start_pos;
  361. gint end_pos;
  362. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &start_pos, &end_pos);
  363. return (int) end_pos;
  364. }
  365. #else /* SEARCH_TYPE_GLIB */
  366. return lc_mc_search->iovector[lc_index * 2 + 1];
  367. #endif /* SEARCH_TYPE_GLIB */
  368. }
  369. /* --------------------------------------------------------------------------------------------- */