parallax 1.0
command-line based task/todo manager
Loading...
Searching...
No Matches
task.h File Reference

Contains structures and functions for handling task-related operations. More...

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  Task
 A basic task. More...
struct  TaskClass
 A task class containing other tasks. More...
struct  TaskCollection
 A task collection containing other classes. More...

Macros

#define MAX_TABLE_SIZE   50

Typedefs

typedef struct Task Task
 A basic task.
typedef struct TaskClass TaskClass
 A task class containing other tasks.
typedef struct TaskCollection TaskCollection
 A task collection containing other classes.

Functions

Taskcreate_task (char *class_of_task, char *task_content)
 Creates a task of known class and content.
TaskClasscreate_task_class (char *class_of_task, size_t class_size)
 Creates a task class of a known class and size.
TaskCollectioncreate_task_collection (size_t collection_size)
 Creates a task collection of known size.
Taskget_task (TaskClass *task_class, const char *key)
 Finds a task in a task class by its content.
void add_task (TaskClass *task_class, Task *new_task)
 Adds a task to a task class.
TaskClassget_task_class (TaskCollection *task_collection, const char *key)
 Gets a task class by its class name from a task collection.
void add_task_class (TaskCollection *task_collection, TaskClass *task_class)
 Adds a task class to a task collection.
void remove_task (TaskClass *task_class, char *task_content)
 Removes a task from a task class by its content.
void remove_task_class (TaskCollection *task_collection, char *class_name)
 Removes a task class from a task collection.
void print_task (Task *task)
 Prints the content of a task.

Detailed Description

Contains structures and functions for handling task-related operations.

Defines the schema of tasks, task classes, and task collections. It also contains the procedures for creating, modifying, removing, and destroying the task structures.

Definition in file task.h.

Macro Definition Documentation

◆ MAX_TABLE_SIZE

#define MAX_TABLE_SIZE   50

Definition at line 9 of file task.h.

Typedef Documentation

◆ Task

typedef struct Task Task

A basic task.

Contains two strings, which are the class and the content.

◆ TaskClass

typedef struct TaskClass TaskClass

A task class containing other tasks.

Synonymous to a subject or category that the task belongs to.

◆ TaskCollection

typedef struct TaskCollection TaskCollection

A task collection containing other classes.

Serves as a lookup table for the existing classes in the serverside

Function Documentation

◆ add_task()

void add_task ( TaskClass * task_class,
Task * new_task )

Adds a task to a task class.

Parameters
task_classPointer to the task class that will contain the new task
new_taskPointer to the task to be added
Note
Wrapper around the hashmap_set function

Definition at line 74 of file task.c.

75{
79 if ( task_class == NULL || new_task->task_content == NULL )
80 {
81 printf("Task class error: task class or key is null\n");
82 return;
83 }
84 if ( new_task == NULL )
85 {
86 printf("Task class error: new task to be added is null\n");
87 return;
88 }
89 if ( task_class->table_size <= 0 )
90 {
91 printf("Task class error: invalid table size (%zu)", task_class->table_size);
92 return;
93 }
94
95 hashmap_set(task_class->task_class_table, new_task->task_content, new_task);
96}
void hashmap_set(HashMap *map, char *key, void *value)
Adds an entry to a hashmap by a key string and value.
Definition hashmap.c:42
size_t table_size
Definition task.h:42
HashMap * task_class_table
Definition task.h:43
char * task_content
Definition task.h:31

◆ add_task_class()

void add_task_class ( TaskCollection * task_collection,
TaskClass * task_class )

Adds a task class to a task collection.

Parameters
task_collectionPointer to the task collection that will contain the new task
task_classPointer to the task class to be added
Note
Wrapper around hashmap_set

Definition at line 123 of file task.c.

124{
128 if (task_collection == NULL || task_class == NULL)
129 {
130 return;
131 }
132 if (task_class->task_class_name == NULL)
133 {
134 return;
135 }
136
137 hashmap_set(task_collection->task_collection, task_class->task_class_name, task_class);
138}
char * task_class_name
Definition task.h:41
HashMap * task_collection
Definition task.h:54

◆ create_task()

Task * create_task ( char * class_of_task,
char * task_content )

Creates a task of known class and content.

Parameters
class_of_taskPointer to the class string of the task
task_contentPointer to the content string of the task
Returns
Pointer to the created task

Definition at line 16 of file task.c.

17{
18 Task* task = (Task*)malloc(sizeof(Task));
19 task->task_class = class_of_task;
20 task->task_content = task_content;
21 return task;
22}
A basic task.
Definition task.h:29
char * task_class
Definition task.h:30

◆ create_task_class()

TaskClass * create_task_class ( char * class_of_task,
size_t class_size )

Creates a task class of a known class and size.

Parameters
class_of_taskPointer to the class string of the task class
class_sizeSize of the task class hash table
Returns
Pointer to the created task class
Note
This is a wrapper around the hashmap_create function

Definition at line 24 of file task.c.

25{
29 TaskClass* task_class = (TaskClass*)malloc(sizeof(TaskClass));
30 task_class->task_class_name = class_name;
31 task_class->table_size = class_size;
32 task_class->task_class_table = hashmap_create(class_size);
33 return task_class;
34}
HashMap * hashmap_create(size_t table_size)
Creates a hashmap of a fixed size.
Definition hashmap.c:32
A task class containing other tasks.
Definition task.h:40

◆ create_task_collection()

TaskCollection * create_task_collection ( size_t collection_size)

Creates a task collection of known size.

Parameters
collection_sizeSize of the task collection hash table
Returns
Pointer to the created task collection
Note
This is a wrapper around the hashmap_create function

Definition at line 36 of file task.c.

37{
41 TaskCollection* task_collection = (TaskCollection*)malloc(sizeof(TaskCollection));
42 task_collection->collection_size = collection_size;
43 task_collection->task_collection = hashmap_create(collection_size);
44 return task_collection;
45}
A task collection containing other classes.
Definition task.h:53
size_t collection_size
Definition task.h:55

◆ get_task()

Task * get_task ( TaskClass * task_class,
const char * key )

Finds a task in a task class by its content.

Parameters
task_classPointer to the task class to look into
keyPointer to the key string (task content)
Returns
Pointer to the task found. Returns NULL if not found
Note
Wrapper around the hashmap_get function.

Casts the output of hashmap_get to a Task* type

Definition at line 47 of file task.c.

48{
54 if ( task_class == NULL || key == NULL )
55 {
56 printf("Task class error: task class or key is null\n");
57 return NULL;
58 }
59 if (task_class->table_size <= 0)
60 {
61 printf("Task class error: invalid table size (%zu)", task_class->table_size);
62 return NULL;
63 }
64
65 void* ret = hashmap_get(task_class->task_class_table, key);
66 if (ret == NULL)
67 {
68 return NULL;
69 }
70
71 return (Task*)ret; //cast into Task type
72}
void * hashmap_get(HashMap *map, char *key)
Finds an entry in a non-empty hashmap by a key.
Definition hashmap.c:80

◆ get_task_class()

TaskClass * get_task_class ( TaskCollection * task_collection,
const char * key )

Gets a task class by its class name from a task collection.

Parameters
task_collectionPointer to the task collection containing the task class to be found
keyPointer to the key string (task class name)
Returns
Pointer to the task class found. Returns NULL if not found
Note
Wrapper around the hashmap_get function

Casts the output of hashmap_get to a TaskClass* type

Definition at line 98 of file task.c.

99{
105 if (task_collection == NULL || key == NULL)
106 {
107 return NULL;
108 }
109 if (task_collection->collection_size <= 0)
110 {
111 return NULL;
112 }
113
114 void* ret = hashmap_get(task_collection->task_collection, key);
115
116 if (ret == NULL)
117 {
118 return NULL;
119 }
120 return (TaskClass*)ret; // Cast into a TaskClass type
121}

◆ print_task()

void print_task ( Task * task)

Prints the content of a task.

Parameters
taskPointer to the task to be printed
Warning
Assumes that the task attributes are non-NULL

Definition at line 172 of file task.c.

173{
177 if (task == NULL)
178 {
179 return;
180 }
181 printf("Task class: %s\nContent: %s\n", task->task_class, task->task_content);
182}

◆ remove_task()

void remove_task ( TaskClass * task_class,
char * task_content )

Removes a task from a task class by its content.

Parameters
task_classPointer to the task class that contains the task to be removed
task_contentPointer to the content string of the task to be removed
Note
Wrapper around hashmap_elem_remove

Definition at line 140 of file task.c.

141{
145 if (task_class == NULL || task_content == NULL)
146 {
147 return;
148 }
149 if (task_class->task_class_table == NULL)
150 {
151 return;
152 }
153 hashmap_elem_remove(task_class->task_class_table, task_content);
154}
void hashmap_elem_remove(HashMap *map, char *key)
Removes an entry in a hashmap by its key.
Definition hashmap.c:102

◆ remove_task_class()

void remove_task_class ( TaskCollection * task_collection,
char * class_name )

Removes a task class from a task collection.

Parameters
task_collectionPointer to the task collection that contains the task class to be removed
class_namePointer to the class name string of the task class to be removed
Note
Wrapper around hashmap_elem_remove

Definition at line 156 of file task.c.

157{
161 if (task_collection == NULL || class_name == NULL)
162 {
163 return;
164 }
165 if (task_collection->task_collection == NULL)
166 {
167 return;
168 }
169 hashmap_elem_remove(task_collection->task_collection, class_name);
170}