As a result of a previous post (viewtopic.php?t=132126), I have received a few private messages asking me about a parallel routine that I have written to remove non ascii chars from an input string.
For anyone who does not know how to create parallel routines, I found the following website useful for getting started (http://it.toolbox.com/blogs/dw-soa/data ... easy-20926).
My C code to remove non ascii chars from a string is below;
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char * PRemoveNonAscii(char* varInStr)
{
int varStrLen; // Int to hold the char size of the incoming string. //
int varCount=0; // Int to be used as a counter. //
varStrLen=strlen(varInStr); // Calculate the length of the input string. //
char* varOutStr=(char*) malloc (varStrLen+1); // Create a pointer to a char array & allocate memory for the output string. //
while (*varInStr) // Loop through each char of the passed string until the null terminator is reached. //
{
if (isascii(*varInStr)) // Check to see if char is a valid ascii char. //
{
varOutStr[varCount]=*varInStr; // Char is a valid ascii char so write to output array. //
varCount++; // Increment array counter. //
}
varInStr++; // Move to next char in input string. //
}
varOutStr[varCount]='\0'; // Add a null terminator to the end of the output string. //
return varOutStr; // Return result. //
free(varInStr); // Free the memory allocated to the input string. //
free(varOutStr); // Free the memory allocated to the output string. //
}
PLEASE NOTE: I am not sure how DataStage handles memory allocation (specifically when it releases the memory allocated), which is why I have put two free commands at the end of my function (one for the input string and one for the output). I haven't had any issues using this function in our environment (DataStage 7.5.3), but we are yet to move it to production (as I thought it wise to have our IT department review this code before we migrate). I don't consider myself to be a professional C or C++ programmer.... so.... USE THIS CODE AT YOUR OWN RISK!
If anyone has any feedback on the above code, or an alternative way of achieving the same result, I would be interested in hearing your thoughts.