06-29-2011 08:07 AM
Hi, I'm writing a function to actively remove unwanted characters from an input field. It will run every time a key press event occurs on the selected input fields. It is going to remove quotes and things like that. This is what I have so far..
void test(){
char unwantedCharSet[50]={"abcD"};
char tempString2[200];
int matchedCharIndex;
GetCtrlVal (panelHandle[MAIN], MAIN_STRING,tempString);
do{
RemoveSurroundingWhiteSpace (tempString);
matchedCharIndex = strcspn (tempString, unwantedCharSet);
if(matchedCharIndex==strlen(tempString)){
SetCtrlVal (panelHandle[MAIN], MAIN_STRING, tempString);
return;
}
else{
strncpy (tempString2, tempString, matchedCharIndex);
CopyString (tempString, matchedCharIndex, tempString,matchedCharIndex+1,(strlen(tempString)-matchedCharIndex));
strcat (tempString2, tempString);
}
sprintf(tempString,"%s",tempString2);
} while(1);
return;
}
It is still glitchy. Does anyone have any advice to efficiently do this?
Thanks in advance!
Solved! Go to Solution.
06-29-2011 08:22 AM
Hi,
did you consider using regular expressions, via RegExpr_FindPatternInText ?
06-29-2011 08:32 AM
Thanks, thats a good one to remember. (I'm still very much in a learning phase)
I'm also trying to use what Al S suggested here.
void test(){
char otherString[50];
GetCtrlVal(panelHandle[MAIN],MAIN_STRING,tempString);
filter(tempString,otherString);
SetCtrlVal(panelHandle[MAIN],MAIN_STRING,otherString);
return;
}
int filter(char *inputString, char *outputString){
int inputPtr, outputPtr = 0;
for (inputPtr=0;inputPtr<strlen(inputString);inputPtr++,outputPtr++){
if (inputString[inputPtr]==41||inputString[inputPtr]==30){
inputPtr++; // increment the inputString pointer to skip the backspace
outputPtr--; // decrement the outputString pointer to overwrite
}
outputString[outputPtr]=inputString[inputPtr];
}
outputString[outputPtr]='\0';
return 0;
}
06-29-2011 01:40 PM
This was my solution, although, it takes a long time to perform. Does anyone have any tips on a better (faster/cleaner) approach?
/*=====================================================================*/
// TEST
/*=====================================================================*/
void test(){
GetCtrlVal(panelHandle[MAIN],MAIN_STRING,tempString);
SetCtrlVal(panelHandle[MAIN],MAIN_STRING,filter(tempString,unwantedCharSet));
return;
}
/*=====================================================================*/
// FILTER
/*=====================================================================*/
char *filter(char *inputString,char *filterString){
int ptr=0,matchedCharIndex=0;
char outputString[100];
do{
matchedCharIndex = strcspn (inputString,filterString);
if(matchedCharIndex!=strlen(tempString)){
strcpy (outputString, inputString);
for(ptr=matchedCharIndex;ptr<=strlen(inputString);ptr++){
outputString[ptr]=inputString[ptr+1];
DebugPrintf("%s\n",outputString);
if(outputString[ptr]=='\n'||outputString[ptr]=='\0')
break;
}
strcpy(inputString,outputString);
}
else
return inputString;
} while(1);
return inputString;
}