regex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. Search text engine.
  3. Regex search
  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 "lib/global.h"
  24. #include "lib/strutil.h"
  25. #include "lib/search.h"
  26. #include "lib/strescape.h"
  27. #include "src/charsets.h"
  28. #include "internal.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. typedef enum {
  33. REPLACE_T_NO_TRANSFORM = 0,
  34. REPLACE_T_UPP_TRANSFORM_CHAR = 1,
  35. REPLACE_T_LOW_TRANSFORM_CHAR = 2,
  36. REPLACE_T_UPP_TRANSFORM = 4,
  37. REPLACE_T_LOW_TRANSFORM = 8
  38. } replace_transform_type_t;
  39. /*** file scope variables ************************************************************************/
  40. /*** file scope functions ************************************************************************/
  41. static gboolean
  42. mc_search__regex_str_append_if_special (GString * copy_to, GString * regex_str, gsize * offset)
  43. {
  44. char *tmp_regex_str;
  45. gsize spec_chr_len;
  46. const char **spec_chr;
  47. const char *special_chars[] = {
  48. "\\s", "\\S",
  49. "\\d", "\\D",
  50. "\\b", "\\B",
  51. "\\w", "\\W",
  52. "\\t", "\\n",
  53. "\\r", "\\f",
  54. "\\a", "\\e",
  55. "\\x", "\\X",
  56. "\\c", "\\C",
  57. "\\l", "\\L",
  58. "\\u", "\\U",
  59. "\\E", "\\Q",
  60. NULL
  61. };
  62. spec_chr = special_chars;
  63. tmp_regex_str = &(regex_str->str[*offset]);
  64. while (*spec_chr) {
  65. spec_chr_len = strlen (*spec_chr);
  66. if (!strncmp (tmp_regex_str, *spec_chr, spec_chr_len)) {
  67. if (!strutils_is_char_escaped (regex_str->str, tmp_regex_str)) {
  68. if (!strncmp ("\\x", *spec_chr, spec_chr_len)) {
  69. if (*(tmp_regex_str + spec_chr_len) == '{') {
  70. while ((spec_chr_len < regex_str->len - *offset)
  71. && *(tmp_regex_str + spec_chr_len) != '}')
  72. spec_chr_len++;
  73. if (*(tmp_regex_str + spec_chr_len) == '}')
  74. spec_chr_len++;
  75. } else
  76. spec_chr_len += 2;
  77. }
  78. g_string_append_len (copy_to, tmp_regex_str, spec_chr_len);
  79. *offset += spec_chr_len;
  80. return TRUE;
  81. }
  82. }
  83. spec_chr++;
  84. }
  85. return FALSE;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. static void
  89. mc_search__cond_struct_new_regex_hex_add (const char *charset, GString * str_to,
  90. const char *one_char, gsize str_len)
  91. {
  92. GString *upp, *low;
  93. gchar *tmp_str;
  94. gsize loop;
  95. upp = mc_search__toupper_case_str (charset, one_char, str_len);
  96. low = mc_search__tolower_case_str (charset, one_char, str_len);
  97. for (loop = 0; loop < upp->len; loop++) {
  98. if (loop < low->len) {
  99. if (upp->str[loop] == low->str[loop])
  100. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  101. else
  102. tmp_str =
  103. g_strdup_printf ("[\\x%02X\\x%02X]", (unsigned char) upp->str[loop],
  104. (unsigned char) low->str[loop]);
  105. } else {
  106. tmp_str = g_strdup_printf ("\\x%02X", (unsigned char) upp->str[loop]);
  107. }
  108. g_string_append (str_to, tmp_str);
  109. g_free (tmp_str);
  110. }
  111. g_string_free (upp, TRUE);
  112. g_string_free (low, TRUE);
  113. }
  114. /* --------------------------------------------------------------------------------------------- */
  115. static void
  116. mc_search__cond_struct_new_regex_accum_append (const char *charset, GString * str_to,
  117. GString * str_from)
  118. {
  119. GString *recoded_part;
  120. gchar *one_char;
  121. gsize loop;
  122. gboolean just_letters;
  123. loop = 0;
  124. recoded_part = g_string_new ("");
  125. while (loop < str_from->len) {
  126. one_char =
  127. mc_search__get_one_symbol (charset, &(str_from->str[loop]),
  128. (str_from->len - loop > 6) ? 6 : str_from->len - loop,
  129. &just_letters);
  130. if (!strlen (one_char)) {
  131. loop++;
  132. continue;
  133. }
  134. if (just_letters) {
  135. mc_search__cond_struct_new_regex_hex_add (charset, recoded_part, one_char,
  136. strlen (one_char));
  137. } else {
  138. g_string_append (recoded_part, one_char);
  139. }
  140. loop += strlen (one_char);
  141. if (!strlen (one_char))
  142. loop++;
  143. g_free (one_char);
  144. }
  145. g_string_append (str_to, recoded_part->str);
  146. g_string_free (recoded_part, TRUE);
  147. g_string_set_size (str_from, 0);
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. static GString *
  151. mc_search__cond_struct_new_regex_ci_str (const char *charset, const char *str, gsize str_len)
  152. {
  153. GString *accumulator, *spec_char, *ret_str;
  154. gsize loop;
  155. GString *tmp;
  156. tmp = g_string_new_len (str, str_len);
  157. ret_str = g_string_new ("");
  158. accumulator = g_string_new ("");
  159. spec_char = g_string_new ("");
  160. loop = 0;
  161. while (loop <= str_len) {
  162. if (mc_search__regex_str_append_if_special (spec_char, tmp, &loop)) {
  163. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  164. g_string_append_len (ret_str, spec_char->str, spec_char->len);
  165. g_string_set_size (spec_char, 0);
  166. continue;
  167. }
  168. if (tmp->str[loop] == '[' && !strutils_is_char_escaped (tmp->str, &(tmp->str[loop]))) {
  169. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  170. while (loop < str_len && !(tmp->str[loop] == ']'
  171. && !strutils_is_char_escaped (tmp->str,
  172. &(tmp->str[loop])))) {
  173. g_string_append_c (ret_str, tmp->str[loop]);
  174. loop++;
  175. }
  176. g_string_append_c (ret_str, tmp->str[loop]);
  177. loop++;
  178. continue;
  179. }
  180. /*
  181. TODO: handle [ and ]
  182. */
  183. g_string_append_c (accumulator, tmp->str[loop]);
  184. loop++;
  185. }
  186. mc_search__cond_struct_new_regex_accum_append (charset, ret_str, accumulator);
  187. g_string_free (accumulator, TRUE);
  188. g_string_free (spec_char, TRUE);
  189. g_string_free (tmp, TRUE);
  190. return ret_str;
  191. }
  192. /* --------------------------------------------------------------------------------------------- */
  193. static mc_search__found_cond_t
  194. mc_search__regex_found_cond_one (mc_search_t * lc_mc_search, mc_search_regex_t * regex,
  195. GString * search_str)
  196. {
  197. #ifdef SEARCH_TYPE_GLIB
  198. GError *error = NULL;
  199. if (!g_regex_match_full
  200. (regex, search_str->str, -1, 0, G_REGEX_MATCH_NEWLINE_ANY, &lc_mc_search->regex_match_info,
  201. &error)) {
  202. g_match_info_free (lc_mc_search->regex_match_info);
  203. lc_mc_search->regex_match_info = NULL;
  204. if (error) {
  205. lc_mc_search->error = MC_SEARCH_E_REGEX;
  206. lc_mc_search->error_str = str_conv_gerror_message (error, _(" Regular expression error "));
  207. g_error_free (error);
  208. return COND__FOUND_ERROR;
  209. }
  210. return COND__NOT_FOUND;
  211. }
  212. lc_mc_search->num_rezults = g_match_info_get_match_count (lc_mc_search->regex_match_info);
  213. #else /* SEARCH_TYPE_GLIB */
  214. lc_mc_search->num_rezults = pcre_exec (regex, lc_mc_search->regex_match_info,
  215. search_str->str, search_str->len - 1, 0, 0, lc_mc_search->iovector,
  216. MC_SEARCH__NUM_REPLACE_ARGS);
  217. if (lc_mc_search->num_rezults < 0) {
  218. return COND__NOT_FOUND;
  219. }
  220. #endif /* SEARCH_TYPE_GLIB */
  221. return COND__FOUND_OK;
  222. }
  223. /* --------------------------------------------------------------------------------------------- */
  224. static mc_search__found_cond_t
  225. mc_search__regex_found_cond (mc_search_t * lc_mc_search, GString * search_str)
  226. {
  227. gsize loop1;
  228. mc_search_cond_t *mc_search_cond;
  229. mc_search__found_cond_t ret;
  230. for (loop1 = 0; loop1 < lc_mc_search->conditions->len; loop1++) {
  231. mc_search_cond = (mc_search_cond_t *) g_ptr_array_index (lc_mc_search->conditions, loop1);
  232. if (!mc_search_cond->regex_handle)
  233. continue;
  234. ret = mc_search__regex_found_cond_one (lc_mc_search, mc_search_cond->regex_handle, search_str);
  235. if (ret != COND__NOT_FOUND)
  236. return ret;
  237. }
  238. return COND__NOT_ALL_FOUND;
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. static int
  242. mc_search_regex__get_max_num_of_replace_tokens (const gchar * str, gsize len)
  243. {
  244. int max_token = 0;
  245. gsize loop;
  246. for (loop = 0; loop < len - 1; loop++) {
  247. if (str[loop] == '\\' && (str[loop + 1] & (char) 0xf0) == 0x30 /* 0-9 */ ) {
  248. if (strutils_is_char_escaped (str, &str[loop]))
  249. continue;
  250. if (max_token < str[loop + 1] - '0')
  251. max_token = str[loop + 1] - '0';
  252. continue;
  253. }
  254. if (str[loop] == '$' && str[loop + 1] == '{') {
  255. gsize tmp_len;
  256. char *tmp_str;
  257. int tmp_token;
  258. if (strutils_is_char_escaped (str, &str[loop]))
  259. continue;
  260. for (tmp_len = 0;
  261. loop + tmp_len + 2 < len && (str[loop + 2 + tmp_len] & (char) 0xf0) == 0x30;
  262. tmp_len++);
  263. if (str[loop + 2 + tmp_len] == '}') {
  264. tmp_str = g_strndup (&str[loop + 2], tmp_len);
  265. tmp_token = atoi (tmp_str);
  266. if (max_token < tmp_token)
  267. max_token = tmp_token;
  268. g_free (tmp_str);
  269. }
  270. }
  271. }
  272. return max_token;
  273. }
  274. /* --------------------------------------------------------------------------------------------- */
  275. static char *
  276. mc_search_regex__get_token_by_num (const mc_search_t * lc_mc_search, gsize lc_index)
  277. {
  278. int fnd_start = 0, fnd_end = 0;
  279. #ifdef SEARCH_TYPE_GLIB
  280. g_match_info_fetch_pos (lc_mc_search->regex_match_info, lc_index, &fnd_start, &fnd_end);
  281. #else /* SEARCH_TYPE_GLIB */
  282. fnd_start = lc_mc_search->iovector[lc_index * 2 + 0];
  283. fnd_end = lc_mc_search->iovector[lc_index * 2 + 1];
  284. #endif /* SEARCH_TYPE_GLIB */
  285. if (fnd_end - fnd_start == 0)
  286. return NULL;
  287. return g_strndup (lc_mc_search->regex_buffer->str + fnd_start, fnd_end - fnd_start);
  288. }
  289. /* --------------------------------------------------------------------------------------------- */
  290. static int
  291. mc_search_regex__process_replace_str (const GString * replace_str, const gsize current_pos,
  292. gsize * skip_len, replace_transform_type_t * replace_flags)
  293. {
  294. int ret = -1;
  295. char *tmp_str;
  296. const char *curr_str = &(replace_str->str[current_pos]);
  297. if (current_pos > replace_str->len)
  298. return -1;
  299. *skip_len = 0;
  300. if (*curr_str == '$' && *(curr_str + 1) == '{' && (*(curr_str + 2) & (char) 0xf0) == 0x30) {
  301. if (strutils_is_char_escaped (replace_str->str, curr_str)) {
  302. *skip_len = 1;
  303. return -1;
  304. }
  305. for (*skip_len = 0;
  306. current_pos + *skip_len + 2 < replace_str->len
  307. && (*(curr_str + 2 + *skip_len) & (char) 0xf0) == 0x30; (*skip_len)++);
  308. if (*(curr_str + 2 + *skip_len) != '}')
  309. return -1;
  310. tmp_str = g_strndup (curr_str + 2, *skip_len);
  311. if (tmp_str == NULL)
  312. return -1;
  313. ret = atoi (tmp_str);
  314. g_free (tmp_str);
  315. *skip_len += 3; /* ${} */
  316. return ret;
  317. }
  318. if (*curr_str == '\\') {
  319. if (strutils_is_char_escaped (replace_str->str, curr_str)) {
  320. *skip_len = 1;
  321. return -1;
  322. }
  323. if ((*(curr_str + 1) & (char) 0xf0) == 0x30) {
  324. ret = *(curr_str + 1) - '0';
  325. *skip_len = 2; /* \\ and one digit */
  326. return ret;
  327. }
  328. ret = -2;
  329. *skip_len += 2;
  330. switch (*(curr_str + 1)) {
  331. case 'U':
  332. *replace_flags |= REPLACE_T_UPP_TRANSFORM;
  333. *replace_flags &= ~REPLACE_T_LOW_TRANSFORM;
  334. break;
  335. case 'u':
  336. *replace_flags |= REPLACE_T_UPP_TRANSFORM_CHAR;
  337. break;
  338. case 'L':
  339. *replace_flags |= REPLACE_T_LOW_TRANSFORM;
  340. *replace_flags &= ~REPLACE_T_UPP_TRANSFORM;
  341. break;
  342. case 'l':
  343. *replace_flags |= REPLACE_T_LOW_TRANSFORM_CHAR;
  344. break;
  345. case 'E':
  346. *replace_flags = REPLACE_T_NO_TRANSFORM;
  347. break;
  348. default:
  349. ret = -1;
  350. break;
  351. }
  352. }
  353. return ret;
  354. }
  355. static void
  356. mc_search_regex__process_append_str (GString * dest_str, const char *from, gsize len,
  357. replace_transform_type_t * replace_flags)
  358. {
  359. gsize loop = 0;
  360. gsize char_len;
  361. char *tmp_str;
  362. GString *tmp_string;
  363. if (len == (gsize) -1)
  364. len = strlen (from);
  365. if (*replace_flags == REPLACE_T_NO_TRANSFORM) {
  366. g_string_append_len (dest_str, from, len);
  367. return;
  368. }
  369. while (loop < len) {
  370. tmp_str = mc_search__get_one_symbol (NULL, from + loop, len - loop, NULL);
  371. char_len = strlen (tmp_str);
  372. if (*replace_flags & REPLACE_T_UPP_TRANSFORM_CHAR) {
  373. *replace_flags &= !REPLACE_T_UPP_TRANSFORM_CHAR;
  374. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  375. g_string_append (dest_str, tmp_string->str);
  376. g_string_free (tmp_string, TRUE);
  377. } else if (*replace_flags & REPLACE_T_LOW_TRANSFORM_CHAR) {
  378. *replace_flags &= !REPLACE_T_LOW_TRANSFORM_CHAR;
  379. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  380. g_string_append (dest_str, tmp_string->str);
  381. g_string_free (tmp_string, TRUE);
  382. } else if (*replace_flags & REPLACE_T_UPP_TRANSFORM) {
  383. tmp_string = mc_search__toupper_case_str (NULL, tmp_str, char_len);
  384. g_string_append (dest_str, tmp_string->str);
  385. g_string_free (tmp_string, TRUE);
  386. } else if (*replace_flags & REPLACE_T_LOW_TRANSFORM) {
  387. tmp_string = mc_search__tolower_case_str (NULL, tmp_str, char_len);
  388. g_string_append (dest_str, tmp_string->str);
  389. g_string_free (tmp_string, TRUE);
  390. } else {
  391. g_string_append (dest_str, tmp_str);
  392. }
  393. g_free (tmp_str);
  394. loop += char_len;
  395. }
  396. }
  397. /*** public functions ****************************************************************************/
  398. void
  399. mc_search__cond_struct_new_init_regex (const char *charset, mc_search_t * lc_mc_search,
  400. mc_search_cond_t * mc_search_cond)
  401. {
  402. GString *tmp = NULL;
  403. #ifdef SEARCH_TYPE_GLIB
  404. GError *error = NULL;
  405. #else /* SEARCH_TYPE_GLIB */
  406. const char *error;
  407. int erroffset;
  408. #endif /* SEARCH_TYPE_GLIB */
  409. if (!lc_mc_search->is_case_sentitive) {
  410. tmp = g_string_new_len (mc_search_cond->str->str, mc_search_cond->str->len);
  411. g_string_free (mc_search_cond->str, TRUE);
  412. mc_search_cond->str = mc_search__cond_struct_new_regex_ci_str (charset, tmp->str, tmp->len);
  413. g_string_free (tmp, TRUE);
  414. }
  415. #ifdef SEARCH_TYPE_GLIB
  416. mc_search_cond->regex_handle =
  417. g_regex_new (mc_search_cond->str->str, G_REGEX_OPTIMIZE | G_REGEX_RAW | G_REGEX_DOTALL, 0,
  418. &error);
  419. if (error != NULL) {
  420. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  421. lc_mc_search->error_str = str_conv_gerror_message (error, _(" Regular expression error "));
  422. g_error_free (error);
  423. return;
  424. }
  425. #else /* SEARCH_TYPE_GLIB */
  426. mc_search_cond->regex_handle =
  427. pcre_compile (mc_search_cond->str->str, PCRE_EXTRA, &error, &erroffset, NULL);
  428. if (mc_search_cond->regex_handle == NULL) {
  429. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  430. lc_mc_search->error_str = g_strdup (error);
  431. return;
  432. }
  433. lc_mc_search->regex_match_info = pcre_study (mc_search_cond->regex_handle, 0, &error);
  434. if (lc_mc_search->regex_match_info == NULL) {
  435. if (error) {
  436. lc_mc_search->error = MC_SEARCH_E_REGEX_COMPILE;
  437. lc_mc_search->error_str = g_strdup (error);
  438. g_free (mc_search_cond->regex_handle);
  439. mc_search_cond->regex_handle = NULL;
  440. return;
  441. }
  442. }
  443. #endif /* SEARCH_TYPE_GLIB */
  444. }
  445. /* --------------------------------------------------------------------------------------------- */
  446. gboolean
  447. mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data,
  448. gsize start_search, gsize end_search, gsize * found_len)
  449. {
  450. gsize current_pos, virtual_pos;
  451. int current_chr = 0;
  452. gint start_pos;
  453. gint end_pos;
  454. if (lc_mc_search->regex_buffer != NULL)
  455. g_string_free (lc_mc_search->regex_buffer, TRUE);
  456. lc_mc_search->regex_buffer = g_string_new ("");
  457. virtual_pos = current_pos = start_search;
  458. while (virtual_pos <= end_search) {
  459. g_string_set_size (lc_mc_search->regex_buffer, 0);
  460. lc_mc_search->start_buffer = current_pos;
  461. while (1) {
  462. current_chr = mc_search__get_char (lc_mc_search, user_data, current_pos);
  463. if (current_chr == MC_SEARCH_CB_ABORT)
  464. break;
  465. current_pos++;
  466. if (current_chr == MC_SEARCH_CB_SKIP)
  467. continue;
  468. virtual_pos++;
  469. g_string_append_c (lc_mc_search->regex_buffer, (char) current_chr);
  470. if (current_chr == 0 || (char) current_chr == '\n')
  471. break;
  472. if (virtual_pos > end_search)
  473. break;
  474. }
  475. switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer)) {
  476. case COND__FOUND_OK:
  477. #ifdef SEARCH_TYPE_GLIB
  478. g_match_info_fetch_pos (lc_mc_search->regex_match_info, 0, &start_pos, &end_pos);
  479. #else /* SEARCH_TYPE_GLIB */
  480. start_pos = lc_mc_search->iovector[0];
  481. end_pos = lc_mc_search->iovector[1];
  482. #endif /* SEARCH_TYPE_GLIB */
  483. if (found_len)
  484. *found_len = end_pos - start_pos;
  485. lc_mc_search->normal_offset = lc_mc_search->start_buffer + start_pos;
  486. return TRUE;
  487. break;
  488. case COND__NOT_ALL_FOUND:
  489. break;
  490. default:
  491. g_string_free (lc_mc_search->regex_buffer, TRUE);
  492. lc_mc_search->regex_buffer = NULL;
  493. return FALSE;
  494. break;
  495. }
  496. if (( lc_mc_search->update_fn != NULL ) &&
  497. ( (lc_mc_search->update_fn) (user_data, current_pos) == MC_SEARCH_CB_ABORT))
  498. current_chr = MC_SEARCH_CB_ABORT;
  499. if (current_chr == MC_SEARCH_CB_ABORT)
  500. break;
  501. }
  502. g_string_free (lc_mc_search->regex_buffer, TRUE);
  503. lc_mc_search->regex_buffer = NULL;
  504. lc_mc_search->error = MC_SEARCH_E_NOTFOUND;
  505. if (current_chr != MC_SEARCH_CB_ABORT)
  506. lc_mc_search->error_str = g_strdup (_(STR_E_NOTFOUND));
  507. else
  508. lc_mc_search->error_str = NULL;
  509. return FALSE;
  510. }
  511. /* --------------------------------------------------------------------------------------------- */
  512. GString *
  513. mc_search_regex_prepare_replace_str (mc_search_t * lc_mc_search, GString * replace_str)
  514. {
  515. GString *ret;
  516. gchar *tmp_str;
  517. int num_replace_tokens, lc_index;
  518. gsize loop;
  519. gsize len = 0;
  520. gchar *prev_str;
  521. replace_transform_type_t replace_flags = REPLACE_T_NO_TRANSFORM;
  522. num_replace_tokens =
  523. mc_search_regex__get_max_num_of_replace_tokens (replace_str->str, replace_str->len);
  524. if (lc_mc_search->num_rezults < 0)
  525. return g_string_new_len (replace_str->str, replace_str->len);
  526. if (num_replace_tokens > lc_mc_search->num_rezults - 1
  527. || num_replace_tokens > MC_SEARCH__NUM_REPLACE_ARGS) {
  528. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  529. lc_mc_search->error_str = g_strdup (STR_E_RPL_NOT_EQ_TO_FOUND);
  530. return NULL;
  531. }
  532. ret = g_string_new ("");
  533. prev_str = replace_str->str;
  534. for (loop = 0; loop < replace_str->len - 1; loop++) {
  535. lc_index = mc_search_regex__process_replace_str (replace_str, loop, &len, &replace_flags);
  536. if (lc_index == -1) {
  537. if (len != 0) {
  538. mc_search_regex__process_append_str (ret, prev_str,
  539. replace_str->str - prev_str + loop,
  540. &replace_flags);
  541. mc_search_regex__process_append_str (ret, replace_str->str + loop + 1, len - 1,
  542. &replace_flags);
  543. prev_str = replace_str->str + loop + len;
  544. loop += len - 1;
  545. }
  546. continue;
  547. }
  548. if (lc_index == -2) {
  549. if (loop)
  550. mc_search_regex__process_append_str (ret, prev_str,
  551. replace_str->str - prev_str + loop,
  552. &replace_flags);
  553. prev_str = replace_str->str + loop + len;
  554. loop += len - 1;
  555. continue;
  556. }
  557. if (lc_index > lc_mc_search->num_rezults) {
  558. g_string_free (ret, TRUE);
  559. lc_mc_search->error = MC_SEARCH_E_REGEX_REPLACE;
  560. lc_mc_search->error_str = g_strdup_printf (STR_E_RPL_INVALID_TOKEN, lc_index);
  561. return NULL;
  562. }
  563. tmp_str = mc_search_regex__get_token_by_num (lc_mc_search, lc_index);
  564. if (tmp_str == NULL)
  565. continue;
  566. if (loop)
  567. mc_search_regex__process_append_str (ret, prev_str, replace_str->str - prev_str + loop,
  568. &replace_flags);
  569. prev_str = replace_str->str + loop + len;
  570. mc_search_regex__process_append_str (ret, tmp_str, -1, &replace_flags);
  571. g_free (tmp_str);
  572. loop += len - 1;
  573. }
  574. mc_search_regex__process_append_str (ret, prev_str,
  575. replace_str->str - prev_str + replace_str->len,
  576. &replace_flags);
  577. return ret;
  578. }