i still couldnt find the problem...i steped through setSearchTag()...it does exactly what i want...but if...i print the variable file before and after the method call...it changes....which is really odd...ill post my code again with the search function...thanks again for anyhelp
Code:
void searchForTags(char *file) {
char *currentSearch;
int *startOfData;
int *endOfData;
char *titles;
currentSearch = setSearchTag("title");
startOfData = search(file, currentSearch, "start");
currentSearch = setSearchTag("/title");
endOfData = search(file, currentSearch, "end");
}
char* setSearchTag(char *search) {
char *newSearchTag;
char *startTag = "<";
char *endTag = ">";
int newSearchTagLength = strlen(search) + strlen(startTag) + strlen(endTag);
if ((newSearchTag = ( char * )malloc(sizeof(char) * (newSearchTagLength)+1)) == NULL){
fprintf(stderr, "Unable to allocate %d bytes of memory for current search\n", newSearchTagLength+1);
exit(1);
}
strcpy(newSearchTag, startTag);
strcat(newSearchTag,search);
strcat(newSearchTag,endTag);
return newSearchTag;
}
int* search(char *file, char *currentSearch, char *tagType) {
int error = 0;
regex_t re;
size_t a = 2;
regmatch_t arrayOfMatches[1000];
int counter=0;
int *Matches;
if (regcomp(&re, currentSearch, REG_EXTENDED|REG_ICASE) != 0) {
return(0);
}
if ((Matches = malloc(sizeof(int))) == NULL){
fprintf(stderr, "Unable to reallocate %d bytes of memory for current search\n", sizeof(int));
exit(1);
}
printf("-----XXXXXXXXXXXXXXXXXXXXXXXXXX-----\n");
/* Finds the matches on the line */
error = regexec (&re, file, a, arrayOfMatches, 0);
/* while matches found */
while (error == 0) {
if(strcmp(tagType,"start")){
Matches[counter] = arrayOfMatches[counter].rm_eo;
}
else{
Matches[counter] = arrayOfMatches[counter].rm_so;
}
if ((Matches = (int *)realloc(Matches, sizeof(int))) == NULL){
fprintf(stderr, "Unable to reallocate %d bytes of memory for file\n", sizeof(int));
exit(1);
}
/* This call to regexec() finds the next match */
error = regexec (&re, file + arrayOfMatches[counter].rm_eo, 1, arrayOfMatches, REG_NOTBOL);
counter++;
}
if (error == 0) {
return 0;
}
regfree(&re);
return Matches;
}