-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
67 lines (61 loc) · 2.1 KB
/
task.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Defines all the functions listed in "task.h".
* @author Marcelo F, Cabral ([email protected])
*/
#include"task.h"
#ifndef TASK_C
#define TASK_C
/**
* Creates a Task struct by dynamically allocating memory for it.
* @param _name a pointer to a character array that contains the task's name
* @param _tt a pointer to a TaskTime struct.
* @return a pointer to the newly allocated Task struct (Task*), if _tt is not NULL. Else, returns NULL.
*/
Task* createTask(char* _name, TaskTime *_tt){
Task *ta = NULL;
if(_tt != NULL){
ta = (Task*) malloc(sizeof(Task));
ta->name = _name;
ta->tt = _tt;
}
return ta;
}
/**
* Deallocates a Task struct in memory or simply returns a Task struct's address, depending on the operation code.
* @param ta a pointer to a Task struct.
* @param op an operation code. If 1 is passed, returns a pointer with the same value as ta. If 0 is passed, deallocates data pointed at by ta.
* @return NULL, if op == 0; same value as ta, if op == 1.
* Returning the same address as the one passed as an argument becomes convenient when swapping tasks between two different days or changing a task's day.
*/
Task* removeTask(Task *ta, int op){
Task *remov = NULL;
if(ta != NULL){
if(op){ remov = ta; }
else{
free(ta->name); //deallocates the name string
removeTaskTime(ta->tt, op); //deallocates the task time struct
free(ta); //deallocates the pointers
}
}
return remov;
}
/**
* Prints a Task struct's name (name) and it's TaskTime struct's starting hour (b_h), starting minutes (b_m), ending hour (e_h) and ending minutes (e_m).
* @param t a pointer to a Task struct (Task*).
*/
void printTask(Task *t){
if(t != NULL){
printf("\n\tName: %s", t->name);
printTaskTime(t->tt);
}
}
/**
* Prints a Task struct's TaskTime info (starting hour (b_h), starting minutes (b_m), ending hour (e_h) and ending minutes (e_m)).
* @param t a pointer to a Task struct (Task*).
*/
void printOnlyTaskTime(Task *t){
if(t != NULL){
printTaskTime(t->tt);
}
}
#endif