From 415b0bcdd9c4f4797fab478cd57b0b361d55b678 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 26 Mar 2018 13:34:44 -0400 Subject: [PATCH] lock buffer in binary mode in prep for rework of loading and saving --- Makefile | 1 - lib/buf.c | 19 ++++--------------- lib/filetype.c | 36 ------------------------------------ 3 files changed, 4 insertions(+), 52 deletions(-) delete mode 100644 lib/filetype.c diff --git a/Makefile b/Makefile index 8841a2c..4d1a0ac 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,6 @@ MAN1 = docs/tide.1 docs/pick.1 docs/picktag.1 docs/pickfile.1 LIBEDIT_OBJS = \ lib/buf.o \ - lib/filetype.o \ lib/utf8.o \ lib/utils.o \ lib/job.o \ diff --git a/lib/buf.c b/lib/buf.c index 43050b1..b712569 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -32,7 +32,7 @@ void buf_init(Buf* buf, void (*errfn)(char*)) { buf->modified = false; buf->expand_tabs = ExpandTabs; buf->copy_indent = CopyIndent; - buf->charset = UTF_8; + buf->charset = BINARY; buf->crlf = 0; buf->bufsize = 8192; buf->bufstart = (Rune*)malloc(buf->bufsize * sizeof(Rune)); @@ -56,20 +56,9 @@ size_t buf_load(Buf* buf, char* path) { /* load the file and determine the character set */ FMap file = mmap_readonly(buf->path); - filetype(buf, file); - - /* read the file contents into the buffer */ buf_resize(buf, next_size(file.len)); - for (size_t i = 0; i < file.len;) { - Rune r; - if (buf->charset == BINARY) { - r = file.buf[i++]; - } else { - size_t len = 0; - while (!utf8decode(&r, &len, file.buf[i++])); - } - buf_insert(buf, false, buf_end(buf), r); - } + for (size_t i = 0; i < file.len;) + buf_insert(buf, false, buf_end(buf), file.buf[i++]); /* jump to address if we got one */ if (addr) @@ -94,7 +83,7 @@ void buf_save(Buf* buf) { /* text files should always end in a new line. If we detected a binary file or at least a non-utf8 file, skip this part. */ - if (!buf_iseol(buf, buf_end(buf)-1) && (buf->charset != BINARY)) + if (!buf_iseol(buf, buf_end(buf)-1)) buf_insert(buf, false, buf_end(buf), '\n'); size_t wrlen = 0; diff --git a/lib/filetype.c b/lib/filetype.c deleted file mode 100644 index 0670c90..0000000 --- a/lib/filetype.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include - -static const char Utf8Valid[256] = { - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0, -}; - -void filetype(Buf* buf, FMap file) { - size_t crs = 0, lfs = 0, tabs = 0; - /* look for bytes that are invalid in utf-8 and count tabs, carriage - returns, and line feeds */ - int type = buf->charset; - for (size_t i = 0; i < file.len; i++) { - if (type == UTF_8) - type &= Utf8Valid[(int)file.buf[i]]; - switch(file.buf[i]) { - case '\r': crs++; break; - case '\n': lfs++; break; - case '\t': tabs++; break; - } - } - - /* setup filetype attributes in the buffer */ - if (crs || lfs) - buf->crlf = (crs == lfs); - buf->charset = type; - buf->expand_tabs = (tabs == 0); -} -- 2.54.0