Code:
#include <stdio.h>
#include <string.h>
#include <regex.h>
#define MAXLINE 255
struct headline {
char *title;
char *link;
char *description;
};
typedef struct headline Headline;
void readInFile(void);
void printHeadlines(int *, Headline *[]);
int match(const char *, char *, Headline *[], Headline *, int * );
char getHeadlines(int, int, const char *, Headline *[], Headline *, int * );
void storeHeadlines(char *, Headline * , Headline *[], int * );
//made global so i dont have to keep passing it through each method
char searchType[]="description";
int main(void) {
printf ("-------------------------------------------------------------\n\n");
readInFile();
printf ("-------------------------------------------------------------\n");
return 0;
}
void readInFile(void) {
FILE *p; /*pipe stream*/
char line[MAXLINE]; /*lines read*/
char *searchPattern = NULL;
const char initialPattern [] = "<description>.*</description>"; /*initilise search pattern */
int lineCounter = 1;
Headline *arrayOfStories[1];
Headline story;
int arrayCounter=0;
if((p = popen("/usr/other/sdm/Bin/wget -q -O - http://homepages.ihug.com.au/~test.xml","r")) == NULL) {
fprintf(stderr, "cannot create pipe \n");
exit(1);
}
//printf ("%d", sizeof (line));
//performs search
while(fgets(line, sizeof line, p) != NULL) {
printf("Line %d: - - - - - - - - - - - - \n", lineCounter);
strcpy(searchType, "title");
searchPattern = (char *) malloc (sizeof (char) * (strlen (initialPattern) + 1));
strcpy(searchPattern, "<title>.*</title>");
printf("Searching for TITLE\n");
match(line,searchPattern, arrayOfStories, &story, &arrayCounter);
strcpy(searchType, "link");
strcpy(searchPattern, "<link>.*</link>");
printf("Searching for LINK\n");
match(line,searchPattern, arrayOfStories, &story, &arrayCounter);
strcpy(searchType, "description");
strcpy(searchPattern, "<description>.*</description>");
printf("Searching for DESCRIPTION\n");
match(line,searchPattern, arrayOfStories, &story, &arrayCounter);
lineCounter++;
printf ("\n");
free(searchPattern);
}
pclose(p);
}
int match(const char *string, char *pattern, Headline *arrayOfStories[], Headline *story, int *arrayCounter) {
int error = 0;
regex_t re;
size_t a = 2;
regmatch_t arrayOfMatches[1000];
int counter=0;
if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE) != 0) {
return(0); /* report error */
}
/* this call to regexec() finds the first match on the line */
error = regexec (&re, string, a, arrayOfMatches, 0);
/* while matches found */
while (error == 0) {
/* substring found between pm.rm_so and pm.rm_eo */
getHeadlines(arrayOfMatches[counter].rm_so, arrayOfMatches[counter].rm_eo, string,arrayOfStories,story,arrayCounter);
/* This call to regexec() finds the next match */
error = regexec (&re, string + arrayOfMatches[counter].rm_eo, 1, arrayOfMatches, REG_NOTBOL);
counter++;
}
regfree(&re);
return(1);
}
char getHeadlines(int start, int end, const char *searchString, Headline *arrayOfStories[], Headline *story, int *arrayCounter){
char *tagdata = "";
char tempChar;
char STag[] = "<description>";
char ETag[] = "</description>";
int counter=0;
int sTagLength=0;
int eTagLength=0;
if(strcmp(searchType,"title")==0) {
strcpy(STag, "<title>");
strcpy(ETag, "</title>");
}
else if(strcmp(searchType,"link")==0) {
strcpy(STag, "<link>");
strcpy(ETag, "</link>");
}
else {
strcpy(STag, "<description>");
strcpy(ETag, "</description>");
}
sTagLength = strlen(STag);
eTagLength = strlen(ETag);
start = start + sTagLength;
end = end - eTagLength;
tagdata = (char *)calloc((end-start+1), sizeof(char));
while(start < end) {
tempChar = searchString[start];
tagdata[counter] = tempChar;
counter++;
start++;
}
tagdata[counter] = '\0';
//printf("Results: ");
//printf("%s\n", tagdata);
storeHeadlines(tagdata, story, arrayOfStories, arrayCounter);
free(tagdata);
}
void storeHeadlines(char *tagdata, Headline *story, Headline *arrayOfStories[], int *counter) {
if(strcmp(searchType,"title")==0) {
(*story).title = (char *)malloc(strlen(tagdata)+1);
strcpy ((*story).title, tagdata);
}
else if(strcmp(searchType,"link")==0) {
(*story).link = (char *)malloc(strlen(tagdata)+1);
strcpy ((*story).link, tagdata);
}
else {
(*story).description = (char *)malloc(strlen(tagdata)+1);
strcpy ((*story).description, tagdata);
}
if((*story).title != NULL && (*story).link != NULL && (*story).description != NULL) {
*arrayOfStories = (Headline *)malloc (1);
*arrayOfStories[*counter] = (Headline)*story;
*counter++;
printHeadlines(counter, arrayOfStories);
}
free((*story).title);
free((*story).link);
free((*story).description);
story=NULL;
}
void printHeadlines(int *counter, Headline *arrayOfStories[]) {
Headline *tempPtr;
Headline tempStory;
int i=0;
for(; i<*counter; i++) {
tempPtr = arrayOfStories [i];
tempStory = *tempPtr;
try printf ("%p", tempPtr) and see what the output is
printf("TITLE %s \n LINK %s \n DESCRIPTION %s \n", tempStory.title, tempStory.link, tempStory.description);
}
}