schoeppchen
IS-IT--Management
Hi there,
I found a str-replace Function but it does not work ... any hints why not? It always returns the string without replacing...
-x-x-x-
#include "postgres.h"
#include <string.h>
char * pgsql_strreplace (char * string,char * search,char *replace)
{
char * cp = string;
char * s1;
char * s2;
register int z1=0;
char *tmp = malloc (strlen(string) - strlen(search) + strlen(replace)+1);
if (!*search) return string;
while (*cp)
{
s1 = cp;
s2 = search;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
// substring found
if (!*s2) {
// Replace String attached
tmp=strcat(tmp,replace);
cp+=strlen(search);
tmp=strcat(tmp,cp);
strcpy(string,tmp);
free(tmp);
// call recursive to replace multiple entries
string=pgsql_strreplace(string,search,replace);
return string;
}
tmp[z1]=string[z1];
tmp[z1+1]=0;
cp++;
z1++;
}
free(tmp);
return string;
}
-x-x-x-
I found a str-replace Function but it does not work ... any hints why not? It always returns the string without replacing...
-x-x-x-
#include "postgres.h"
#include <string.h>
char * pgsql_strreplace (char * string,char * search,char *replace)
{
char * cp = string;
char * s1;
char * s2;
register int z1=0;
char *tmp = malloc (strlen(string) - strlen(search) + strlen(replace)+1);
if (!*search) return string;
while (*cp)
{
s1 = cp;
s2 = search;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
// substring found
if (!*s2) {
// Replace String attached
tmp=strcat(tmp,replace);
cp+=strlen(search);
tmp=strcat(tmp,cp);
strcpy(string,tmp);
free(tmp);
// call recursive to replace multiple entries
string=pgsql_strreplace(string,search,replace);
return string;
}
tmp[z1]=string[z1];
tmp[z1+1]=0;
cp++;
z1++;
}
free(tmp);
return string;
}
-x-x-x-