summaryrefslogtreecommitdiff
path: root/buffer.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-03 20:55:42 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-03 20:55:42 -0500
commit16ab3629d435d52539b6345d79433e27c367ce83 (patch)
tree44fb3c7a13870dbd748e0f35d5236d6b3a31cb9a /buffer.c
parent35cb8b8cfbd141d4ba2e3f1435a9550864d9e2e1 (diff)
make install, command line arguments
Diffstat (limited to 'buffer.c')
-rw-r--r--buffer.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/buffer.c b/buffer.c
index 8ad3733..5fbcd2e 100644
--- a/buffer.c
+++ b/buffer.c
@@ -448,6 +448,7 @@ static void buffer_line_free(Line *line) {
free(line->str);
}
+// Free a buffer. Once a buffer is freed, it can be used again for other purposes.
// Does not free the pointer `buffer` (buffer might not have even been allocated with malloc)
void buffer_free(TextBuffer *buffer) {
Line *lines = buffer->lines;
@@ -465,16 +466,23 @@ void buffer_free(TextBuffer *buffer) {
arr_free(buffer->undo_history);
arr_free(buffer->redo_history);
+ Settings *settings = buffer->settings;
+ Font *font = buffer->font;
+
// zero buffer, except for error
char error[sizeof buffer->error];
memcpy(error, buffer->error, sizeof error);
memset(buffer, 0, sizeof *buffer);
memcpy(buffer->error, error, sizeof error);
+
+ buffer_create(buffer, font, settings);
}
// filename must be around for at least as long as the buffer is.
-Status buffer_load_file(TextBuffer *buffer, char const *filename) {
+void buffer_load_file(TextBuffer *buffer, char const *filename) {
+ buffer_free(buffer);
+
buffer->filename = filename;
FILE *fp = fopen(filename, "rb");
if (fp) {
@@ -483,7 +491,7 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) {
fseek(fp, 0, SEEK_SET);
if (file_size > 10L<<20) {
buffer_seterr(buffer, "File too big (size: %zu).", file_size);
- return false;
+ return;
}
u8 *file_contents = buffer_calloc(buffer, 1, file_size);
@@ -544,13 +552,20 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) {
if (!success) {
buffer_free(buffer);
}
- return success;
} else {
buffer_seterr(buffer, "File %s does not exist.", filename);
- return false;
}
}
+void buffer_new_file(TextBuffer *buffer, char const *filename) {
+ buffer_free(buffer);
+
+ buffer->filename = filename;
+ buffer->lines_capacity = 4;
+ buffer->lines = buffer_calloc(buffer, buffer->lines_capacity, sizeof *buffer->lines);
+ buffer->nlines = 1;
+}
+
bool buffer_save(TextBuffer *buffer) {
FILE *out = fopen(buffer->filename, "wb");
if (out) {
@@ -1743,7 +1758,7 @@ void buffer_render(TextBuffer *buffer, float x1, float y1, float x2, float y2) {
char32_t c = *p;
switch (c) {
- case U'\n': assert(0);
+ case U'\n': assert(0); break;
case U'\r': break; // for CRLF line endings
case U'\t': {
uint tab_width = buffer->settings->tab_width;