Skip to content

Commit acd3130

Browse files
committed
command_queue: implement command queue functions
1 parent 242c375 commit acd3130

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/utils/command_queue.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "command_queue.h"
2+
3+
// Delete first command queue, return head
4+
struct command_queue *pop_command(struct command_queue *head) {
5+
struct command_queue *new_head = NULL;
6+
if (!head)
7+
return NULL;
8+
else if (!head->next) {
9+
if (head->data)
10+
free(head->data);
11+
free(head);
12+
return NULL;
13+
} else {
14+
new_head = head->next;
15+
if (head->data)
16+
free(head->data);
17+
free(head);
18+
return new_head;
19+
}
20+
}
21+
22+
// Push command in queue, return head, can have head=NULL for init queue
23+
struct command_queue *push_command(struct command_queue *head, struct command_queue *new_elt) {
24+
if (!head) {
25+
return new_elt;
26+
}
27+
struct command_queue *iter = head;
28+
while (iter->next) {
29+
iter = iter->next;
30+
}
31+
iter->next = new_elt;
32+
return head;
33+
}
34+
35+
// Get command queue data, you should known how to cast it
36+
void *peek_command(struct command_queue *head) {
37+
if (head)
38+
return head->data;
39+
return NULL;
40+
}
41+
42+
// Get user command enum of the first of the queue
43+
enum user_command peek_user_command(struct command_queue *head) {
44+
if (head)
45+
return head->command;
46+
return NONE;
47+
}

0 commit comments

Comments
 (0)