#include #include #include #include "mem.h" #include "slice.h" #include "vector.h" #define SEGMENT_LEN 8192 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; } 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_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) { assert("TODO" == 0); } void eb_newline(struct ed_buf *eb) { assert("TODO" == 0); } /* set cursor */ void eb_set_cur_prev_line(struct ed_buf *eb) { assert("TODO" == 0); } void eb_set_cur_backward(struct ed_buf *eb) { assert("TODO" == 0); } void eb_set_cur_next_line(struct ed_buf *eb) { assert("TODO" == 0); } 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) { assert("TODO" == 0); } 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) { assert("TODO" == 0); } 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) { assert("TODO" == 0); } void eb_load_file(struct ed_buf *eb) { assert("TODO" == 0); } void eb_save_file(struct ed_buf *eb) { assert("TODO" == 0); }