Save input buffer contents

master
June McEnroe 2022-02-20 16:57:11 -05:00
parent da1b943fcc
commit 78ff548b93
3 changed files with 46 additions and 2 deletions

2
chat.h
View File

@ -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,

39
input.c
View File

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

7
ui.c
View File

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