Removing Unprintable characters
Posted: Tue Dec 18, 2007 8:31 am
Hi,
How to remove unprintable characters in PX.
How to remove unprintable characters in PX.
Code: Select all
If InputCol < Char(32)
Then <InputCol>
Else <something>
Function pxRemoveNPChars can be called with or without a second argument ie. a replace character. If replace character is not specified - remove all happens.#define NP_RANGE_START 32 // change 32 to your value
#define NP_RANGE_END 126 // change 126 to your value
Code: Select all
#include<stdio.h>
#include<unistd.h> // for exit
#include<stdlib.h>
#include<string.h>
#define NP_RANGE_START 32
#define NP_RANGE_END 126
bool IsCharNP( char InCharToCheck )
{
bool IsNotPrintable = false;
//#if DEBUG
// printf("\nChecking %c %d",InCharToCheck, InCharToCheck);
//#endif
if( (InCharToCheck > NP_RANGE_START ) && ( InCharToCheck < NP_RANGE_END ))
{
IsNotPrintable = false;
}
else
{
IsNotPrintable = true;
}
//#if DEBUG
// printf("%s printable char", IsNotPrintable ? " NOT a " : " IS a ");
//#endif
return IsNotPrintable;
}
void Abort(char *MsgToAbortWith)
{
//printf("[Abort] %s \n", MsgToAbortWith);
return MsgToAbortWith;
}
char *GetNewString(int InLen)
{
char *StrInMem = (char *) malloc(InLen);
if( NULL != StrInMem )
return StrInMem;
else
Abort("Insufficient memory - malloc failed");
}
char *RemoveNPCharsWithoutRep(char *StrToRemove)
{
int NewStrIndex = 0;
char *TmpStr = GetNewString( strlen( StrToRemove) + 1);
for( int index = 0; StrToRemove[index] ; index++)
{
/* Check if it is a Non Printable char ? */
if( true == IsCharNP(StrToRemove[index]) )
continue;
TmpStr[NewStrIndex++] = StrToRemove[index];
}
TmpStr[NewStrIndex] = '\0';
return TmpStr;
}
char* RemoveNPCharsWithRep(char *StrToRemove, char CharToReplace)
{
for( int index = 0; StrToRemove[index] ; index++)
{
/* Check if it is a Non Printable char ? */
if( true == IsCharNP(StrToRemove[index]) )
{
StrToRemove[index] = CharToReplace;
}
else
continue;
}
return StrToRemove;
}
/*
* Function : [b]pxRemoveNPChars[/b] * Input : char * InStrToRemove - IN - the input string
* Input : char ReplaceChar - IN - the char to replace OR * if none
* Description : This function removes/replaces all the Non printable chars with the given input.
* Note : Should be called with writable memory locations. Calling this function with read-only input like "Abcdefg" would fail.
* Should be called with char Test[100] = "Abcdefg". RemoveNPChars(Test) and not RemoveNPChars("Abcdefg");
* TODO : To be tested thorougly before use
*
* */
char *pxRemoveNPChars( char *InStrToRemove , char ReplaceChar = '*')
{
char *StrToRet = NULL;
/* First check whether the Input string is valid ? */
if( NULL == InStrToRemove )
{
/* The Input was NULL , do nothing and return NULL indicating error to caller */
return StrToRet;
}
if( '*' == ReplaceChar )
{
/* User does not want to replace NP chars with anything */
char *NewString = RemoveNPCharsWithoutRep(InStrToRemove);
StrToRet = NewString;
}
else
{
StrToRet = RemoveNPCharsWithRep(InStrToRemove, ReplaceChar);
}
return StrToRet;
}
in the above code and add below code as the last block#define NP_RANGE_START 70
Code: Select all
/* The printable chars have been moved from 32 to 70 for testing purposes only */
int main()
{
char TestBuffer[]="ABCDmithAAAAAbcccABCqwwwwweehfsAqAe";
printf("Input - %s \n", TestBuffer);
char *TmpStr = RemoveNPChars(TestBuffer,'*');
printf("\nReturned string = %s\n", TmpStr);
TmpStr = RemoveNPChars(TestBuffer,'$');
printf("\nReturned string = %s\n", TmpStr);
}
And eastern Australians for 'flood' as well, I understand.ray.wurlod wrote:I heard today that Massachusetts residents currently have 37 words for snow, not one of which is printable!
![]()
Function : pxRemoveNPChars *
Input : char * InStrToRemove - IN - the input string
Input : char * ReplaceChar - IN - the char to replace OR '' (empty) if none ie. for remove all.
Description : This function removes/replaces all the Non printable chars with the given input.
Code: Select all
#include<stdio.h>
#include<unistd.h> // for exit
#include<stdlib.h>
#include<string.h>
#define NP_RANGE_START 32
#define NP_RANGE_END 126
bool IsCharNP( char InCharToCheck )
{
bool IsNotPrintable = false;
if( (InCharToCheck > NP_RANGE_START ) && ( InCharToCheck < NP_RANGE_END ))
{
IsNotPrintable = false;
}
else
{
IsNotPrintable = true;
}
return IsNotPrintable;
}
char *GetNewString(int InLen)
{
char *StrInMem = (char *) malloc(InLen);
if( NULL != StrInMem )
return StrInMem;
else
abort();
}
char *RemoveNPCharsWithoutRep(char *StrToRemove)
{
int NewStrIndex = 0;
char *TmpStr = GetNewString( strlen( StrToRemove) + 1);
for( int index = 0; StrToRemove[index] ; index++)
{
/* Check if it is a Non Printable char ? */
if( true == IsCharNP(StrToRemove[index]) )
continue;
TmpStr[NewStrIndex++] = StrToRemove[index];
}
TmpStr[NewStrIndex] = '\0';
return TmpStr;
}
char* RemoveNPCharsWithRep(char *StrToRemove, char CharToReplace)
{
for( int index = 0; StrToRemove[index] ; index++)
{
/* Check if it is a Non Printable char ? */
if( true == IsCharNP(StrToRemove[index]) )
{
StrToRemove[index] = CharToReplace;
}
else
continue;
}
return StrToRemove;
}
/*
* Function : [b]pxRemoveNPChars[/b] * Input : char * InStrToRemove - IN - the input string
* Input : char ReplaceChar - IN - the char to replace OR '' (empty) if none
* Description : This function removes/replaces all the Non printable chars with the given input.
*/
char *pxRemoveNPChars( char *InStrToRemove , char *ReplaceChar)
{
char *StrToRet = NULL;
/* First check whether the Input string is valid ? */
if (( NULL == InStrToRemove )) // || (strlen(InStrToRemove) < 1))
{
/* The Input was NULL , do nothing and return NULL indicating error to caller */
return StrToRet;
}
if(( NULL == ReplaceChar) ) // || (strlen(ReplaceChar) < 1) )
{
/* User does not want to replace NP chars with anything */
char *NewString = RemoveNPCharsWithoutRep(InStrToRemove);
StrToRet = NewString;
}
else
{ StrToRet = RemoveNPCharsWithRep(InStrToRemove, ReplaceChar[0]); }
return StrToRet;
}