forked from xurxodiz/questcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysentences.h
executable file
·71 lines (61 loc) · 1.58 KB
/
mysentences.h
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
// This file deals with sentences and blocks of them.
// They behave basically like any other queue.
// Initialize. Never use a SB before calling this function!
void sb_makeempty(senblock *my) {
my->start = 0;
my->end = -1;
my->size = 0;
}
// Create a new, empty block of sentences. Use this as a factory.
senblock* sb_new() {
senblock* s = (senblock*) malloc(sizeof(senblock));
sb_makeempty(s);
return s;
}
// Check if there's any content within.
int sb_isempty(senblock *my) {
return (my->size == 0);
}
// Check for free slots.
int sb_isfull(senblock *my) {
return (my->end == MAXQUEUE);
}
// Add an element.
void sb_push(senblock *my, sentence* sy) {
if (sb_isfull(my)) {
msg_error_too_many_sentences();
abort();
}
my->end = (my->end+1);
my->size++;
my->queue[my->end] = sy;
}
// Look at the top without removing it.
sentence* sb_peek(senblock *my) {
if (sb_isempty(my)) {
msg_error_there_are_no_sentences();
abort();
}
return my->queue[my->start];
}
// Remove and return the top.
sentence* sb_pop(senblock *my) {
sentence* sy = sb_peek(my);
my->size--;
my->start = (my->start+1);
return sy;
}
// Resets the header to the beginning.
// WARNING: Be careful with the contents, the pointers may lead to unsafe content.
// Use with care and only you know what you're doing.
void sb_reset(senblock *my) {
my->start = 0;
my->size = (my->end+1 - my->start);
}
// Easy and quick way to create sentences
sentence* newsen(sentype type, void* comm) {
sentence* s = (sentence*) malloc(sizeof(sentence));
s->type = type;
s->command = comm;
return s;
}