diff options
author | June McEnroe <june@causal.agency> | 2020-02-11 18:01:50 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-02-11 18:01:50 -0500 |
commit | c9590bab061fcaa3e11a94eea8c34026a6c7a880 (patch) | |
tree | 49f34bd8ee14183d099dd8cca987618bfdc2c085 | |
parent | Set self.nick to * initially (diff) | |
download | catgirl-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.c | 26 |
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; |