An alternative would be a simple function that iterates over the source string copying characters that are not to be removed to a destination string as in the following.
char * CopyStringRemove(char *pDest, const char *pSrc)
{
// copy the source string, pSrc, to the destination string, pDest, while
// removing special codes that are between a < character and a | character.
// we will copy the two special code characters but remove everything in between.
char * pRet = pDest;
if (pDest) {
if (pSrc) {
int iState = 0; // state indicates whether copying characters or not.
for (; *pSrc; pSrc++) {
switch (*pSrc) {
case '<':
iState = 1; // indicate we are skipping characters
*pDest++ = *pSrc; // however copy this character we found
break;
case '|':
iState = 0; // indicate we are copying characters
break;
default:
break;
}
switch (iState) {
case 0:
*pDest++ = *pSrc; // state is to copy the current character
break;
case 1: // state is to not copy current character, just skip over it.
break;
}
}
}
*pDest = 0;
}
return pRet;
}
This function provides quite a bit of flexibility in that the source can be a constant or not. The destination may be an array on the stack or an array malloced from the heap. If the source array is not const
then you can do an inplace change by calling the CopyStringRemove()
function with both source and destination being the same buffer.
It also allows for problems with the input such as not having a '<' character or a '|' character in the string.
A test harness such as:
void testfunc(const char *buff)
{
{
char destbuff[128] = { 0 };
printf(" orig string \"%s\"\n", buff);
CopyStringRemove(destbuff, buff);
printf(" new \"%s\"\n", destbuff);
}
{
char destbuff[128] = { 0 };
char buff2[128] = { 0 };
strcpy_s(buff2, sizeof(buff2), buff);
printf(" orig string \"%s\"\n", buff2);
CopyStringRemove(destbuff, buff2);
printf(" new \"%s\"\n", destbuff);
}
{
char buff2[128] = { 0 };
strcpy_s(buff2, sizeof(buff2), buff);
printf(" orig string \"%s\"\n", buff2);
CopyStringRemove(buff2, buff2);
printf(" new \"%s\"\n", buff2);
}
}
void main_xfun(void)
{
char *buff = "cat -v < x y z | ";
char *buffa = "cat -v < x y z ";
char *buffb = "cat -v x y z | ";
char *buffc = "cat -v x y z ";
printf("\ntest #1\n");
testfunc(buff);
printf("\ntest #2\n");
testfunc(buffa);
printf("\ntest #3\n");
testfunc(buffb);
printf("\ntest #4\n");
testfunc(buffc);
}
yields a result of:
test #1
orig string "cat -v < x y z | "
new "cat -v <| "
orig string "cat -v < x y z | "
new "cat -v <| "
orig string "cat -v < x y z | "
new "cat -v <| "
test #2
orig string "cat -v < x y z "
new "cat -v <"
orig string "cat -v < x y z "
new "cat -v <"
orig string "cat -v < x y z "
new "cat -v <"
test #3
orig string "cat -v x y z | "
new "cat -v x y z | "
orig string "cat -v x y z | "
new "cat -v x y z | "
orig string "cat -v x y z | "
new "cat -v x y z | "
test #4
orig string "cat -v x y z "
new "cat -v x y z "
orig string "cat -v x y z "
new "cat -v x y z "
orig string "cat -v x y z "
new "cat -v x y z "