cleans up the add row code a bit
This commit is contained in:
parent
16913585f4
commit
7f608c0013
@ -12,6 +12,9 @@ typedef enum {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
char* line;
|
char* line;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
|
char* render;
|
||||||
|
size_t r_len;
|
||||||
} row_t;
|
} row_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -109,6 +112,8 @@ void screen_buffer_free(screen_buffer_t* buf);
|
|||||||
*/
|
*/
|
||||||
void free_editor_row(void);
|
void free_editor_row(void);
|
||||||
|
|
||||||
|
void editor_render_row(row_t* row);
|
||||||
|
|
||||||
void editor_add_row(const char* line, size_t len);
|
void editor_add_row(const char* line, size_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
21
src/term.c
21
src/term.c
@ -164,6 +164,23 @@ void free_editor_row(void)
|
|||||||
free(editor.rows);
|
free(editor.rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void editor_render_row(row_t* row)
|
||||||
|
{
|
||||||
|
if (row->render != NULL) // if the row is malloc'd we want to delete it
|
||||||
|
free(row->render);
|
||||||
|
row->render = malloc(sizeof(char) * row->len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// small helper function to initialize a row
|
||||||
|
void create_row(row_t* row, size_t len)
|
||||||
|
{
|
||||||
|
row->len = len;
|
||||||
|
row->line = malloc(sizeof(char) * (len + 1));
|
||||||
|
|
||||||
|
row->render = NULL;
|
||||||
|
row->r_len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void editor_add_row(const char* line, size_t len)
|
void editor_add_row(const char* line, size_t len)
|
||||||
{
|
{
|
||||||
// Need to add space for another row
|
// Need to add space for another row
|
||||||
@ -171,8 +188,8 @@ void editor_add_row(const char* line, size_t len)
|
|||||||
|
|
||||||
// Set len and malloc space for the incoming line +1 for \0
|
// Set len and malloc space for the incoming line +1 for \0
|
||||||
size_t idx = editor.num_rows;
|
size_t idx = editor.num_rows;
|
||||||
editor.rows[idx].len = len;
|
|
||||||
editor.rows[idx].line = malloc(sizeof(char) * (len + 1));
|
create_row(&editor.rows[idx], len);
|
||||||
|
|
||||||
memcpy(editor.rows[idx].line, line, len);
|
memcpy(editor.rows[idx].line, line, len);
|
||||||
editor.rows[idx].line[len] = '\0'; // need to null terminate it
|
editor.rows[idx].line[len] = '\0'; // need to null terminate it
|
||||||
|
Loading…
x
Reference in New Issue
Block a user