Save and load the URL ring in the save file

weechat-hashes
C. McEnroe 2021-01-11 18:05:22 -05:00
parent 763802cae6
commit 51c92f94ff
3 changed files with 72 additions and 14 deletions

2
chat.h
View File

@ -356,6 +356,8 @@ void urlScan(uint id, const char *nick, const char *mesg);
void urlOpenCount(uint id, uint count); void urlOpenCount(uint id, uint count);
void urlOpenMatch(uint id, const char *str); void urlOpenMatch(uint id, const char *str);
void urlCopyMatch(uint id, const char *str); void urlCopyMatch(uint id, const char *str);
int urlSave(FILE *file);
void urlLoad(FILE *file, size_t version);
enum { IgnoreCap = 64 }; enum { IgnoreCap = 64 };
extern struct Ignore { extern struct Ignore {

40
ui.c
View File

@ -946,7 +946,8 @@ static const time_t Signatures[] = {
0x6C72696774616302, // no self.pos 0x6C72696774616302, // no self.pos
0x6C72696774616303, // no buffer line heat 0x6C72696774616303, // no buffer line heat
0x6C72696774616304, // no mute 0x6C72696774616304, // no mute
0x6C72696774616305, 0x6C72696774616305, // no URLs
0x6C72696774616306,
}; };
static size_t signatureVersion(time_t signature) { static size_t signatureVersion(time_t signature) {
@ -967,25 +968,35 @@ int uiSave(const char *name) {
FILE *file = dataOpen(name, "w"); FILE *file = dataOpen(name, "w");
if (!file) return -1; if (!file) return -1;
if (writeTime(file, Signatures[4])) return -1; int error = 0
if (writeTime(file, self.pos)) return -1; || writeTime(file, Signatures[5])
|| writeTime(file, self.pos);
if (error) return error;
for (uint num = 0; num < windows.len; ++num) { for (uint num = 0; num < windows.len; ++num) {
const struct Window *window = windows.ptrs[num]; const struct Window *window = windows.ptrs[num];
if (writeString(file, idNames[window->id])) return -1; error = 0
if (writeTime(file, window->mute)) return -1; || writeString(file, idNames[window->id])
if (writeTime(file, window->heat)) return -1; || writeTime(file, window->mute)
if (writeTime(file, window->unreadSoft)) return -1; || writeTime(file, window->heat)
if (writeTime(file, window->unreadWarm)) return -1; || writeTime(file, window->unreadSoft)
|| writeTime(file, window->unreadWarm);
if (error) return error;
for (size_t i = 0; i < BufferCap; ++i) { for (size_t i = 0; i < BufferCap; ++i) {
const struct Line *line = bufferSoft(window->buffer, i); const struct Line *line = bufferSoft(window->buffer, i);
if (!line) continue; if (!line) continue;
if (writeTime(file, line->time)) return -1; error = 0
if (writeTime(file, line->heat)) return -1; || writeTime(file, line->time)
if (writeString(file, line->str)) return -1; || writeTime(file, line->heat)
|| writeString(file, line->str);
if (error) return error;
} }
if (writeTime(file, 0)) return -1; error = writeTime(file, 0);
if (error) return error;
} }
return fclose(file); return 0
|| writeString(file, "")
|| urlSave(file)
|| fclose(file);
} }
static time_t readTime(FILE *file) { static time_t readTime(FILE *file) {
@ -1026,7 +1037,7 @@ void uiLoad(const char *name) {
char *buf = NULL; char *buf = NULL;
size_t cap = 0; size_t cap = 0;
while (0 < readString(file, &buf, &cap)) { while (0 < readString(file, &buf, &cap) && buf[0]) {
struct Window *window = windows.ptrs[windowFor(idFor(buf))]; struct Window *window = windows.ptrs[windowFor(idFor(buf))];
if (version > 3) window->mute = readTime(file); if (version > 3) window->mute = readTime(file);
if (version > 0) { if (version > 0) {
@ -1045,6 +1056,7 @@ void uiLoad(const char *name) {
window->buffer, COLS, window->ignore, window->unreadSoft window->buffer, COLS, window->ignore, window->unreadSoft
); );
} }
urlLoad(file, version);
free(buf); free(buf);
fclose(file); fclose(file);

44
url.c
View File

@ -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);
}