diff options
Diffstat (limited to 'src/piece-table.c')
-rw-r--r-- | src/piece-table.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/piece-table.c b/src/piece-table.c index 6db05d7..2e7f0a2 100644 --- a/src/piece-table.c +++ b/src/piece-table.c @@ -138,16 +138,37 @@ static piece *split_piece(piece *p, size_t pos) { return p; } -void insert_piece_table(piece_table *pt, char val, size_t pos) { - /* At the moment inserts only one char */ - // piece *cur; - assert(pos <= pt->length ); +void insert_piece_table(piece_table *pt, size_t pos, char *in, size_t in_len) { + /* TODO: Make sure it doesn't screw up the cache */ + piece *start, *end, *new; - append_growable_buffer(&pt->add, &val, 1); - // TODO + assert( pos <= pt->length ); + + append_growable_buffer(&pt->add, in, in_len); + + if ( pos == 0 ) { + start = pt->sentinel; + } else if ( pos == pt->length ) { + start = pt->sentinel->prev; + } else { + find_piece_by_pos(pt, pos); + start = split_piece(pt->cached, pos - pt->cached_offset); + } + end = start->next; + + new = make_piece(); + new->start = pt->add.content + pt->add.used - in_len; + new->length = in_len; + + start->next = new; + new->prev = start; + new->next = end; + end->prev = new; + pt->length += in_len; } void delete_piece_table(piece_table *pt, size_t pos, size_t len) { + /* TODO: Make sure it doesn't screw up the cache */ piece *cur, *next, *start, *end; assert( pos + len <= pt->length ); |