чување 3bc96ad4e58536669049e5932212ce42100546c7
родитељ 64ba48dfe29bbfefacb9d618301587481c928197
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Sat, 19 Feb 2022 00:09:34 +0100
Fix insert_line to correctly deal with DrawState.buffer
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | TODO | | | 2 | ++ |
| M | draw.c | | | 2 | +- |
| M | sled.c | | | 46 | ++++++++++++++++++++++++++++++---------------- |
измењених датотека: 3, додавања: 33(+), брисања: 17(-)
diff --git a/TODO b/TODO
@@ -19,6 +19,8 @@
[ ] Allow creating files (error out if directory not writeable, only
create file when saving)
+ [ ] Add handling of terminal resize event
+
[ ] Add vi H/L
[ ] Go to line #
diff --git a/draw.c b/draw.c
@@ -114,7 +114,7 @@ init_drawstate(struct DrawState* state, char* error, char* filename,
void
free_drawstate(struct DrawState* state)
{
- size_t row = 0;
+ size_t row = 0;
for (row = 0; row < state->buffer_size; row++)
free_bufferline(state->buffer + row);
free(state->buffer);
diff --git a/sled.c b/sled.c
@@ -148,7 +148,7 @@ reread_file(struct DrawState* state)
{
long lineno = 1;
long col = 1;
- size_t row = 0;
+ size_t row = 0;
int result;
for (row = 0; row < state->buffer_size; row++)
@@ -199,8 +199,8 @@ int
load_file(struct DrawState* state, long* lineno, long* col)
{
struct BufferLine* newchunk = NULL;
- struct BufferLine* line = NULL;
- FILE* input = NULL;
+ struct BufferLine* line = NULL;
+ FILE* input = NULL;
char input_line[MAXBUFLINE];
newchunk = calloc(ALLOC_DELTA, sizeof(struct BufferLine));
@@ -856,18 +856,33 @@ erase_to_end(struct DrawState* state)
int
insert_line(struct DrawState* state)
{
- state->rows_count++;
- struct BufferLine* newbuf = realloc(state->buffer,
- state->rows_count * sizeof(struct BufferLine));
- if (!newbuf)
- return 1;
+ struct BufferLine* newchunk = NULL;
+ uint32_t* newtext = NULL;
- state->buffer = newbuf;
+ state->rows_count++;
+ if (state->rows_count == state->buffer_size)
+ {
+ state->buffer_size += ALLOC_DELTA;
+ newchunk = realloc(state->buffer,
+ state->buffer_size * sizeof(struct BufferLine));
+ if (!newchunk)
+ {
+ strcpy(state->error, errors[ERR_CANT_ALLOC]);
+ return 0;
+ }
+ state->buffer = newchunk;
+ }
init_bufferline(&state->buffer[state->rows_count - 1]);
- uint32_t* newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
- if (!newtext)
- return 1;
- state->buffer[state->rows_count - 1].text = newtext;
+ if (!state->buffer[state->rows_count - 1].text)
+ {
+ newtext = calloc(MAXBUFLINE, sizeof(uint32_t));
+ if (!newtext)
+ {
+ strcpy(state->error, errors[ERR_CANT_ALLOC]);
+ return 0;
+ }
+ state->buffer[state->rows_count - 1].text = newtext;
+ }
for (size_t i = state->rows_count - 1; i > state->row + 1; i--)
{
u32_strncpy(state->buffer[i].text, state->buffer[i - 1].text,
@@ -881,7 +896,6 @@ insert_line(struct DrawState* state)
state->buffer[state->row].text[state->column] = 0;
state->buffer[state->row].length
= u32_strlen(state->buffer[state->row].text);
-
state->row++;
if (state->row > state->first_shown_row + (state->maxy - 2))
state->first_shown_row = state->row - state->maxy + 2;
@@ -955,7 +969,7 @@ join_lines(struct DrawState* state, size_t first_line, int forward)
state->buffer[i].length = state->buffer[i + 1].length;
}
/*free_bufferline(state->buffer + state->rows_count - 1);*/
- *state->buffer[state->rows_count - 1].text = 0;
+ *state->buffer[state->rows_count - 1].text = 0;
state->buffer[state->rows_count - 1].length = 0;
state->rows_count--;
/*struct BufferLine* newbuf = realloc(state->buffer,
@@ -963,7 +977,7 @@ join_lines(struct DrawState* state, size_t first_line, int forward)
if (!newbuf)
return NULL;
state->buffer = newbuf;*/
- state->dirty = 1;
+ state->dirty = 1;
return state;
}