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
commit3106ccf7551ef1dc8b0279b032b8c53655fe3c42 (patch)
treed80585c6099857af440460e504f92255a1c4001a /bin/edi/edi.c
parentImplement deserialization in edi (diff)
downloadsrc-3106ccf7551ef1dc8b0279b032b8c53655fe3c42.tar.gz
src-3106ccf7551ef1dc8b0279b032b8c53655fe3c42.zip
Return enum Error from file functions in edi
Diffstat (limited to 'bin/edi/edi.c')
-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 9418f1d9..1ef2e055 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);
 }