Posted: Mon Nov 05, 2012 10:38 am
That's nice, but I'd still like to be able to implement the pxStrFilter routine that is in the third post of this thread.
A short text to describe your forum
http://dsxchange.com./
Code: Select all
/******************************************************************************
* pxEreplace - DataStage parallel routine
*
* Published on DSXchange.com by user DSguru2B
* http://www.dsxchange.com/viewtopic.php?t=106358
* http://www.dsxchange.com/viewtopic.php?t=147367&highlight=ereplace
*
* Bugs (malloc, realloc, count) fixed by Philip Hibbs, Capgemini
*
* INSTRUCTIONS
*
* 1. Copy the source file pxEreplace.cpp into a directory on the server
* 2. Run the following command:
*
* g++ -O -fPIC -Wno-deprecated -c pxEreplace.cpp
* Sun CC syntax
* /opt/SUNWspro/bin/CC -m64 -O -PIC -c pxEreplace_4.C -o pxEreplace_4.o
*
* (check Administrator->Properties->Environment->Parallel->Compiler settings)
*
* 3. Copy the output into the DataStage library directory:
*
* cp pxEreplace.o `cat /.dshome`/../PXEngine/lib/pxEreplace.o
* cp pxEreplace_4.o pxEreplace.o
*
* 4. Create the Server Routine with the following properties:
*
* Routine Name : pxEreplace
* External subroutine name : pxEreplace
* Type : External function
* Object type : Object
* Return type : char*
* Library path : /software/opt/IBM/InformationServer/Server/PXEngine/lib/pxEreplace.o
* Arguments:
* str I char*
* subStr I char*
* rep I char*
* num I int
* beg I int
*
* Save & Close
*
* Any time that anything changes, you must recompile all jobs that use the routine.
*
******************************************************************************/
#include "string.h"
#include "stdlib.h"
char* pxEreplace(char *str, char *subStr, char *rep, int num, int beg)
{
char empty[1]="";
if (!str) {str = empty;}
if (!subStr) {subStr = empty;}
if (!rep) {rep = empty;}
int buflen = strlen(str)+1;
char *result = (char *)malloc( buflen );
if (!result) {return 0;}
if (buflen==1) {result[0]='\0'; return result;}
int oldlen = strlen(subStr);
int newlen = strlen(rep);
int i, x, count = 0;
if (oldlen==0)
{ // special case - insert rep once at the start of the string and return
if (newlen>0)
{
buflen = buflen + newlen;
result = (char *)realloc( result, buflen );
}
strcpy(result, rep);
strcpy(result+newlen, str);
return result;
}
//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 = buflen;}
//Get the character position in i for substring instance to start from
for (i = 0; str[i] != '\0' ; i++)
{
if (strncmp(&str[i], subStr, oldlen) == 0)
{
count++;
if (count == beg) { break; }
i += oldlen - 1;
}
}
//Get everything before position i before replacement begins
x = 0;
while (i != x)
{ result[x++] = *str++; }
//Start replacement
while (*str) //for the complete input string
{
if (num != 0 ) // untill no more occurances need to be changed
{
if (strncmp(str, subStr, oldlen) == 0)
{
if (newlen > oldlen)
{
buflen = buflen + (newlen - oldlen);
result = (char *)realloc( result, buflen );
}
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
}