PxEreplace routine issue when calling through datastage
Moderators: chulett, rschirm, roy
PxEreplace routine issue when calling through datastage
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
}
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
}
Datastage User
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
resolved
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 ...
#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 ...
Datastage User
as per duke's suggestion
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
}
#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
}
Datastage User
-
- Participant
- Posts: 42
- Joined: Wed Dec 26, 2012 1:13 pm
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
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
-
- Premium Member
- Posts: 1735
- Joined: Thu Mar 01, 2007 5:44 am
- Location: Troy, MI
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
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
Priyadarshi Kunal
Genius may have its limitations, but stupidity is not thus handicapped.
Genius may have its limitations, but stupidity is not thus handicapped.
-
- Participant
- Posts: 42
- Joined: Wed Dec 26, 2012 1:13 pm
-
- Premium Member
- Posts: 1735
- Joined: Thu Mar 01, 2007 5:44 am
- Location: Troy, MI