summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--Makefile35
-rw-r--r--src/buffer.c46
-rw-r--r--src/buffer.h27
-rw-r--r--src/main.c3
-rw-r--r--src/piece-table.c54
-rw-r--r--src/piece-table.h32
-rw-r--r--tests/buffer.c14
8 files changed, 215 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e6da1ac
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+tests/*
+!tests/.c
+out
+parc
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8b75684
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,35 @@
+CC = gcc
+TARGET = parc
+BASENM = buffer piece-table
+SRC = $(addsuffix .c, $(addprefix src/, $(BASENM)) )
+OBJDIR = out
+OBJ = $(addsuffix .o, $(addprefix $(OBJDIR)/, $(BASENM)))
+
+HEADERS = $(wildcard src/*.h)
+
+TEST := buffer
+TEST_FILES = $(addprefix tests/, $(TEST))
+TEST_SOURCES = $(addsuffix .c, $(addprefix tests/, $(TEST)))
+
+all: $(TARGET)
+
+$(TARGET): $(OBJ) src/main.c
+ @$(CC) $(CFLAGS) $(OBJ) src/main.c -o $(TARGET)
+
+$(OBJ): $(OBJDIR)/%.o: src/%.c $(HEADERS)
+ @mkdir -p out
+ @$(CC) $(CFLAGS) -o $@ -c $<
+
+clean:
+ @rm -f $(TARGET)
+ @rm -f out/*
+ @rm -rf out/
+ @rm -f $(TEST_FILES)
+
+$(TEST_FILES): $(TARGET) $(TEST_SOURCES) $(HEADERS)
+ @$(CC) $(CFLAGS) -o $@ $(addsuffix .c, $@) $(OBJ)
+ @if ./$@ ; then echo "PASS: $@";else echo "FAIL: $@ exit value: $$?"; fi
+
+test: $(TEST_FILES)
+
+.PHONY: clean all test
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100644
index 0000000..2fbfe72
--- /dev/null
+++ b/src/buffer.c
@@ -0,0 +1,46 @@
+#include "buffer.h"
+#include <string.h>
+#include <malloc.h>
+
+bool init_growable_buffer(growable_buffer *buffer) {
+ buffer->size = GROW_SIZE;
+ buffer->used = 0;
+ buffer->content = malloc(GROW_SIZE);
+ if (!buffer->content) {
+ return false;
+ }
+ return true;
+}
+
+bool grow_growable_buffer(growable_buffer *buffer, size_t at_least) {
+ size_t new_size = ((at_least % GROW_SIZE) + 1 ) * GROW_SIZE;
+ if (new_size < buffer->size){
+ buffer->content = realloc(buffer->content, new_size);
+ if (!buffer->content){
+ return false;
+ }
+ buffer->size = new_size;
+ return true;
+ }
+}
+
+void free_growable_buffer(growable_buffer *buffer) {
+ free(buffer->content);
+ buffer->content = NULL;
+ buffer->used = 0;
+ buffer->size = 0;
+}
+
+
+
+bool init_fixed_buffer(fixed_buffer *buffer, char *orig) {
+ buffer->content = orig;
+ buffer->size = strlen(orig);
+ return buffer->content != NULL;
+}
+
+void free_fixed_buffer(fixed_buffer *buffer) {
+ free(buffer->content);
+ buffer->content = NULL;
+ buffer->size = 0;
+}
diff --git a/src/buffer.h b/src/buffer.h
new file mode 100644
index 0000000..35f2bdf
--- /dev/null
+++ b/src/buffer.h
@@ -0,0 +1,27 @@
+#ifndef BUFFER_H
+#define BUFFER_H
+#include <stdbool.h>
+#include <stddef.h>
+
+#define GROW_SIZE 10
+
+typedef struct {
+ char *content;
+ size_t used;
+ size_t size;
+} growable_buffer;
+
+bool init_growable_buffer(growable_buffer *buffer);
+bool grow_growable_buffer(growable_buffer *buffer, size_t at_least);
+void free_growable_buffer(growable_buffer *buffer);
+
+
+typedef struct {
+ char *content;
+ size_t size;
+} fixed_buffer;
+
+bool init_fixed_buffer(fixed_buffer *buffer, char *orig);
+void free_fixed_buffer(fixed_buffer *buffer);
+
+#endif // BUFFER_H
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..3cc9dc4
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,3 @@
+int main(int argc, char *argv[]){
+ return 0;
+}
diff --git a/src/piece-table.c b/src/piece-table.c
new file mode 100644
index 0000000..490950b
--- /dev/null
+++ b/src/piece-table.c
@@ -0,0 +1,54 @@
+#include "piece-table.h"
+
+static piece *make_piece() {
+ return malloc(sizeof(piece));
+}
+
+static piece *make_sentinel() {
+ piece *p = make_piece();
+ p->length = 0;
+ p->next = NULL;
+ p->prev = NULL;
+ p->start = NULL;
+ return p;
+}
+
+bool init_piece_table(piece_table *pt, char *orig) {
+ piece *pc, *sentinel;
+
+ init_growable_buffer(&pt->add);
+ init_fixed_buffer(&pt->orig, orig);
+
+ pt->length = pt->orig.size;
+
+ pc = make_piece();
+ if(pc == NULL){
+ return false;
+ }
+ sentinel = make_sentinel();
+ if(sentinel == NULL){
+ return false;
+ }
+ pc->start = pt->orig.content;
+ pc->length = pt->orig.size;
+ pc->prev = sentinel;
+ pc->next = sentinel;
+
+ pt->cached_offset = 0;
+ pt->cached = pc;
+ return true;
+}
+
+void free_piece_table(piece_table *pt) {
+ free_growable_buffer(&pt->add);
+ free_fixed_buffer(&pt->orig);
+ free_piece_list(pt->sentinel);
+ pt->sentinel = NULL;
+ pt->cached = NULL;
+ pt->cached_offset = 0;
+ pt->length = 0;
+}
+
+void free_piece_list(piece *pt) {
+ // TODO
+}
diff --git a/src/piece-table.h b/src/piece-table.h
new file mode 100644
index 0000000..94ab072
--- /dev/null
+++ b/src/piece-table.h
@@ -0,0 +1,32 @@
+#ifndef PIECE_TABLE_H
+#define PIECE_TABLE_H
+
+#include <malloc.h>
+#include <stddef.h>
+#include "buffer.h"
+
+struct piece {
+ char *start;
+ size_t length;
+ struct piece *next;
+ struct piece *prev;
+};
+
+typedef struct piece piece;
+
+typedef struct {
+ fixed_buffer orig;
+ growable_buffer add;
+ piece *sentinel;
+ size_t length;
+
+ piece *cached;
+ size_t cached_offset;
+} piece_table;
+
+bool init_piece_table(piece_table *pt, char *orig);
+void free_piece_table(piece_table *pt);
+
+void free_piece_list(piece *pt);
+
+#endif // PIECE_TABLE_H
diff --git a/tests/buffer.c b/tests/buffer.c
new file mode 100644
index 0000000..f38a95f
--- /dev/null
+++ b/tests/buffer.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+#include "../src/buffer.h"
+
+int main (int argc, char *argv[]) {
+ growable_buffer buf;
+ init_growable_buffer(&buf);
+ grow_growable_buffer(&buf, 1000);
+ if (buf.size != 1000){
+ printf("HOLA\n");
+ return 1;
+ }
+ free_growable_buffer(&buf);
+ return 0;
+}