Hi,

Can anyboby help me.

I'm try to write a UDF which replace substring in string according regular
expression.
When I compile my program with -I ./../regex gcc switch I recive as result
"Segmentetion fault". But when I compile my program without -I ./../regex -
my program works fine. I think gcc found regex.h in /usr/include.

Hope someone can help me
Thank you in advance

Regards,
Vladislav

This is my source:
(test program with function main() )

#include <stdio.h>
#include <string.h>
#include <regex.h>

#define NS 10
char* reg_replace(const char *pattern, const char *replace, const char
*string, int icase, int extended);
char* regex_error(int err, regex_t *re);

int main()
{
    char* rez;
    rez = reg_replace("t", "test passed:", "htpt:///", 1, 0);
    printf("Result  =%s\n",rez);
    free(rez);
}

char* reg_replace(const char *pattern, const char *replace, const char
*string, int icase, int extended)
{
    regex_t* re;
    regmatch_t* subs;
    char *buf, /* buf is where we build the replaced string */
         *nbuf, /* nbuf is used when we grow the buffer */
         *walkbuf; /* used to walk buf when replacing backrefs */
    const char *walk; /* used to walk replacement string for backrefs */

    int buf_len;
    int pos, tmp, string_len, new_l;
    int tmp1;
    int err, copts = 0;

    string_len = strlen(string);

    if (icase)
 copts = REG_ICASE;
    if (extended)
 copts |= REG_EXTENDED;


    re = (regex_t*)malloc(sizeof(regex_t));

    err = regcomp(re, pattern, copts);
    if (err)
        return regex_error(err, re);

    buf = (char*) malloc(strlen(string)+1);

 /* start with a buffer that is twice the size of the stringo
    we're doing replacements in */
    buf_len = 2 * string_len + 1;
    buf = (char*) malloc(buf_len * sizeof(char));

    if (!buf) {
     regfree(re);
 return "Unable to allocate memory in php_reg_replace";
    }

    err = pos = 0;
    buf[0] = '\0';

    subs = (regmatch_t*)malloc(sizeof(regmatch_t)*NS);

// while (!err) {
    if (!err) {
     err = regexec(re, &string[pos], (size_t) NS, subs, 0);
 if (err && err != REG_NOMATCH) {
  regex_error(err, re);
  regfree(re);
  free(subs);
  free(re);
  return 0;
 }
 if (!err) {
     tmp = strlen(string);
     for( pos = 0; pos < NS; pos++) {
  if(subs[pos].rm_so !=-1) {
      tmp = tmp - (subs[pos].rm_eo - subs[pos].rm_so) + strlen(replace);
  }
  printf("check point:%i, value=%i\n",pos,subs[pos].rm_so);
     }
     printf("Result length = %i\n",tmp);
     buf = (char*) malloc(tmp+1);
     buf[tmp] = '\0';
         pos = tmp = tmp1 = 0;
            while(subs[pos].rm_so !=-1){
  printf("In str:%s, pattern:%s, replace:%s, start: %i, end: %i\n",
      string, pattern, replace, subs[pos].rm_so,subs[pos].rm_eo);
      memcpy(buf+tmp,string+tmp1,subs[pos].rm_so-tmp1);
  tmp += subs[pos].rm_so;
  tmp1 = subs[pos].rm_eo;
  memcpy(buf+tmp, replace, strlen(replace));
  tmp += strlen(replace);
  pos++;
     }
     memcpy(buf+tmp,string+tmp1,strlen(string)-tmp1);
  }
 }

 /* don't want to leak memory .. */
 regfree(re);
 free(subs);
 free(re);
 /* whew. */
 return buf;
}

char* regex_error(int err, regex_t *re) {

 char *buf = NULL, *message = NULL;
 size_t len;
 size_t buf_len;

 /* get the length of the message */
 buf_len = regerror(err, re, NULL, 0);

 if (buf_len) {
  buf = (char*) malloc(buf_len * sizeof(char));
  if (!buf) return "fail silently"; /* fail silently */
  /* finally, get the error message */
  regerror(err, re, buf, buf_len);
 }

 len = regerror(err, re, NULL, 0);
 if (len) {
  message = (char *)malloc((buf_len + len + 2) * sizeof(char));
  if (!message) {
   return "** fail silently"; /*  */
  }
  if (buf_len) {
   snprintf(message, buf_len, "%s: ", buf);
   buf_len += 1; /* so pointer math below works */
  }
  /* drop the message into place */
  regerror(err, re, message + buf_len, len);
 }
 free(buf);
 return message;
}



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to