]> git.mdlowis.com Git - projs/tide.git/commitdiff
General cleanup and commenting
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 15 Oct 2016 02:33:30 +0000 (22:33 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 15 Oct 2016 02:33:30 +0000 (22:33 -0400)
Makefile
buf.c
charset.c
edit.h
keyboard.c
mouse.c
tests/tests.c
utf8.c
utils.c [new file with mode: 0644]
xedit.c

index 5259d5af14e4c44e51e443d502d5fa4906cc247d..71c0a42dbf265b1f6daad54ba56835fc379b97bf 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 LDFLAGS  = -L/opt/X11/lib -lX11 -lXft -lfontconfig
 CFLAGS   = --std=gnu99 -Wall -Wextra -I. -I/opt/X11/include -I/opt/local/include/freetype2 -I/usr/include/freetype2
-OBJS     = buf.o screen.o utf8.o keyboard.o mouse.o charset.o
+OBJS     = buf.o screen.o utf8.o keyboard.o mouse.o charset.o utils.o
 TESTOBJS = tests/tests.o tests/buf.o tests/utf8.o
 
 all: edit test
diff --git a/buf.c b/buf.c
index 7a523bf3ae0d0ab5803f12b6c15ef85e4c270e89..c7e8f49c1aa870e64b7bf246328937f9060d4f14 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -1,64 +1,9 @@
 #define _GNU_SOURCE
 #include <string.h>
 #include <assert.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 
 #include "edit.h"
 
-typedef struct {
-    uint8_t* buf;
-    size_t len;
-} FMap;
-
-static FMap fmap(char* path) {
-    int fd;
-    FMap file = { .buf = NULL, .len = 0 };
-    struct stat sb;
-    if (((fd = open(path, O_RDONLY, 0)) < 0) ||
-        (fstat(fd, &sb) < 0) ||
-        (sb.st_size == 0))
-        return file;
-    file.buf = (uint8_t*)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
-    file.len = sb.st_size;
-    if (file.buf == MAP_FAILED)
-        die("memory mapping of file failed");
-    return file;
-}
-
-static void funmap(FMap file) {
-    if (file.buf)
-        munmap(file.buf, file.len);
-}
-
-static void utf8load(Buf* buf, FMap file) {
-    for (size_t i = 0; i < file.len;) {
-        Rune r = 0;
-        size_t len = 0;
-        while (!utf8decode(&r, &len, file.buf[i++]));
-        buf_ins(buf, buf_end(buf), r);
-    }
-}
-
-static void utf8save(Buf* buf, FILE* file) {
-    unsigned end = buf_end(buf);
-    for (unsigned i = 0; i < end; i++)
-        fputrune(buf_get(buf, i), file);
-}
-
-static void binload(Buf* buf, FMap file) {
-    for (size_t i = 0; i < file.len; i++)
-        buf_ins(buf, buf_end(buf), file.buf[i]);
-}
-
-static void binsave(Buf* buf, FILE* file) {
-    unsigned end = buf_end(buf);
-    for (unsigned i = 0; i < end; i++)
-        fputc((int)buf_get(buf, i), file);
-}
-
 void buf_load(Buf* buf, char* path) {
     buf->insert_mode = true;
     if (!strcmp(path,"-")) {
index 24bf4e35c87057811043c00ea6b69f25e14422f2..e002f1f57485a4897e0b1065574feef4b88a0572 100644 (file)
--- a/charset.c
+++ b/charset.c
@@ -35,3 +35,14 @@ int charset(const uint8_t* buf, size_t len) {
         type = Utf8Valid[(int)buf[i]];
     return type;
 }
+
+void binload(Buf* buf, FMap file) {
+    for (size_t i = 0; i < file.len; i++)
+        buf_ins(buf, buf_end(buf), file.buf[i]);
+}
+
+void binsave(Buf* buf, FILE* file) {
+    unsigned end = buf_end(buf);
+    for (unsigned i = 0; i < end; i++)
+        fputc((int)buf_get(buf, i), file);
+}
diff --git a/edit.h b/edit.h
index 5fa99f1dd38e65834d507b633e81f73457406d2a..3bcef0df071d6c9623237416de19608c45820ffa 100644 (file)
--- a/edit.h
+++ b/edit.h
@@ -5,7 +5,19 @@
 #include <stdarg.h>
 #include <string.h>
 
-/* Charset Handling
+/* Utility Functions
+ *****************************************************************************/
+typedef struct {
+    uint8_t* buf; /* memory mapped byte buffer */
+    size_t len;   /* length of the buffer */
+} FMap;
+
+FMap fmap(char* path);
+void funmap(FMap file);
+void die(const char* fmt, ...);
+uint32_t getmillis(void);
+
+/* Unicode Handling
  *****************************************************************************/
 enum {
     UTF_MAX   = 6u,        /* maximum number of bytes that make up a rune */
@@ -18,21 +30,57 @@ enum {
 /* Represents a unicode code point */
 typedef uint32_t Rune;
 
-enum {
-    BINARY = 0,
-    UTF_8,
-    UTF_16BE,
-    UTF_16LE,
-    UTF_32BE,
-    UTF_32LE,
-};
-
-int charset(const uint8_t* buf, size_t len);
 size_t utf8encode(char str[UTF_MAX], Rune rune);
 bool utf8decode(Rune* rune, size_t* length, int byte);
 Rune fgetrune(FILE* f);
 void fputrune(Rune rune, FILE* f);
 
+/* Buffer management functions
+ *****************************************************************************/
+typedef struct buf {
+    char* path;       /* the path to the open file */
+    int charset;      /* the character set of the buffer */
+    bool insert_mode; /* tracks current mode */
+    bool modified;    /* tracks whether the buffer has been modified */
+    size_t bufsize;   /* size of the buffer in runes */
+    Rune* bufstart;   /* start of the data buffer */
+    Rune* bufend;     /* end of the data buffer */
+    Rune* gapstart;   /* start of the gap */
+    Rune* gapend;     /* end of the gap */
+} Buf;
+
+void buf_load(Buf* buf, char* path);
+void buf_save(Buf* buf);
+void buf_init(Buf* buf);
+void buf_clr(Buf* buf);
+void buf_del(Buf* buf, unsigned pos);
+void buf_ins(Buf* buf, unsigned pos, Rune);
+Rune buf_get(Buf* buf, unsigned pos);
+unsigned buf_bol(Buf* buf, unsigned pos);
+unsigned buf_eol(Buf* buf, unsigned pos);
+unsigned buf_end(Buf* buf);
+unsigned buf_byrune(Buf* buf, unsigned pos, int count);
+unsigned buf_byline(Buf* buf, unsigned pos, int count);
+unsigned buf_getcol(Buf* buf, unsigned pos);
+unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col);
+
+/* Charset Handling
+ *****************************************************************************/
+enum {
+    BINARY = 0, /* binary encoded file */
+    UTF_8,      /* UTF-8 encoded file */
+    UTF_16BE,   /* UTF-16 encoding, big-endian */
+    UTF_16LE,   /* UTF-16 encoding, little-endian */
+    UTF_32BE,   /* UTF-32 encoding, big-endian */
+    UTF_32LE,   /* UTF-32 encoding, little-endian */
+};
+
+int charset(const uint8_t* buf, size_t len);
+void utf8load(Buf* buf, FMap file);
+void utf8save(Buf* buf, FILE* file);
+void binload(Buf* buf, FMap file);
+void binsave(Buf* buf, FILE* file);
+
 /* Input Handling
  *****************************************************************************/
 /* key definitions */
@@ -129,36 +177,6 @@ typedef struct {
 void handle_key(Rune key);
 void handle_mouse(MouseEvent* mevnt);
 
-/* Buffer management functions
- *****************************************************************************/
-typedef struct buf {
-    char* path;       /* the path to the open file */
-    int charset;      /* the character set of the buffer */
-    bool insert_mode; /* tracks current mode */
-    bool modified;    /* tracks whether the buffer has been modified */
-    size_t bufsize;   /* size of the buffer in runes */
-    Rune* bufstart;   /* start of the data buffer */
-    Rune* bufend;     /* end of the data buffer */
-    Rune* gapstart;   /* start of the gap */
-    Rune* gapend;     /* end of the gap */
-} Buf;
-
-void buf_load(Buf* buf, char* path);
-void buf_save(Buf* buf);
-void buf_init(Buf* buf);
-void buf_clr(Buf* buf);
-void buf_del(Buf* buf, unsigned pos);
-void buf_ins(Buf* buf, unsigned pos, Rune);
-Rune buf_get(Buf* buf, unsigned pos);
-unsigned buf_bol(Buf* buf, unsigned pos);
-unsigned buf_eol(Buf* buf, unsigned pos);
-unsigned buf_end(Buf* buf);
-unsigned buf_byrune(Buf* buf, unsigned pos, int count);
-unsigned buf_byline(Buf* buf, unsigned pos, int count);
-unsigned buf_getcol(Buf* buf, unsigned pos);
-unsigned buf_setcol(Buf* buf, unsigned pos, unsigned col);
-
-
 /* Screen management functions
  *****************************************************************************/
 typedef struct {
@@ -181,10 +199,6 @@ unsigned screen_setcell(unsigned row, unsigned col, Rune r);
 Rune screen_getcell(unsigned row, unsigned col);
 void screen_status(char* fmt, ...);
 
-/* Miscellaneous Functions
- *****************************************************************************/
-void die(const char* fmt, ...);
-
 /* Color Scheme Handling
  *****************************************************************************/
 /* color indexes for the colorscheme */
index b8536e661757013f30d3d8ea415e2635d12a11be..0a970fb40cc70101f4a175793392cdef4da58b81 100644 (file)
@@ -82,7 +82,5 @@ static void control_keys(Rune key) {
 }
 
 static void vi_keys(Rune key) {
-    static unsigned count = 0;
     (void)key;
 }
-
diff --git a/mouse.c b/mouse.c
index c848f6f44b429121d2f1eea924466e24e996ae03..ae3073ce0c0b19fff203e8ff2cf6dbc389c2726b 100644 (file)
--- a/mouse.c
+++ b/mouse.c
@@ -1,31 +1,5 @@
 #include "edit.h"
 
-#define _GNU_SOURCE
-#include <time.h>
-#include <sys/time.h>
-
-#ifdef __MACH__
-#define CLOCK_MONOTONIC 0
-// clock_gettime is not implemented on OSX
-int clock_gettime(int id, struct timespec* t) {
-    (void)id;
-    struct timeval now;
-    int rv = gettimeofday(&now, NULL);
-    if (rv) return rv;
-    t->tv_sec  = now.tv_sec;
-    t->tv_nsec = now.tv_usec * 1000;
-    return 0;
-}
-#endif
-
-uint32_t getmillis(void) {
-    struct timespec time;
-    clock_gettime(CLOCK_MONOTONIC, &time);
-    return ((time.tv_sec * 1000) + (time.tv_nsec / 1000000));
-}
-
-/*****************************************************************************/
-
 void unused(MouseEvent* mevnt) {
     (void)mevnt;
 }
index 2c92b66a2fe14ada89253183487676387c256076..0c037bc71284bfb4060601582ed76830a36129e9 100644 (file)
@@ -7,10 +7,6 @@ unsigned CursorPos;
 unsigned TargetCol;
 enum ColorScheme ColorBase;
 
-void die(const char* m, ...) {
-    (void)m;
-}
-
 int main(int argc, char** argv) {
     atf_init(argc,argv);
     RUN_EXTERN_TEST_SUITE(BufferTests);
diff --git a/utf8.c b/utf8.c
index 6c301e05f64962dc2408c82548e0411fd9533089..dac7d2f2d735d86bb375994eb40597cf820fe4f3 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -83,6 +83,22 @@ Rune fgetrune(FILE* f) {
 
 void fputrune(Rune rune, FILE* f) {
     char utf[UTF_MAX] = {0};
-    utf8encode(utf, rune);
-    fprintf(f, "%s", utf);
+    size_t n = utf8encode(utf, rune);
+    fwrite(utf, 1, n, f);
 }
+
+void utf8load(Buf* buf, FMap file) {
+    for (size_t i = 0; i < file.len;) {
+        Rune r = 0;
+        size_t len = 0;
+        while (!utf8decode(&r, &len, file.buf[i++]));
+        buf_ins(buf, buf_end(buf), r);
+    }
+}
+
+void utf8save(Buf* buf, FILE* file) {
+    unsigned end = buf_end(buf);
+    for (unsigned i = 0; i < end; i++)
+        fputrune(buf_get(buf, i), file);
+}
+
diff --git a/utils.c b/utils.c
new file mode 100644 (file)
index 0000000..33e3392
--- /dev/null
+++ b/utils.c
@@ -0,0 +1,59 @@
+#include "edit.h"
+
+#define _GNU_SOURCE
+#include <time.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifdef __MACH__
+#define CLOCK_MONOTONIC 0
+// clock_gettime is not implemented on OSX
+int clock_gettime(int id, struct timespec* t) {
+    (void)id;
+    struct timeval now;
+    int rv = gettimeofday(&now, NULL);
+    if (rv) return rv;
+    t->tv_sec  = now.tv_sec;
+    t->tv_nsec = now.tv_usec * 1000;
+    return 0;
+}
+#endif
+
+uint32_t getmillis(void) {
+    struct timespec time;
+    clock_gettime(CLOCK_MONOTONIC, &time);
+    return ((time.tv_sec * 1000) + (time.tv_nsec / 1000000));
+}
+
+void die(const char* msgfmt, ...) {
+    va_list args;
+    va_start(args, msgfmt);
+    fprintf(stderr, "Error: ");
+    vfprintf(stderr, msgfmt, args);
+    fprintf(stderr, "\n");
+    va_end(args);
+    exit(EXIT_FAILURE);
+}
+
+FMap fmap(char* path) {
+    int fd;
+    FMap file = { .buf = NULL, .len = 0 };
+    struct stat sb;
+    if (((fd = open(path, O_RDONLY, 0)) < 0) ||
+        (fstat(fd, &sb) < 0) ||
+        (sb.st_size == 0))
+        return file;
+    file.buf = (uint8_t*)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
+    file.len = sb.st_size;
+    if (file.buf == MAP_FAILED)
+        die("memory mapping of file failed");
+    return file;
+}
+
+void funmap(FMap file) {
+    if (file.buf)
+        munmap(file.buf, file.len);
+}
diff --git a/xedit.c b/xedit.c
index 3bc56847da5caadb40a13019a0b6f4f6faa7b701..d2253dc18561df74f0b9a7dab2d54e4c82729bfc 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -30,7 +30,6 @@ struct {
     XIC xic;
     XIM xim;
 } X;
-
 struct {
     struct {
         int height;
@@ -142,16 +141,6 @@ int font_makespecs(XftGlyphFontSpec* specs, const Rune* runes, int len, int x, i
 
 /*****************************************************************************/
 
-void die(const char* msgfmt, ...) {
-    va_list args;
-    va_start(args, msgfmt);
-    fprintf(stderr, "Error: ");
-    vfprintf(stderr, msgfmt, args);
-    fprintf(stderr, "\n");
-    va_end(args);
-    exit(EXIT_FAILURE);
-}
-
 static XftColor xftcolor(enum ColorId cid) {
     Color c = Palette[cid][ColorBase];
     XftColor xc;