I'm trying to develop a function in a library that looks for the caracters '<', '>' and '&'; and substitutes them for <
, >
and &
.
So far, I've developed this function:
char *sXMLspecialReplace(char *rawString) {
char *old_pointer = rawString;
int new_index = 0;
/*In most cases there won't be character substitution, so we can
start allocating with the original length and only reallocate if
a special char is spotted, therefore, memory reallocation will
take place in few circumstances.
+ 1 for NULL terminator.*/
int result_size = strlen(rawString) + 1;
char *result = (char *) malloc(result_size);
do {
switch (*old_pointer) {
case '&':
result_size += 4; // From 1 to 5
result = (char *) realloc(result, result_size);
strcpy(result + new_index, "&");
new_index += 5;
break;
case '<':
result_size += 3; // From 1 to 4
result = (char *) realloc(result, result_size);
strcpy(result + new_index, "<");
new_index += 4;
break;
case '>':
result_size += 3; // From 1 to 4
result = (char *) realloc(result, result_size);
strcpy(result + new_index, ">");
new_index += 4;
break;
default:
//Most frequent case
result[new_index++] = *old_pointer;
}
old_pointer++;
} while (*old_pointer != '\0');
/*do-while ensures the function exits with a null terminator*/
return result;
}
While it works in some cases, there are others when it doesn't work. In a test I tried to do the following:
char *my_company = "HELLO INDUSTRIES EUROPE";
char *xml_company = sXMLspecialReplace(my_company);
sprintf(sXMLanswer,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<company>%s</company>\n",
xml_company);
printf(sXMLanswer);
OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<company>HELLO INDUSTRIES EUROPE[[[[[</company>
EDIT 1: format <
, >
and &
.