right now im completly confused. im working on a function that is supposed to tag a tag and change its data
<tag>data</tag>
so a call like
Code:
char test[225];
strcpy(test, "<crap>testing</crap>");
changetags(test, "TESTING!!!");
printf("%s\n", test);
would give me back "<crap>TESTING!!!</crap>"
im completly confused. here my code, sorry it so long. but i have no idea what wrong here, i think ive been looking at it for to long.
Code:
int changedata(char *src, char change[]) {
int x = 0; /* Current Position. */
int check;
int starttag_start;
int starttag_end;
int endtag_start;
int endtag_end;
char *value = malloc(1);
value[0] = '\0';
int length = 0;
int change_length = strlen(change);
while(src[x] != '\0') {
if(src[x] == '<') {
check = x;
while(src[check] != '>' && src[check] != ' ' && src[check] != '\0')
++check;
/* Not html. */
if(src[check] == ' ' || src[check] == '\0') {
value = realloc(value, 2);
value[length] = src[check];
value[(length + 1)] = '\0';
++length;
}
/* Start html tag found. */
if(src[check] == '>') {
starttag_start = x;
starttag_end = check;
while(src[check] != '<' && src[check] != '\0')
++check;
/* Start of end tag found. */
if(src[check] == '<')
endtag_start = check;
else
break;
/* End of end tag found. */
while(src[check] != '>' && src[check] != ' ' && src[check] != '\0')
++check;
if(src[check] == '>') {
endtag_end = check;
/* Check everything. */
if(starttag_start == 0 || starttag_end == 0)
break;
if(endtag_start == 0 || endtag_end == 0)
break;
/* Slowly copy the start tag. */
while(starttag_start < starttag_end) {
value = realloc(value, 2);
value[length] = src[starttag_start];
value[(length + 1)] = '\0';
++length;
++starttag_start;
}
/* Copy the changed data. */
value = realloc(value, (change_length + 1));
strcat(value, change);
length += change_length;
/* Copy the end tag. */
while(endtag_start < endtag_end) {
value = realloc(value, 2);
value[length] = src[endtag_start];
value[(length + 1)] = '\0';
++length;
++endtag_start;
}
} else
break;
/* Reset. */
starttag_start = 0;
starttag_end = 0;
endtag_start = 0;
endtag_end = 0;
}
} else {
value = realloc(value, 2);
value[length] = src[x];
value[(length + 1)] = '\0';
++length;
}
++x;
}
strcpy(src, value);
free(value);
return 0;
}