summary refs log tree commit diff
path: root/bin/edi/file.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-11-22 16:05:51 -0500
committerJune McEnroe <june@causal.agency>2018-11-22 16:05:51 -0500
commit8ee3459cdb3152b9ef551c97041c725ae816977d (patch)
treeedbbc08d01bed996de6b860cbab2523a3787e795 /bin/edi/file.c
parentReorganize ignored directories (diff)
downloadsrc-8ee3459cdb3152b9ef551c97041c725ae816977d.tar.gz
src-8ee3459cdb3152b9ef551c97041c725ae816977d.zip
Simplify edi file reading
Diffstat (limited to '')
-rw-r--r--bin/edi/file.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/bin/edi/file.c b/bin/edi/file.c
index bfab9293..834730b1 100644
--- a/bin/edi/file.c
+++ b/bin/edi/file.c
@@ -25,14 +25,15 @@
 #include "edi.h"
 
 enum {
-	BufCap = 8192,
+	BufferCap = 8192,
+	TableCap = 2,
 	LogCap = 8,
 };
 
 struct File fileAlloc(char *path) {
 	struct File file = {
 		.path = path,
-		.buf = bufferAlloc(BufCap),
+		.buf = bufferAlloc(BufferCap),
 		.log = logAlloc(LogCap),
 	};
 	if (!path) logPush(&file.log, TableEmpty);
@@ -45,7 +46,7 @@ void fileFree(struct File *file) {
 	free(file->path);
 }
 
-static const mbstate_t MBStateInit;
+static const mbstate_t StateInit;
 
 // TODO: Error handling.
 void fileRead(struct File *file) {
@@ -59,22 +60,19 @@ void fileRead(struct File *file) {
 		return;
 	}
 
-	struct Table table = tableAlloc(1);
-
-	char buf[BufCap];
-	mbstate_t mbState = MBStateInit;
+	struct Table table = tableAlloc(TableCap);
+	char buf[BufferCap];
+	mbstate_t state = StateInit;
 	while (!feof(stream)) {
 		size_t mbsLen = fread(buf, 1, sizeof(buf), stream);
 		if (ferror(stream)) err(EX_IOERR, "%s", file->path);
 
 		const char *mbs = buf;
-		mbstate_t mbLenState = mbState;
-		size_t wcsLen = mbsnrtowcs(NULL, &mbs, mbsLen, 0, &mbLenState);
+		wchar_t *wcs = bufferDest(&file->buf, mbsLen);
+		size_t wcsLen = mbsnrtowcs(wcs, &mbs, mbsLen, mbsLen, &state);
 		if (wcsLen == (size_t)-1) err(EX_DATAERR, "%s", file->path);
 
-		wchar_t *wcs = bufferDest(&file->buf, wcsLen);
-		assert(wcsLen == mbsnrtowcs(wcs, &mbs, mbsLen, wcsLen, &mbState));
-
+		bufferTruncate(&file->buf, wcsLen);
 		tablePush(&table, file->buf.slice);
 	}
 	logPush(&file->log, table);