sorted some includes + added version number
[smdp.git] / cstack.c
1 #include <stdlib.h> // malloc, realloc
2
3 #include "include/cstack.h"
4
5 cstack_t *cstack_init() {
6     cstack_t *stack = malloc(sizeof(cstack_t));
7     stack->content = (void*)0;
8     stack->alloc = stack->size = 0;
9     stack->head = -1;
10     stack->push = cstack_push;
11     stack->pop = cstack_pop;
12     stack->top = cstack_top;
13     stack->empty = cstack_empty;
14     stack->delete = cstack_delete;
15     return stack;
16 }
17
18 void cstack_push(cstack_t *self, char c) {
19     if(self->size + sizeof(c) > self->alloc) {
20         self->alloc += (sizeof(char));
21         self->content = realloc(self->content, self->alloc);
22     }
23     self->content[++self->head] = c;
24     self->size += (sizeof(char));
25 }
26
27 char cstack_pop(cstack_t *self) {
28     self->size -= (sizeof(char));
29     return self->content[self->head--];
30 }
31
32 char cstack_top(cstack_t *self) {
33     return self->content[self->head];
34 }
35
36 int cstack_empty(cstack_t *self)  {
37    return self->head == -1;
38 }
39
40 void cstack_delete(cstack_t *self) {
41     free(self->content);
42     free(self);
43 }
44