forked from adrianomarto/soft_uart
-
Notifications
You must be signed in to change notification settings - Fork 5
/
queue.c
executable file
·102 lines (95 loc) · 2.08 KB
/
queue.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "queue.h"
/**
* Initializes a given queue.
* @param queue given queue
*/
void initialize_queue(struct queue* queue)
{
queue->size = 0;
queue->front = 0;
queue->rear = 0;
}
/**
* Adds a given character into a given queue.
* @param queue given queue
* @param character given character
* @return 1 if the character is added to the queue. 0 if the queue is full.
*/
int enqueue_character(struct queue* queue, const unsigned char character)
{
int success = 0;
if (queue->size < QUEUE_MAX_SIZE)
{
if (queue->size != 0)
{
queue->rear++;
if (queue->rear >= QUEUE_MAX_SIZE)
{
queue->rear = 0;
}
}
else
{
queue->rear = 0;
queue->front = 0;
}
queue->data[queue->rear] = character;
queue->size++;
success = 1;
}
return success;
}
/**
* Gets a character from a fiven queue.
* @param queue given queue
* @param character a character
* @return 1 if a character is fetched from the queue. 0 if the queue is empy.
*/
int dequeue_character(struct queue* queue, unsigned char* character)
{
int success = 0;
if (queue->size > 0)
{
*character = queue->data[queue->front];
queue->front++;
if (queue->front >= QUEUE_MAX_SIZE)
{
queue->front = 0;
}
queue->size--;
success = 1;
}
return success;
}
/**
* Adds a given string to a given queue.
* @param queue given queue
* @param string given string
* @param string_size size of the given string
* @return The amount of characters successfully added to the queue.
*/
int enqueue_string(struct queue* queue, const unsigned char* string, int string_size)
{
int n = 0;
while (n < string_size && enqueue_character(queue, string[n]))
{
n++;
}
return n;
}
/**
* Gets the number of characters that can be added to a given queue.
* @return number of characters.
*/
int get_queue_room(struct queue* queue)
{
return QUEUE_MAX_SIZE - queue->size;
}
/**
* Gets the number of characters contained in a given queue.
* @return number of characters.
*/
int get_queue_size(struct queue* queue)
{
return queue->size;
}