about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-11 18:01:50 -0500
committerJune McEnroe <june@causal.agency>2020-02-11 18:01:50 -0500
commitc9590bab061fcaa3e11a94eea8c34026a6c7a880 (patch)
tree49f34bd8ee14183d099dd8cca987618bfdc2c085
parentSet self.nick to * initially (diff)
downloadcatgirl-c9590bab061fcaa3e11a94eea8c34026a6c7a880.tar.gz
catgirl-c9590bab061fcaa3e11a94eea8c34026a6c7a880.zip
Use time_t for save signature
It's actually more likely to be 64-bit than size_t anyway, and it
eliminates some helper functions.

Also don't error when reading an empty save file.
-rw-r--r--ui.c26
1 files changed, 11 insertions, 15 deletions
diff --git a/ui.c b/ui.c
index cb4115b..29d5583 100644
--- a/ui.c
+++ b/ui.c
@@ -885,20 +885,17 @@ void uiRead(void) {
 	inputUpdate();
 }
 
-static const size_t Signatures[] = {
+static const time_t Signatures[] = {
 	0x6C72696774616301,
 };
 
-static size_t signatureVersion(size_t signature) {
+static size_t signatureVersion(time_t signature) {
 	for (size_t i = 0; i < ARRAY_LEN(Signatures); ++i) {
 		if (signature == Signatures[i]) return i;
 	}
-	err(EX_DATAERR, "unknown file signature %zX", signature);
+	err(EX_DATAERR, "unknown file signature %jX", (uintmax_t)signature);
 }
 
-static int writeSize(FILE *file, size_t value) {
-	return (fwrite(&value, sizeof(value), 1, file) ? 0 : -1);
-}
 static int writeTime(FILE *file, time_t time) {
 	return (fwrite(&time, sizeof(time), 1, file) ? 0 : -1);
 }
@@ -910,7 +907,7 @@ int uiSave(const char *name) {
 	FILE *file = dataOpen(name, "w");
 	if (!file) return -1;
 
-	if (writeSize(file, Signatures[0])) return -1;
+	if (writeTime(file, Signatures[0])) return -1;
 	const struct Window *window;
 	for (window = windows.head; window; window = window->next) {
 		if (writeString(file, idNames[window->id])) return -1;
@@ -926,13 +923,6 @@ int uiSave(const char *name) {
 	return fclose(file);
 }
 
-static size_t readSize(FILE *file) {
-	size_t value;
-	fread(&value, sizeof(value), 1, file);
-	if (ferror(file)) err(EX_IOERR, "fread");
-	if (feof(file)) errx(EX_DATAERR, "unexpected eof");
-	return value;
-}
 static time_t readTime(FILE *file) {
 	time_t time;
 	fread(&time, sizeof(time), 1, file);
@@ -956,7 +946,13 @@ void uiLoad(const char *name) {
 		return;
 	}
 
-	size_t signature = readSize(file);
+	time_t signature;
+	fread(&signature, sizeof(signature), 1, file);
+	if (ferror(file)) err(EX_IOERR, "fread");
+	if (feof(file)) {
+		fclose(file);
+		return;
+	}
 	signatureVersion(signature);
 
 	char *buf = NULL;
10&follow=1'>Factor out commandAvailableJune McEnroe 2022-02-23Give examples of "general events" 2.1June McEnroe 2022-02-23Add missing unistd.h include in input.cJune McEnroe 2022-02-22Document the interfaceJune McEnroe 2022-02-20Clean up unimplemented editing mode stuffJune McEnroe 2022-02-20Save input buffer contentsJune McEnroe 2022-02-20Share a cut buffer between all edit buffersJune McEnroe 2022-02-20Assert return values in edit testsJune McEnroe 2022-02-20Move mbs out of struct Edit, use a global bufferJune McEnroe This saves 4K in the edit buffers, not to mention all the heap allocations for the separate mbs buffers! There might be a way to be more clever about capacities, but I don't think it's worth it. 2022-02-20Clear edit buffer before running commandJune McEnroe Otherwise a command that switches windows will update the status line while the edit buffer still has input "pending", showing an indicator. 2022-02-20Show indicator in status when window has pending inputJune McEnroe 2022-02-20Use separate edit buffers for each IDJune McEnroe 2022-02-20Make sure new cap is actually larger than new lengthJune McEnroe 2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe