/*This is a modified version of tagtostruct.h created by Nick Szmyd that was reused with his permission*/ #ifndef __TAGTOSTRUCT_H #define __TAGTOSTRUCT_H #include #include #include #include #define SIZE 150 //contains the information for one media object typedef struct node { char name[SIZE]; char keywords[SIZE]; char subsection[SIZE]; char url[SIZE]; char level[SIZE]; struct node *next; } node; //pointers to nodes of different types typedef struct tagstructure { struct node *ts; struct node *as; struct node *vs; struct node *is; } tagstructure; //reads the tag structure and creates the appropriate nodes tagstructure *conversion(char *filename) { tagstructure *tags; //pointer to all nodes node *active, //pointer to current node *head; //pointer to head of list char temp[80]; //temporarily holds tag type, level, and param types int ch, //holds current character flag, //flag of when to create a new node flag2, //flag of when a tag is closed cat, //tells which param we are working with i; //lcv FILE *file1; //file pointer of file to read from flag2 = 1; if ((file1 = fopen(filename, "r")) == NULL) { printf("Error opening file for input!\n"); exit(0); } tags = malloc(sizeof(tagstructure)); tags->ts = NULL; tags->as = NULL; tags->vs = NULL; tags->is = NULL; for (;;) { ch = fgetc(file1); if(ch == EOF) if (ch == EOF) break; if (ch == '>') ch = fgetc(file1); if (!isspace(ch)) { if (ch == '<') { ch = fgetc(file1); if(ch == '/') { temp[0] = fgetc(file1); temp[1] = fgetc(file1); temp[2] = '\0'; ch = fgetc(file1); if(flag2 == 0) //found a closing tag, make sure it matches { if((head == tags->ts)&&(!(strcmp(temp, "ts") == 0))) { printf("Invalid closing tag on ts\n"); exit(0); } if((head == tags->as)&&(!(strcmp(temp, "as") == 0))) { printf("Invalid closing tag on as\n"); exit(0); } if((head == tags->is)&&(!(strcmp(temp, "is") == 0))) { printf("Invalid closing tag on is\n"); exit(0); } if((head == tags->vs)&&(!(strcmp(temp, "vs") == 0))) { printf("Invalid closing tag on vs\n"); exit(0); } i = 0; while(ch != '>') { temp[i++] = ch; ch = fgetc(file1); } temp[i] = '\0'; if(strcmp(temp,active->level) != 0) { printf("Invalid closing tag with level %s for opening tag with level %d\n", temp, active->level); exit(0); } flag2 = 1; } else { while (ch != '>') ch = fgetc(file1); } } else { for (i = 0; i < 2; i++) { temp[i] = ch; ch = fgetc(file1); } temp[i] = '\0'; if (ch == '>') { //found an opening tag if (strcmp(temp, "ts") == 0) { tags->ts = malloc(sizeof(node)); active = tags->ts; head = tags->ts; } else if (strcmp(temp, "as") == 0) { tags->as = malloc(sizeof(node)); head = tags->as; active = tags->as; } else if (strcmp(temp, "vs") == 0) { tags->vs = malloc(sizeof(node)); active = tags->vs; head = tags->vs; } else if (strcmp(temp, "is") == 0) { tags->is = malloc(sizeof(node)); active = tags->is; head = tags->is; } else { printf("Invalid tag type %s\n", temp); exit(0); } active->next = NULL; flag = 1; flag2 = 1; } else if (isdigit(ch) || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { flag2 = 0; if((strcmp(temp, "as") != 0)&&(strcmp(temp, "is") != 0)&&(strcmp(temp, "vs") != 0)&&(strcmp(temp, "ts") != 0)) { printf("Invalid tag type %s\n", temp); exit(0); } i = 0; while (isdigit(ch) || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { temp[i++] = ch; ch = fgetc(file1); } temp[i] = '\0'; if(flag == 1) { flag = 0; } else { active->next = malloc(sizeof(node)); active = active->next; active->next = NULL; } strcpy(active->level,temp); } else { printf("in here\n"); while (ch != '>') ch = fgetc(file1); } } } else { //found a parameter i = 0; while (ch != '=') { temp[i++] = ch; ch = fgetc(file1); } temp[i] = '\0'; if (strcmp(temp, "name") == 0) cat = 1; else if (strcmp(temp, "keywords") == 0) cat = 2; else if (strcmp(temp, "subsection") == 0) cat = 3; else if (strcmp(temp, "url") == 0) cat = 4; else { printf("Invalid parameter type %s\n", temp); exit(0); } ch = fgetc(file1); ch = fgetc(file1); i = 0; while (ch != '"') { if (ch != '\n') temp[i++] = ch; else temp[i++] = ' '; ch = fgetc(file1); } temp[i] = '\0'; switch (cat) { case 1: strcpy(active->name, temp); break; case 2: strcpy(active->keywords, temp); break; case 3: strcpy(active->subsection, temp); break; case 4: strcpy(active->url, temp); } } } } close(file1); return tags; } #endif