summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe2022-02-20 16:57:11 -0500
committerJune McEnroe2022-02-20 16:57:11 -0500
commit78ff548b934bb265437e3afeea78942c601691e6 (patch)
treeb3bbfc5b326bd93204aa9ffb451130e78e256576
parentda1b943fccc426bc43caa7e7e51208833b9fce66 (diff)
Save input buffer contents
-rw-r--r--chat.h2
-rw-r--r--input.c39
-rw-r--r--ui.c7
3 files changed, 46 insertions, 2 deletions
diff --git a/chat.h b/chat.h
index 17334fa..1c46f00 100644
--- a/chat.h
+++ b/chat.h
@@ -334,6 +334,8 @@ void inputUpdate(void);
 bool inputPending(uint id);
 void inputRead(void);
 void inputCompleteAdd(void);
+int inputSave(FILE *file);
+void inputLoad(FILE *file, size_t version);
 
 enum Scroll {
 	ScrollOne,
diff --git a/input.c b/input.c
index b6c51a2..de8cecf 100644
--- a/input.c
+++ b/input.c
@@ -586,3 +586,42 @@ void inputRead(void) {
 	}
 	inputUpdate();
 }
+
+static int writeString(FILE *file, const char *str) {
+	return (fwrite(str, strlen(str) + 1, 1, file) ? 0 : -1);
+}
+
+int inputSave(FILE *file) {
+	int error;
+	for (uint id = 0; id < IDCap; ++id) {
+		if (!edits[id].len) continue;
+		char *ptr = editString(&edits[id], &buf, &cap, NULL);
+		if (!ptr) return -1;
+		error = 0
+			|| writeString(file, idNames[id])
+			|| writeString(file, ptr);
+		if (error) return error;
+	}
+	return writeString(file, "");
+}
+
+static ssize_t readString(FILE *file, char **buf, size_t *cap) {
+	ssize_t len = getdelim(buf, cap, '\0', file);
+	if (len < 0 && !feof(file)) err(EX_IOERR, "getdelim");
+	return len;
+}
+
+void inputLoad(FILE *file, size_t version) {
+	if (version < 8) return;
+	while (0 < readString(file, &buf, &cap) && buf[0]) {
+		uint id = idFor(buf);
+		readString(file, &buf, &cap);
+		size_t max = strlen(buf);
+		int error = editReserve(&edits[id], 0, max);
+		if (error) err(EX_OSERR, "editReserve");
+		size_t len = mbstowcs(edits[id].buf, buf, max);
+		assert(len != (size_t)-1);
+		edits[id].len = len;
+		edits[id].pos = len;
+	}
+}
diff --git a/ui.c b/ui.c
index 67c6349..64bd6ce 100644
--- a/ui.c
+++ b/ui.c
@@ -288,7 +288,8 @@ static const uint64_t Signatures[] = {
 	0x6C72696774616305, // no URLs
 	0x6C72696774616306, // no thresh
 	0x6C72696774616307, // no window time
-	0x6C72696774616308,
+	0x6C72696774616308, // no input
+	0x6C72696774616309,
 };
 
 static size_t signatureVersion(uint64_t signature) {
@@ -305,9 +306,10 @@ static int writeUint64(FILE *file, uint64_t u) {
 int uiSave(void) {
 	return 0
 		|| ftruncate(fileno(saveFile), 0)
-		|| writeUint64(saveFile, Signatures[7])
+		|| writeUint64(saveFile, Signatures[8])
 		|| writeUint64(saveFile, self.pos)
 		|| windowSave(saveFile)
+		|| inputSave(saveFile)
 		|| urlSave(saveFile)
 		|| fclose(saveFile);
 }
@@ -350,5 +352,6 @@ void uiLoad(const char *name) {
 		self.pos = readUint64(saveFile);
 	}
 	windowLoad(saveFile, version);
+	inputLoad(saveFile, version);
 	urlLoad(saveFile, version);
 }