diff options
| -rw-r--r-- | editor_piece.c | 127 |
1 files changed, 107 insertions, 20 deletions
diff --git a/editor_piece.c b/editor_piece.c index 817dda2..572da7e 100644 --- a/editor_piece.c +++ b/editor_piece.c @@ -1,76 +1,163 @@ +#include <sys/mman.h> + #include <stddef.h> #include <assert.h> +#include "mem.h" #include "slice.h" +#include "vector.h" -struct ed_buf; +#define SEGMENT_LEN 8192 -void eb_init(struct ed_buf **eb) { - assert("TODO" == 0); +DEFINE_VECTOR(Vec_voidp, void *); + +struct add_buf { + Vec_voidp *segments; + size_t size; +}; + +static void ab_init(struct add_buf **ab) +{ + struct add_buf *nab = mem_malloc(sizeof *nab); + + Vec_voidp_init(&nab->segments); + nab->size = 0; + *ab = nab; } -void eb_free(struct ed_buf *eb) { - assert("TODO" == 0); +static void ab_free(struct add_buf *ab) +{ + size_t len = Vec_voidp_len(ab->segments); + + for (size_t i = 0; i < len; i++) + munmap(Vec_voidp_get(ab->segments, i), SEGMENT_LEN); + + Vec_voidp_free(ab->segments); + mem_free(ab); +} + +inline static char *new_segment() +{ + return mmap(NULL, SEGMENT_LEN, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); +} + +static void ab_push(struct add_buf *ab, char ch) +{ + if (++ab->size > Vec_voidp_len(ab->segments) * SEGMENT_LEN) + Vec_voidp_push(ab->segments, new_segment()); + + char *seg = (char *)Vec_voidp_get(ab->segments, ab->size / SEGMENT_LEN); + seg[ab->size % SEGMENT_LEN] = ch; +} + +struct piece { + size_t is_origin:1; + + /* padding */ + + size_t start_index; + size_t length; +}; + +struct ed_buf { + Vec_voidp *ptab; + const char *origin; + struct add_buf *add; +}; + +void eb_init(struct ed_buf **eb) +{ + Vec_voidp *ptab; + Vec_voidp_init(&ptab); + + struct ed_buf *neb = mem_malloc(sizeof *neb); + neb->ptab = ptab; + neb->origin = NULL; + ab_init(&neb->add); + + *eb = neb; } -void eb_insert(struct ed_buf *eb, int ch) { +void eb_free(struct ed_buf *eb) +{ + Vec_voidp_free(eb->ptab); + ab_free(eb->add); + + free(eb); +} + +void eb_insert(struct ed_buf *eb, int ch) +{ assert("TODO" == 0); } -void eb_backspace(struct ed_buf *eb) { +void eb_backspace(struct ed_buf *eb) +{ assert("TODO" == 0); } -void eb_newline(struct ed_buf *eb) { +void eb_newline(struct ed_buf *eb) +{ assert("TODO" == 0); } /* set cursor */ -void eb_set_cur_prev_line(struct ed_buf *eb) { +void eb_set_cur_prev_line(struct ed_buf *eb) +{ assert("TODO" == 0); } -void eb_set_cur_backward(struct ed_buf *eb) { +void eb_set_cur_backward(struct ed_buf *eb) +{ assert("TODO" == 0); } -void eb_set_cur_next_line(struct ed_buf *eb) { +void eb_set_cur_next_line(struct ed_buf *eb) +{ assert("TODO" == 0); } -void eb_set_cur_forward(struct ed_buf *eb) { +void eb_set_cur_forward(struct ed_buf *eb) +{ assert("TODO" == 0); } /* get */ -void eb_get_line_slice(const struct ed_buf *eb, size_t pos, struct slice *sl) { +void eb_get_line_slice(const struct ed_buf *eb, size_t pos, struct slice *sl) +{ assert("TODO" == 0); } -size_t eb_get_cur_col(const struct ed_buf *eb) { +size_t eb_get_cur_col(const struct ed_buf *eb) +{ assert("TODO" == 0); } -size_t eb_get_cur_row(const struct ed_buf *eb) { +size_t eb_get_cur_row(const struct ed_buf *eb) +{ assert("TODO" == 0); } -size_t eb_get_line_num(const struct ed_buf *eb) { +size_t eb_get_line_num(const struct ed_buf *eb) +{ assert("TODO" == 0); } /* io */ -void eb_bind(struct ed_buf *eb, const char *path) { +void eb_bind(struct ed_buf *eb, const char *path) +{ assert("TODO" == 0); } -void eb_load_file(struct ed_buf *eb) { +void eb_load_file(struct ed_buf *eb) +{ assert("TODO" == 0); } -void eb_save_file(struct ed_buf *eb) { +void eb_save_file(struct ed_buf *eb) +{ assert("TODO" == 0); } - |
