/* unload an 'on demand loaded' room Sometimes, the find_Room() functions load a room on demand This is cool, but there are cases in which the loaded room needs to be destroyed again -- this is done by unload_Room() */ void unload_Room(Room *r) { if (r == NULL) return; if (r->number == HOME_ROOM && count_Queue(r->inside) <= 0) { /* demand loaded Home room */ (void)remove_Room(&HomeRooms, r); if (save_Room(r)) log_err("unload_Room(): failed to save room #%u %s", r->number, r->name); destroy_Room(r); return; } if (r->number == MAIL_ROOM) { /* demand loaded Mail> room */ Room *h; /* Note: mail rooms are usually stored in the user as usr->mail However, if the user was not online, it was put on the HomeRooms list so if we can find it there, it should be unloaded if we can't find it there, the room should not be unloaded This procedure is more efficient than scanning all users for "(usr->mail == r)" because the HomeRooms list is usually very short or empty */ for(h = HomeRooms; h != NULL; h = h->next) { if (h == r) { (void)remove_Room(&HomeRooms, r); if (save_Room(r)) log_err("unload_Room(): failed to save HomeRoom #%u %s", r->number, r->name); destroy_Room(r); return; } /* do some house-cleaning Note: due the 'return' statement just above, we may not complete the house-cleaning work... */ if (count_Queue(h->inside) <= 0) { (void)remove_Room(&HomeRooms, r); if (save_Room(r)) log_err("unload_Room(): failed to save room #%u %s", r->number, r->name); destroy_Room(r); } } } }
int main(void) { char* topic1 = "Hello"; int time1 = 10; int room1 = 1001; struct Meeting* Meeting1 = create_Meeting(time1, topic1); struct Room* Room1 = create_Room(room1); printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // 0 printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // -1 print_Meeting(find_Room_Meeting(Room1, time1)); printf("%d\n", remove_Room_Meeting(Room1, Meeting1)); // 0 printf("%d\n", remove_Room_Meeting(Room1, Meeting1)); // -1 printf("%d\n", add_Room_Meeting(Room1, Meeting1)); // 0 clear_Room(Room1); print_Room(Room1); const char *topicA = "Whisky", *topicB = "Tango"; const char *firstname1 = "Bo", *lastname1 = "Jackson", *phoneno1 = "123"; const char *firstname2 = "Mike", *lastname2 = "David", *phoneno2 = "456"; struct Person* person1 = create_Person(firstname1, lastname1, phoneno1); struct Person* person2 = create_Person(firstname2, lastname2, phoneno2); struct Meeting *new_Meeting1 = create_Meeting(10, topicA); struct Meeting *new_Meeting2 = create_Meeting(9, topicB); struct Ordered_container* people = OC_create_container((OC_comp_fp_t) compare_Person_lastname); OC_insert(people, person1); OC_insert(people, person2); struct Room* room2 = create_Room(1000); add_Meeting_participant(new_Meeting1, person1); add_Meeting_participant(new_Meeting1, person2); add_Room_Meeting(room2, new_Meeting1); add_Room_Meeting(room2, new_Meeting2); printf("\n...saveload/file...\n"); FILE *fp = fopen("Room_savefile.txt", "w"); save_Room(room2, fp); fclose(fp); fp = fopen("Room_savefile.txt", "r"); struct Room *new_Room = load_Room(fp, people); print_Room(new_Room); fclose(fp); }