summary refs log tree commit diff
path: root/bin/edi/edi.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-11-25 20:53:34 -0500
committerJune McEnroe <june@causal.agency>2018-11-25 20:53:34 -0500
commit065792b3b0968bb38add192976418f1ba1fb40cc (patch)
treecec5761e50bff4dc14d3d6e9901689d768b2bdae /bin/edi/edi.c
parentImplement deserialization in edi (diff)
downloadsrc-065792b3b0968bb38add192976418f1ba1fb40cc.tar.gz
src-065792b3b0968bb38add192976418f1ba1fb40cc.zip
Return enum Error from file functions in edi
Diffstat (limited to '')
-rw-r--r--bin/edi/edi.c34
1 files changed, 16 insertions, 18 deletions
diff --git a/bin/edi/edi.c b/bin/edi/edi.c
index a0231785..d234df5e 100644
--- a/bin/edi/edi.c
+++ b/bin/edi/edi.c
@@ -23,24 +23,27 @@
 
 #include "edi.h"
 
+static void errorExit(enum Error error, const char *prefix) {
+	if (error > Errno) errc(EX_IOERR, error - Errno, "%s", prefix);
+	else errx(EX_DATAERR, "%s: %d", prefix, error);
+}
+
 int main(int argc, char *argv[]) {
 	setlocale(LC_CTYPE, "");
 
 	if (argc < 2) return EX_USAGE;
 
+	enum Error error;
+
 	struct File file = fileAlloc(strdup(argv[1]));
-	fileRead(&file);
-	
-	struct Edit edit = { file.buf, file.log };
+	error = fileRead(&file);
+	if (error) errorExit(error, file.path);
 
 	FILE *store = fopen("store.edi", "w");
 	if (!store) err(EX_CANTCREAT, "store.edi");
 
-	enum Error error = storeWrite(store, &edit);
-	if (error) {
-		if (error > Errno) errc(EX_IOERR, error - Errno, "store.edi");
-		else errx(EX_IOERR, "store.edi: %d", error);
-	}
+	error = storeWrite(store, &file.edit);
+	if (error) errorExit(error, "store.edi");
 
 	fclose(store);
 	if (ferror(store)) err(EX_IOERR, "store.edi");
@@ -48,21 +51,16 @@ int main(int argc, char *argv[]) {
 	store = fopen("store.edi", "r");
 	if (!store) err(EX_CANTCREAT, "store.edi");
 
-	error = storeRead(store, &edit);
-	if (error) {
-		if (error > Errno) errc(EX_IOERR, error - Errno, "store.edi");
-		else errx(EX_DATAERR, "store.edi: %d", error);
-	}
-
-	file.buf = edit.buf;
-	file.log = edit.log;
+	error = storeRead(store, &file.edit);
+	if (error) errorExit(error, "store.edi");
 
-	const struct Table *table = logTable(&file.log);
+	const struct Table *table = logTable(&file.edit.log);
 	for (struct Iter it = iter(table, 0); it.ch != WEOF; it = iterNext(it)) {
 		printf("%lc", it.ch);
 	}
 
-	fileWrite(&file);
+	error = fileWrite(&file);
+	if (error) errorExit(error, file.path);
 
 	fileFree(&file);
 }