]> git.mdlowis.com Git - projs/tide.git/commitdiff
lock buffer in binary mode in prep for rework of loading and saving
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 26 Mar 2018 17:34:44 +0000 (13:34 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 26 Mar 2018 17:34:44 +0000 (13:34 -0400)
Makefile
lib/buf.c
lib/filetype.c [deleted file]

index 8841a2cc07abb818b5f2e9c8f18ebe069d6d4679..4d1a0ac67f2c85384234cfd06244085dc173099f 100644 (file)
--- 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      \
index 43050b1794e44621530e58ee3ad3a12b111097d3..b7125697ea0805d262b91836be370a4d5a3d0a2a 100644 (file)
--- 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 (file)
index 0670c90..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <stdc.h>
-#include <utf.h>
-#include <edit.h>
-
-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);
-}