6557
6569
icase - flag, if set to 1 the match is case insensitive
6559
6571
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6560
char *replace, char *in_string, int icase)
6572
char *replace, char *in_string, int icase, int global)
6562
string string_to_match(in_string);
6563
6574
const char *error= NULL;
6565
6576
int ovector[3];
6566
6577
pcre *re= pcre_compile(pattern,
6567
icase ? PCRE_CASELESS : 0,
6578
icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
6568
6579
&error, &erroffset, NULL);
6569
6580
if (re == NULL)
6572
int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
6580
char *substring_to_replace= in_string + ovector[0];
6581
int substring_length= ovector[1] - ovector[0];
6582
*buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6583
char * new_buf = (char *)malloc(*buf_len_p+1);
6584
if (new_buf == NULL)
6590
memset(new_buf, 0, *buf_len_p+1);
6591
strncpy(new_buf, in_string, substring_to_replace-in_string);
6592
strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
6593
strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
6594
substring_to_replace + substring_length,
6597
- (substring_to_replace-in_string));
6586
int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
6594
char *substring_to_replace= in_string + ovector[0];
6595
int substring_length= ovector[1] - ovector[0];
6596
*buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6597
char * new_buf = (char *)malloc(*buf_len_p+1);
6598
if (new_buf == NULL)
6604
memset(new_buf, 0, *buf_len_p+1);
6605
strncpy(new_buf, in_string, substring_to_replace-in_string);
6606
strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
6607
strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
6608
substring_to_replace + substring_length,
6611
- (substring_to_replace-in_string));
6619
/* Repeatedly replace the string with the matched regex */
6620
string subject(in_string);
6621
size_t replace_length= strlen(replace);
6622
size_t current_position= 0;
6624
while(0 >= (rc= pcre_exec(re, NULL, subject.c_str() + current_position, subject.length() - current_position,
6627
current_position= static_cast<size_t>(ovector[0]);
6628
replace_length= static_cast<size_t>(ovector[1] - ovector[0]);
6629
subject.replace(current_position, replace_length, replace, replace_length);
6632
char *new_buf = (char *) malloc(subject.length() + 1);
6633
if (new_buf == NULL)
6638
memset(new_buf, 0, subject.length() + 1);
6639
strncpy(new_buf, subject.c_str(), subject.length());
6640
*buf_len_p= subject.length() + 1;