/******************************************************************************
* pxStrFirstCharList - DataStage parallel routine
*
* 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 pxStrFirstCharList.cpp
*
* (check Administrator->Properties->Environment->Parallel->Compiler settings)
*
* 3. Copy the output into the DataStage library directory:
*
* cp pxEreplace.o `cat /.dshome`/../PXEngine/lib/pxStringFilter.o
*
* 4. Create the Server Routine with the following properties:
*
* Routine Name : pxStrFirstCharList
* External subroutine name : pxStrFirstCharList
* Type : External function
* Object type : Object
* Return type : char*
* Library path : /software/opt/IBM/InformationServer/Server/PXEngine/lib/pxStrFirstCharList.o
* Arguments:
* str I char*
* chars I char*
*
* Save & Close
*
* Any time that anything changes, you must recompile all jobs that use the routine.
*
******************************************************************************/
#include "string.h"
#include "stdlib.h"
/******************************************************************************
* pxStrFirstCharList
*
* Finds the first character in a list of characters
* e.g. pxStrFirstCharList( "ABC321", "1234567890" ) returns 4
*
******************************************************************************/
int pxStrFirstCharList(char *str, char *chars )
{
if (strlen(str)==0) {return 0;}
if (strlen(chars)==0) {return 0;}
int i = 0;
//Start search
while (str[i]) //for the complete input string
{
if (strchr(chars, str[i]))
{
return i+1;
}
++i;
}
return 0;
}
If the str parameter is an empty string, the job never finishes, and I get a SIGSEGV error in the job log. It should just return 0 on the first line if str is an empty string, though.
If I add this line at the very start of the function:
So, strlen(str) is 0 but strlen(str)==0 does not cause it to return 0. Baffled.
Any ideas? My workaround is to just concatenate a dummy character that is not in the chars list to the end of the first parameter every time I call the routine.
I have found the problem. DataStage passes a null pointer if the string is empty. I need to check all the strings passed for being null. Here is the code.
/******************************************************************************
* pxStrFirstCharList - DataStage parallel routine
*
* 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 pxStrFirstCharList.cpp
*
* (check Administrator->Properties->Environment->Parallel->Compiler settings)
*
* 3. Copy the output into the DataStage library directory:
*
* cp pxStrFirstCharList.o `cat /.dshome`/../PXEngine/lib/pxStrFirstCharList.o
*
* 4. Create the Server Routine with the following properties:
*
* Routine Name : pxStrFirstCharList
* External subroutine name : pxStrFirstCharList
* Type : External function
* Object type : Object
* Return type : char*
* Library path : /software/opt/IBM/InformationServer/Server/PXEngine/lib/pxStrFirstCharList.o
* Arguments:
* str I char*
* chars I char*
*
* Save & Close
*
* Any time that anything changes, you must recompile all jobs that use the routine.
*
******************************************************************************/
#include "string.h"
#include "stdlib.h"
/******************************************************************************
* pxStrFirstCharList
*
* Finds the first character in a list of characters
*
******************************************************************************/
int pxStrFirstCharList(char *str, char *chars )
{
if (!str) {return -1;}
if (!chars) {return -2;}
if (strlen(str)==0) {return 0;}
if (strlen(chars)==0) {return 0;}
int i = 0;
//Start search
while (str[i]) //for the complete input string
{
if (strchr(chars, str[i]))
{
return i+1;
}
++i;
}
return 0;
}