summary refs log tree commit diff
path: root/url.c
diff options
context:
space:
mode:
authorC. McEnroe2021-01-11 18:05:22 -0500
committerC. McEnroe2021-01-11 18:05:22 -0500
commit51c92f94ff2728a6db539a82fb4c501f45118062 (patch)
treea4267a63286a986c071cae823f678e332e76e6da /url.c
parent763802cae630e628255a572806de03fa397f4dae (diff)
Save and load the URL ring in the save file 1.4
Diffstat (limited to 'url.c')
-rw-r--r--url.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/url.c b/url.c
index 53fe271..21f946c 100644
--- a/url.c
+++ b/url.c
@@ -230,3 +230,47 @@ void urlCopyMatch(uint id, const char *str) {
 		}
 	}
 }
+
+static int writeString(FILE *file, const char *str) {
+	return (fwrite(str, strlen(str) + 1, 1, file) ? 0 : -1);
+}
+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;
+}
+
+int urlSave(FILE *file) {
+	for (size_t i = 0; i < Cap; ++i) {
+		const struct URL *url = &ring.urls[(ring.len + i) % Cap];
+		if (!url->url) continue;
+		int error = 0
+			|| writeString(file, idNames[url->id])
+			|| writeString(file, (url->nick ?: ""))
+			|| writeString(file, url->url);
+		if (error) return error;
+	}
+	return writeString(file, "");
+}
+
+void urlLoad(FILE *file, size_t version) {
+	if (version < 5) return;
+	size_t cap = 0;
+	char *buf = NULL;
+	while (0 < readString(file, &buf, &cap) && buf[0]) {
+		struct URL *url = &ring.urls[ring.len++ % Cap];
+		free(url->nick);
+		free(url->url);
+		url->id = idFor(buf);
+		url->nick = NULL;
+		readString(file, &buf, &cap);
+		if (buf[0]) {
+			url->nick = strdup(buf);
+			if (!url->nick) err(EX_OSERR, "strdup");
+		}
+		readString(file, &buf, &cap);
+		url->url = strdup(buf);
+		if (!url->url) err(EX_OSERR, "strdup");
+	}
+	free(buf);
+}