summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEkaitz Zarraga <ekaitz@elenq.tech>2024-02-28 22:03:04 +0100
committerEkaitz Zarraga <ekaitz@elenq.tech>2024-02-28 22:08:20 +0100
commita1f6afa69bb25191d4b206e4ecba1557629330bc (patch)
tree3041324b6f0932ef036e9de230e965a4400f2b08 /src
First commit with project structure
Diffstat (limited to 'src')
-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
5 files changed, 162 insertions, 0 deletions
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