From f83969d3acc160f552bd03c60668ce8e59cffc5c Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 14 Oct 2016 22:33:30 -0400 Subject: [PATCH] General cleanup and commenting --- Makefile | 2 +- buf.c | 55 -------------------------- charset.c | 11 ++++++ edit.h | 104 ++++++++++++++++++++++++++++---------------------- keyboard.c | 2 - mouse.c | 26 ------------- tests/tests.c | 4 -- utf8.c | 20 +++++++++- utils.c | 59 ++++++++++++++++++++++++++++ xedit.c | 11 ------ 10 files changed, 148 insertions(+), 146 deletions(-) create mode 100644 utils.c diff --git a/Makefile b/Makefile index 5259d5a..71c0a42 100644 --- 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 7a523bf..c7e8f49 100644 --- a/buf.c +++ b/buf.c @@ -1,64 +1,9 @@ #define _GNU_SOURCE #include #include -#include -#include -#include -#include #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,"-")) { diff --git a/charset.c b/charset.c index 24bf4e3..e002f1f 100644 --- 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 5fa99f1..3bcef0d 100644 --- a/edit.h +++ b/edit.h @@ -5,7 +5,19 @@ #include #include -/* 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 */ diff --git a/keyboard.c b/keyboard.c index b8536e6..0a970fb 100644 --- a/keyboard.c +++ b/keyboard.c @@ -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 c848f6f..ae3073c 100644 --- a/mouse.c +++ b/mouse.c @@ -1,31 +1,5 @@ #include "edit.h" -#define _GNU_SOURCE -#include -#include - -#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; } diff --git a/tests/tests.c b/tests/tests.c index 2c92b66..0c037bc 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -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 6c301e0..dac7d2f 100644 --- 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 index 0000000..33e3392 --- /dev/null +++ b/utils.c @@ -0,0 +1,59 @@ +#include "edit.h" + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#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 3bc5684..d2253dc 100644 --- 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; -- 2.49.0