Ejemplo n.º 1
0
void tasklist_remove_test()
{
	//Create task list
	tasklist_t *tasklist = tasklist_create();

	//Create tasks
	task_t *low = task_create(work_noArgument, NO_ARGUMENT, LOW);
	task_t *medium = task_create(work_noArgument, NO_ARGUMENT, MEDIUM);
	task_t *high = task_create(work_noArgument, NO_ARGUMENT, HIGH);

	//Insert them in the list
	tasklist_insert(tasklist, high);
	tasklist_insert(tasklist, medium);
	tasklist_insert(tasklist, low);

	//Asserts
	assert(tasklist_remove(tasklist, medium->ID) == medium);
	assert(tasklist_remove(tasklist, low->ID) == low);
	assert(tasklist_remove(tasklist, high->ID) == high);
	assert(tasklist->n_tasks == 0);
	assert(tasklist_remove(tasklist, high->ID) != high);

	//Free memory
	free(tasklist);
	free(low);
	free(medium);
	free(high);
}
Ejemplo n.º 2
0
// Removes a task given the file and the index.
int remove_task(char* filename, int number){
    
    FILE* file = fopen(filename, "r");
    if (file == NULL){
        perror("Cannot remove task");
        errno = 0;
        goto error;
    }

    // Open up the tasklist.
    tasklist* list = tasklist_new();
    if (tasklist_read(list, file) != 0){
        puts("ERROR: could not read tasklist.");
        goto error;
    }

    fclose(file);
    fopen(filename, "w");
    Task* t = tasklist_get(list, number-1);
    if (t == NULL){
        puts("There is no task with that number.");
        goto error;
    }

    printf("Are you sure you want to remove %d: %s? (y/n)\n", number, task_dump(t));
    char answer[5];
    char* ans = answer;
    fgets(ans, 5, stdin);
    if (strcmp(answer, "y\n") == 0){
        tasklist_remove(list, number-1);
        if (tasklist_dump(list, file) != 0){
            puts("ERROR: could not write tasklist.");
        }
    }
    printf("Task %d deleted.\n", number);
    task_free(t);
    tasklist_free(list);
    error:
        fclose(file);
        return 1;
}