Page 1 of 1

PxEreplace routine issue when calling through datastage

Posted: Wed Aug 05, 2009 3:24 pm
by krishna81
Hi i tried below routine which suppose to replace string with given substring but and i have an issue with this.i am ble to process less volune of records(ex:1000) and when i ran with huge volume(ex:15 million)of records it is hanging and keep running never finished and no warnings.is there any buffer issue in this c program
i appreciate in advance for your valuble suggestions.


#include "stdio.h"
#include "string.h"
#include "stdlib.h"

char* pxEreplace(char *str, char *subStr, char *rep, int num, int beg)
{
char *result = (char *)malloc (sizeof(char *));
int newlen = strlen(rep);
int oldlen = strlen(subStr);
int i, x, count = 0;

//If begining is less than or equal to 1 then default it to 1
if (beg <= 1)
{beg = 1;}

//replace all instances if value of num less than or equal to 0
if (num <= 0)
{num = strlen(str);}

//Get the character position in i for substring instance to start from
for (i = 0; str != '\0' ; i++)
{
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}
}

//Get everything before position i before replacement begins

x = 0;
while (i == x) //replace from starting//
{ result[x++] = *str++; }

//Start replacement
while (*str) //for the complete input string
{

if (num != 0 ) // untill no more occurances need to be changed
{
if (strstr(str, subStr) == str )
{
strcpy(&result[x], rep);
x += newlen;
str += oldlen;
num--;
}
else // if no match is found
{
result[x++] = *str++;
}
}
else
{
result[x++] = *str++;
}
}

result[x] = '\0'; //Terminate the string
return result; //Return the replaced string
}

pxEreplace issue

Posted: Wed Aug 05, 2009 7:06 pm
by krishna81
can any post your thoughts here.

Posted: Wed Aug 05, 2009 10:27 pm
by chulett
Patience. If anyone has any, they will.

Posted: Thu Aug 06, 2009 2:09 am
by Sainath.Srinivasan
Initial looks suggest possible memory allocation problem due to malloc.

Try setting that as result[1000] or your maximum string length.

Alternatively you can use another variable at the end to hold the return value and free the result.

Posted same in FAQ also.

resolved

Posted: Thu Aug 06, 2009 2:05 pm
by krishna81
it worked for men now for huge records by removing malloc and replaced with dynamic array.

#include "iostream.h"
#include "string.h"
#include "stdlib.h"

char* PrStrRep( char *InStr, char *SubStr, char *Replace, int Occurance, int Starting)
{

char *result = new char[2000];
size_t newlen = strlen(Replace);
size_t oldlen = strlen(SubStr);
int i, x, count = 0;

so on ...

Posted: Thu Aug 06, 2009 3:22 pm
by kduke
Why not post the complete source so others can benefit. Please put code tags around it.

as per duke's suggestion

Posted: Thu Aug 06, 2009 4:09 pm
by krishna81
Here is the complete code

#include "iostream.h"
#include "string.h"
#include "stdlib.h"

char* PrStrRep( char *InStr, char *SubStr, char *Replace, int Occurance, int Starting)
{

char *result = new char[2000];
size_t newlen = strlen(Replace);
size_t oldlen = strlen(SubStr);
int i, x, count = 0;
//If begining is less than or equal to 1 then default it to 1
if (beg <= 1)
{beg = 1;}

//replace all instances if value of num less than or equal to 0
if (num <= 0)
{num = strlen(str);}

//Get the character position in i for substring instance to start from
for (i = 0; str != '\0' ; i++)
{
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}
}

//Get everything before position i before replacement begins

x = 0;
while (i == x) //replace from starting//
{ result[x++] = *str++; }

//Start replacement
while (*str) //for the complete input string
{

if (num != 0 ) // untill no more occurances need to be changed
{
if (strstr(str, subStr) == str )
{
strcpy(&result[x], rep);
x += newlen;
str += oldlen;
num--;
}
else // if no match is found
{
result[x++] = *str++;
}
}
else
{
result[x++] = *str++;
}
}

result[x] = '\0'; //Terminate the string
return result; //Return the replaced string
}

Posted: Wed Oct 08, 2014 1:41 am
by gowrishankar_h
Thanks for the code.

substring replace is working when the substring is present in middle of the string.but its not working when it is the starting word of the string.

Example:

Replace substring which is in middle of the string

Source before replace: ETL is good tool
Doing replace good to very good
Target after replace :ETL is very good tool


Replace substring which is starting word of a string

Source before replace: ETL is good tool
Doing replace ETL to Datastage
Target after replace :ETL is good tool

In the above scenario its not working... Not able to find where its breaking.Please help me to fix this.


Thanks,
Gowri Shankar H

Posted: Wed Oct 08, 2014 3:54 am
by priyadarshikunal
A resolved topic doesn't get much attention, so you should start a new topic and link back to this post.

Did you try the one posted by Phil Hibbs with the changes from malloc to new as done by Krishna?

See the last post for the code.

viewtopic.php?t=106358&postdays=0&posto ... e&start=30

Posted: Wed Oct 08, 2014 5:53 am
by gowrishankar_h
Thanks a lot...It worked for me 8)

Issue is with the below code

Code:
if (strstr(&str, subStr) == &str)
{
count++;
i += oldlen - 1;
if (count == beg)
{break;}
}


I changed it to this:
Code:
if (strstr(&str, subStr) == &str)
{
count++;
if (count == beg)
{break;}
i += oldlen - 1;
}

Posted: Wed Oct 08, 2014 6:36 am
by qt_ky
Or you can upgrade to 9.x or higher and use the built in ereplace function.

Posted: Thu Oct 09, 2014 5:28 am
by priyadarshikunal
Good that you got it resolved. This bug was already identified by Phil, may not be the same scenario but he has it fixed in his code posted.