c5005ad63841e6f1aea18b80ad6055ebdedc2cc4
[smdp.git] / include / cstack.h
1 #if !defined( CSTACK_H )
2 #define CSTACK_H
3
4 /*
5  * An implementation of a char stack in heap memory.
6  * Copyright (C) 2014 Michael Goehler
7  *
8  * This file is part of mpd.
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  *
23  *
24  * struct: cstack_t which defines char stack type in heap memory
25  *
26  * function: cstack_init to intialize struct of type cstack_t
27  * function: cstack_t->push to add one char on top if the stack
28  * function: cstack_t->pop to remove the top char from the stack
29  * function: cstack_t->top to test if the top char is a given char
30  * function: cstack_t->empty to test if the stack is empty
31  * function: cstack_t->delete to free the allocated memory
32  *
33  * Example:
34  *      cstack_t *p = cstack_init();
35  *      (p->push)(p, 'X');
36  *      printf("%c\n", (p->pop)(p));
37  *      (p->delete)(p);
38  *
39  */
40
41 typedef struct _cstack_t {
42     char *content;
43     size_t alloc;
44     size_t size;
45     int head;
46     void (*push)(struct _cstack_t *self, char c);
47     char (*pop)(struct _cstack_t *self);
48     int (*top)(struct _cstack_t *self, char c);
49     int (*empty)(struct _cstack_t *self);
50     void (*delete)(struct _cstack_t *self);
51 } cstack_t;
52
53 cstack_t *cstack_init();
54 void cstack_push(cstack_t *self, char c);
55 char cstack_pop(cstack_t *self);
56 int cstack_top(cstack_t *self, char c);
57 int cstack_empty(cstack_t *self);
58 void cstack_delete(cstack_t *self);
59
60 #endif // !defined( CSTACK_H )