Parallel routines and emtpy strings

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Parallel routines and emtpy strings

Post by PhilHibbs »

Here is my routine:

Code: Select all

/******************************************************************************
* 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:

Code: Select all

return strlen(str);
then it returns a 0 to my job.

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.
Phil Hibbs | Capgemini
Technical Consultant
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

Out of curiousity, Phil, what UNIX and C++ compiler is this?
-craig

"You can never have too many knives" -- Logan Nine Fingers
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

chulett wrote:Out of curiousity, Phil, what UNIX and C++ compiler is this?
Z/Linux and g++
Phil Hibbs | Capgemini
Technical Consultant
jiegao
Premium Member
Premium Member
Posts: 46
Joined: Fri Sep 22, 2006 6:12 pm

Post by jiegao »

PhilHibbs, is there a specific reason you're passing to pxStrFirstCharList by reference and not by value?
Regards
Jie
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

Because that's how C strings work, they are char pointers.
Phil Hibbs | Capgemini
Technical Consultant
PhilHibbs
Premium Member
Premium Member
Posts: 1044
Joined: Wed Sep 29, 2004 3:30 am
Location: Nottingham, UK
Contact:

Post by PhilHibbs »

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.

Code: Select all

/******************************************************************************
* 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;
}
Phil Hibbs | Capgemini
Technical Consultant
Post Reply