/* * Function: * readEntryFile2LinkedList * * Description: * Reads the entries from a file and creates a linked list with * these entries in the inverse order. * * Arguments: * Pointer to the file stream: * FILE * fp * * Return value: * Pointer to the first node of the linked list. */ LinkedList * readEntryFile2LinkedList(FILE * fp, int parametro, int sentido) /*falta receber o ascendente e o tipo de ordenacao*/ { int id; int age; int height; int weight; LinkedList * lp; Entry * entry; /* Initialize linked list */ lp = initLinkedList(); /* Cycle through file rows */ while(fscanf(fp, "%d %d %d %d", &id, &age, &height, &weight) == 4) { /* Create new entry */ entry = newEntry(id, age, height, weight); /* Store entry in the linked list */ if( parametro == ID ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonIdDesc); } else if( parametro == AGE ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonAgeDesc); } else if( parametro == HEIGHT ){ if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonHeightDesc); } else { if( sentido == ASCENDENTE ) lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightAsc); else lp = insertSortedLinkedList(lp,(Item) entry, &comparisonWeightDesc); } } return lp; }
ListNode* RestInit( FILE* restfile) { char string[MAX_STRING]; int x, y, z, varcounter, tinit, tend; ListNode *head = NULL; Rest * auxrest, * auxrest2; /* Goes through every restriction rest in the restriction file */ while( fgets(string, MAX_STRING, restfile) != NULL) { /* Allocate memory for two restricton related rests */ auxrest = (Rest*) malloc( sizeof(Rest) ); /* auxrest --> rest that "creates (ta)" the restriction*/ VerifyMalloc( (Item) auxrest); auxrest2 = (Rest*) malloc( sizeof(Rest) ); /*auxrest2 --> rest that "destroys (tb)" the restriction */ VerifyMalloc( (Item) auxrest2); /* Count number of read variables */ varcounter = sscanf(string, "%*c %d %d %d %d %d", &tinit, &tend, &x, &y, &z); if( varcounter == 3 ) { auxrest->time = tinit; /* auxrest --> rest that "creates" the restriction*/ auxrest->x = -1; auxrest->y = -1; auxrest->z = x; /*the x value (in this case) represents the floor that's gonna be restricted and we're saving it in the coord z so its more intuitive in the future*/ auxrest->flag = 'f'; /* f flag means floor, for a whole floor rest*/ if ( tend != 0) /*If the restriction is endless then we dont need to add the "ending rest" to the list */ { auxrest2->time = tend; auxrest2->x = -1; auxrest2->y = -1; auxrest2->z = x; auxrest2->flag = 'f'; } } else if( varcounter == 5 ) { auxrest->time = tinit; /* auxrest --> rest that "creates" the restriction*/ auxrest->x = x; /*the x value (in this case) represents the floor that's gonna be restricted*/ auxrest->y = y; auxrest->z = z; auxrest->flag = 'p'; /* p flag means position for a position related rest*/ if ( tend != 0) /*If the restriction is endless then we dont need to add the "ending rest" to the list */ { auxrest2->time = tend; auxrest2->x = x; auxrest2->y = y; auxrest2->z = z; auxrest2->flag = 'p'; } } else { printf("Error. Confliting types or bad data format on rest config file."); exit(0); } head = insertSortedLinkedList(head, (Item) auxrest, CompareRestTime, 1); if ( tend != 0) head = insertSortedLinkedList(head, (Item) auxrest, CompareRestTime, 1); } return head; }