2 * An implementation of a char stack in heap memory.
3 * Copyright (C) 2015 Michael Goehler
5 * This file is part of mdp.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <stdio.h> // fprintf
24 #include <stdlib.h> // malloc, realloc
28 cstack_t *cstack_init() {
29 cstack_t *stack = NULL;
30 if((stack = malloc(sizeof(cstack_t))) != NULL) {
31 stack->content = NULL;
32 stack->alloc = stack->size = 0;
34 stack->push = cstack_push;
35 stack->pop = cstack_pop;
36 stack->top = cstack_top;
37 stack->empty = cstack_empty;
38 stack->delete = cstack_delete;
40 fprintf(stderr, "%s\n", "cstack_init() failed to allocate memory.");
46 void cstack_push(cstack_t *self, wchar_t c) {
47 if(self->size + sizeof(c) > self->alloc) {
48 self->alloc += (sizeof(wchar_t));
49 if((self->content = realloc(self->content, self->alloc)) == NULL) {
50 fprintf(stderr, "%s\n", "cstack_push() failed to reallocate memory.");
54 self->content[++self->head] = c;
55 self->size += (sizeof(wchar_t));
58 wchar_t cstack_pop(cstack_t *self) {
59 self->size -= (sizeof(wchar_t));
60 return self->content[self->head--];
63 bool cstack_top(cstack_t *self, wchar_t c) {
64 return self->head >= 0 && self->content[self->head] == c;
67 bool cstack_empty(cstack_t *self) {
68 return self->head == -1;
71 void cstack_delete(cstack_t *self) {