From 723876da5741892530cd74112ec1510124e95cf9 Mon Sep 17 00:00:00 2001 From: cflip Date: Wed, 25 Jan 2023 11:28:09 -0700 Subject: Rename `append_buffer` to textbuf This name is a little bit better I think, and it will be nice to have a distinction between this utility and the 'file' kind of buffer. --- Makefile | 2 +- buffer.c | 21 --------------------- buffer.h | 14 -------------- editor.c | 16 ++++++++-------- editor.h | 6 +++--- textbuf.c | 32 ++++++++++++++++++++++++++++++++ textbuf.h | 22 ++++++++++++++++++++++ window.c | 6 +++--- 8 files changed, 69 insertions(+), 50 deletions(-) delete mode 100644 buffer.c delete mode 100644 buffer.h create mode 100644 textbuf.c create mode 100644 textbuf.h diff --git a/Makefile b/Makefile index 46c09b7..5ff6c12 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,6 @@ LFLAGS=-lSDL2 -lz OUT=glypher OBJS=main.o \ - buffer.o \ editor.o \ error.o \ file.o \ @@ -11,6 +10,7 @@ OBJS=main.o \ input.o \ line.o \ syntax.o \ + textbuf.o \ window.o DESTDIR=/usr/local diff --git a/buffer.c b/buffer.c deleted file mode 100644 index 596d5d2..0000000 --- a/buffer.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "buffer.h" - -#include -#include - -void ab_append(struct append_buffer* ab, const char* string, int length) -{ - char* new = realloc(ab->buffer, ab->length + length); - - if (new == NULL) - return; - - memcpy(&new[ab->length], string, length); - ab->buffer = new; - ab->length += length; -} - -void ab_free(struct append_buffer* ab) -{ - free(ab->buffer); -} diff --git a/buffer.h b/buffer.h deleted file mode 100644 index 7761f6c..0000000 --- a/buffer.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _BUFFER_H -#define _BUFFER_H - -struct append_buffer { - char* buffer; - int length; -}; - -#define ABUF_INIT { NULL, 0 } - -void ab_append(struct append_buffer* ab, const char* string, int length); -void ab_free(struct append_buffer* ab); - -#endif diff --git a/editor.c b/editor.c index caa2d24..4a489a2 100644 --- a/editor.c +++ b/editor.c @@ -260,7 +260,7 @@ void editor_update_screen_size(struct editor_state *editor) editor->screen_rows -= 2; } -void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* buffer) +void editor_draw_status_bar(struct editor_state* editor, struct textbuf *buffer) { char status[80], right_status[80]; int length = snprintf(status, sizeof(status), "%.20s - %d lines %s", editor->filename ? editor->filename : "[New File]", editor->num_lines, editor->dirty ? "(modified)" : ""); @@ -269,24 +269,24 @@ void editor_draw_status_bar(struct editor_state* editor, struct append_buffer* b if (length > editor->screen_cols) length = editor->screen_cols; - ab_append(buffer, status, length); + textbuf_append(buffer, status, length); while (length < editor->screen_cols) { if (editor->screen_cols - length == right_length) { - ab_append(buffer, right_status, right_length); + textbuf_append(buffer, right_status, right_length); break; } else { - ab_append(buffer, " ", 1); + textbuf_append(buffer, " ", 1); length++; } } - ab_append(buffer, "\n", 1); + textbuf_append(buffer, "\n", 1); } -void editor_draw_message_bar(struct editor_state* editor, struct append_buffer* buffer) +void editor_draw_message_bar(struct editor_state* editor, struct textbuf *buffer) { if (editor->mode == EDITOR_MODE_INSERT) { - ab_append(buffer, "--INSERT--", 10); + textbuf_append(buffer, "--INSERT--", 10); return; } @@ -296,7 +296,7 @@ void editor_draw_message_bar(struct editor_state* editor, struct append_buffer* message_length = editor->screen_cols; if (message_length && time(NULL) - editor->status_message_time < 5) - ab_append(buffer, editor->status_message, message_length); + textbuf_append(buffer, editor->status_message, message_length); } void editor_destroy(struct editor_state *editor) diff --git a/editor.h b/editor.h index e8e4976..f8fbfa8 100644 --- a/editor.h +++ b/editor.h @@ -3,7 +3,7 @@ #include -#include "buffer.h" +#include "textbuf.h" #include "line.h" enum editor_mode { @@ -55,8 +55,8 @@ void editor_add_line_below(struct editor_state* editor); void editor_find(struct editor_state* editor); void editor_scroll(struct editor_state* editor); void editor_update_screen_size(struct editor_state *); -void editor_draw_status_bar(struct editor_state *editor, struct append_buffer *buffer); -void editor_draw_message_bar(struct editor_state *editor, struct append_buffer *buffer); +void editor_draw_status_bar(struct editor_state *editor, struct textbuf *buffer); +void editor_draw_message_bar(struct editor_state *editor, struct textbuf *buffer); void editor_destroy(struct editor_state *editor); diff --git a/textbuf.c b/textbuf.c new file mode 100644 index 0000000..e68ddbd --- /dev/null +++ b/textbuf.c @@ -0,0 +1,32 @@ +#include "textbuf.h" + +#include +#include + +#include "error.h" + +struct textbuf textbuf_init() +{ + struct textbuf result; + result.buffer = NULL; + result.length = 0; + return result; +} + +void textbuf_append(struct textbuf *textbuf, const char *str, int len) +{ + char *new = realloc(textbuf->buffer, textbuf->length + len); + if (new == NULL) { + fatal_error("Failed to reallocate textbuf!"); + return; + } + + memcpy(&new[textbuf->length], str, len); + textbuf->buffer = new; + textbuf->length += len; +} + +void textbuf_free(struct textbuf *textbuf) +{ + free(textbuf->buffer); +} diff --git a/textbuf.h b/textbuf.h new file mode 100644 index 0000000..e5e0f03 --- /dev/null +++ b/textbuf.h @@ -0,0 +1,22 @@ +/* + * textbuf.h: A simple dynamic text buffer. + * + * This file provides the `textbuf` struct, which is a dynamically allocated + * buffer of text. + */ + +#ifndef _BUFFER_H +#define _BUFFER_H + +#include + +struct textbuf { + char *buffer; + size_t length; +}; + +struct textbuf textbuf_init(); +void textbuf_append(struct textbuf *textbuf, const char *str, int len); +void textbuf_free(struct textbuf *textbuf); + +#endif diff --git a/window.c b/window.c index fbc9a36..9518d69 100644 --- a/window.c +++ b/window.c @@ -3,7 +3,7 @@ #include #include -#include "buffer.h" +#include "textbuf.h" #include "editor.h" #include "error.h" #include "font.h" @@ -145,7 +145,7 @@ static void draw_editor(struct editor_state *editor) } /* Draw the statusline containing file information */ - struct append_buffer statusbuf = ABUF_INIT; + struct textbuf statusbuf = textbuf_init(); editor_draw_status_bar(editor, &statusbuf); editor_draw_message_bar(editor, &statusbuf); @@ -154,7 +154,7 @@ static void draw_editor(struct editor_state *editor) SDL_SetTextureColorMod(font_texture, 0xff, 0xff, 0xff); draw_string(statusbuf.buffer, NULL, statusbuf.length, 0, line_y); - ab_free(&statusbuf); + textbuf_free(&statusbuf); } void window_redraw(struct editor_state *editor) -- cgit v1.2.3