CFLAGS=-g -Wall
-tmp: tmp.c cstring.o markdown.o parser.o viewer.o
- cc $(CFLAGS) -o tmp tmp.c cstring.o markdown.o parser.o viewer.o -lncurses
+tmp: tmp.c cstring.o cstack.o markdown.o parser.o viewer.o
+ cc $(CFLAGS) -o tmp tmp.c cstring.o cstack.o markdown.o parser.o viewer.o -lncurses
viewer.o: viewer.c
cc $(CFLAGS) -c viewer.c
markdown.o: markdown.c
cc $(CFLAGS) -c markdown.c
+cstack.o: cstack.c
+ cc $(CFLAGS) -c cstack.c
+
cstring.o: cstring.c
cc $(CFLAGS) -c cstring.c
all: tmp
+clean:
+ rm -f tmp *.o
+
.PHONY: test
test:
$(MAKE) -C test
-clean:
- rm -f tmp *.o
-
--- /dev/null
+#include <stdlib.h> // malloc, realloc
+
+#include "include/cstack.h"
+
+cstack_t *cstack_init() {
+ cstack_t *stack = malloc(sizeof(cstack_t));
+ stack->content = (void*)0;
+ stack->alloc = stack->size = 0;
+ stack->head = -1;
+ stack->push = cstack_push;
+ stack->pop = cstack_pop;
+ stack->top = cstack_top;
+ stack->empty = cstack_empty;
+ stack->delete = cstack_delete;
+ return stack;
+}
+
+void cstack_push(cstack_t *self, char c) {
+ if(self->size + sizeof(c) > self->alloc) {
+ self->alloc += (sizeof(char));
+ self->content = realloc(self->content, self->alloc);
+ }
+ self->content[++self->head] = c;
+ self->size += (sizeof(char));
+}
+
+char cstack_pop(cstack_t *self) {
+ self->size -= (sizeof(char));
+ return self->content[self->head--];
+}
+
+char cstack_top(cstack_t *self) {
+ return self->content[self->head];
+}
+
+int cstack_empty(cstack_t *self) {
+ return self->head == -1;
+}
+
+void cstack_delete(cstack_t *self) {
+ free(self->content);
+ free(self);
+}
+
--- /dev/null
+#if !defined( CSTACK_H )
+#define CSTACK_H
+
+typedef struct _cstack_t {
+ char *content;
+ size_t alloc;
+ size_t size;
+ int head;
+ void (*push)(struct _cstack_t *self, char c);
+ char (*pop)(struct _cstack_t *self);
+ char (*top)(struct _cstack_t *self);
+ int (*empty)(struct _cstack_t *self);
+ void (*delete)(struct _cstack_t *self);
+} cstack_t;
+
+void cstack_push(cstack_t *self, char c);
+char cstack_pop(cstack_t *self);
+char cstack_top(cstack_t *self);
+int cstack_empty(cstack_t *self);
+void cstack_delete(cstack_t *self);
+
+#endif // !defined( CSTACK_H )
\ No newline at end of file
--- /dev/null
+#include <stdio.h>
+
+#include "../include/cstack.h"
+#include "../cstack.c"
+
+int main(int argc, char *argv[]) {
+
+ cstack_t *s = cstack_init();
+ printf("INIT: head: %d, size: %zu, alloc: %zu\n", s->head, s->size, s->alloc);
+
+ (s->push)(s, 'a');
+ printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc);
+
+ (s->push)(s, 'b');
+ printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc);
+
+ (s->push)(s, 'c');
+ printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc);
+
+ char ch = (s->pop)(s);
+ printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc);
+
+ ch = (s->pop)(s);
+ printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc);
+
+ (s->push)(s, 'd');
+ printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc);
+
+ (s->push)(s, 'e');
+ printf("PUSH: char: %c, head: %d, size: %zu, alloc: %zu\n", (s->top)(s), s->head, s->size, s->alloc);
+
+ while(s->size > 0) {
+ ch = (s->pop)(s);
+ printf("POP: char: %c, head: %d, size: %zu, alloc: %zu\n", ch, s->head, s->size, s->alloc);
+ }
+
+ (s->delete)(s);
+
+ return(0);
+}
+