blob: b863a1699b5b0607138a5a7152d978290c5b139f (
plain) (
tree)
|
|
/* Copyright (c) 2021 nytpu <alex@nytpu.com>
* SPDX-License-Identifier: GPL-3.0-only
* For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.
*/
#include "buffer.h"
#include "common.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
bool
in_bounds(RowNum at, RowNum top)
{
if (at < 1 || at > top) return false;
return true;
}
void
insert_row(RowNum at, char *r, size_t len)
{
if (!in_bounds(at, E.numrows+1)) return;
--at;
E.rows = realloc(E.rows, (E.numrows+1) * sizeof(struct Row));
if (at != E.numrows) {
memmove(&E.rows[at+1], &E.rows[at], (E.numrows-at) * sizeof(struct Row));
for (RowNum i = at+1; i <= E.numrows; ++i) ++E.rows[i].idx;
}
E.rows[at].size = len;
E.rows[at].chars = malloc(len + 1);
memcpy(E.rows[at].chars, r, len);
E.rows[at].chars[len] = '\0';
++E.numrows;
E.dirty = true;
}
void
delete_row(RowNum at)
{
if (!in_bounds(at, E.numrows)) return;
--at;
free(E.rows[at].chars);
memmove(&E.rows[at], &E.rows[at+1], (E.numrows-at-1) * sizeof(struct Row));
for (RowNum i = at; i < E.numrows; ++i) --E.rows[i].idx;
--E.numrows;
E.dirty = true;
}
void
delete_rows(RowNum from, RowNum to)
{
if (!in_bounds(from, E.numrows)) return;
if (!in_bounds(to, E.numrows)) return;
for (; from <= to; ++from) delete_row(from);
}
|