From cab57be05c395b18fb726ec2ec64880445caa54a Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 24 Nov 2018 01:10:07 -0500 Subject: Add premature serialization to edi --- bin/edi/Makefile | 1 + bin/edi/edi.h | 12 +++++++- bin/edi/store.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 bin/edi/store.c (limited to 'bin') diff --git a/bin/edi/Makefile b/bin/edi/Makefile index 2c98d9f2..e7f16b39 100644 --- a/bin/edi/Makefile +++ b/bin/edi/Makefile @@ -6,6 +6,7 @@ OBJS += edi.o OBJS += file.o OBJS += iter.o OBJS += log.o +OBJS += store.o OBJS += table.o TESTS += buffer.t diff --git a/bin/edi/edi.h b/bin/edi/edi.h index be7bf681..f335d1bd 100644 --- a/bin/edi/edi.h +++ b/bin/edi/edi.h @@ -14,6 +14,8 @@ * along with this program. If not, see . */ +#include +#include #include #include @@ -75,8 +77,8 @@ struct Log { size_t cap, len; size_t state; struct State { - struct Table table; size_t prev, next; + struct Table table; } *states; }; struct Log logAlloc(size_t cap); @@ -87,6 +89,14 @@ static inline struct Table *logTable(const struct Log *log) { return &log->states[log->state].table; } +struct Edit { + struct Buffer buf; + struct Log log; +}; + +bool storeWrite(FILE *stream, const struct Edit *edit); +bool storeRead(FILE *stream, struct Edit *edit); + struct File { char *path; struct Buffer buf; diff --git a/bin/edi/store.c b/bin/edi/store.c new file mode 100644 index 00000000..f116600a --- /dev/null +++ b/bin/edi/store.c @@ -0,0 +1,92 @@ +/* Copyright (C) 2018 Curtis McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "edi.h" + +static const char Magic[3] = "EDI"; +static const char Version = 1; + +static bool writeBuffer(FILE *stream, const struct Buffer *buf) { + size_t len = 0; + const struct Block *block; + for (block = buf->block; block; block = block->prev) { + len += block->len; + } + if (!fwrite(&len, sizeof(len), 1, stream)) return false; + for (block = buf->block; block; block = block->prev) { + fwrite(block->chars, sizeof(wchar_t), block->len, stream); + if (ferror(stream)) return false; + } + return true; +} + +static bool writeSlice( + FILE *stream, const struct Buffer *buf, struct Slice slice +) { + size_t offset = 0; + const struct Block *block; + for (block = buf->block; block; offset += block->len, block = block->prev) { + if ( + slice.ptr >= block->chars && slice.ptr < &block->chars[block->len] + ) break; + } + assert(block); + size_t ptr = offset + (size_t)(slice.ptr - block->chars); + return fwrite(&ptr, sizeof(ptr), 1, stream) + && fwrite(&slice.len, sizeof(slice.len), 1, stream); +} + +static bool writeTable( + FILE *stream, const struct Buffer *buf, const struct Table *table +) { + if (!fwrite(&table->len, sizeof(table->len), 1, stream)) return false; + for (size_t i = 0; i < table->len; ++i) { + if (!writeSlice(stream, buf, table->slices[i])) return false; + } + return true; +} + +static bool writeState( + FILE *stream, const struct Buffer *buf, const struct State *state +) { + return fwrite(&state->prev, sizeof(state->prev), 1, stream) + && fwrite(&state->next, sizeof(state->next), 1, stream) + && writeTable(stream, buf, &state->table); +} + +static bool writeLog( + FILE *stream, const struct Buffer *buf, const struct Log *log +) { + if (!fwrite(&log->state, sizeof(log->state), 1, stream)) return false; + if (!fwrite(&log->len, sizeof(log->len), 1, stream)) return false; + for (size_t i = 0; i < log->len; ++i) { + if (!writeState(stream, buf, &log->states[i])) return false; + } + return true; +} + +bool storeWrite(FILE *stream, const struct Edit *edit) { + return fwrite(Magic, sizeof(Magic), 1, stream) + && fwrite(&Version, sizeof(Version), 1, stream) + && writeBuffer(stream, &edit->buf) + && writeLog(stream, &edit->buf, &edit->log); +} -- cgit 1.4.1 /bibsort.pl?id=0997e74e2b6bf8d6a3b9e31357e409132a8fef23&follow=1'>Fix bibsort name sorting for middle names, trailing titlesJune McEnroe 2020-12-15Add bibsortJune McEnroe 2020-12-08Add modem -r flag to set baud rateJune McEnroe 2020-12-07Clean up variable expansions in c.shJune McEnroe 2020-12-06Add I Feel It AllJune McEnroe 2020-12-05Handle const strings in c -eJune McEnroe 2020-12-05Disable mouse in htopJune McEnroe 2020-12-03Note lack of macro expansions in Q(...)June McEnroe 2020-12-02Fix missing "to" in "hot tips"June McEnroe 2020-12-02Publish "hot tips"June McEnroe 2020-11-26Publish "Inability"June McEnroe 2020-11-26Render content into atom feedJune McEnroe 2020-11-26Update plan with different things I won't doJune McEnroe 2020-11-19Add "Come On Petunia"June McEnroe 2020-11-13Add x4 to LESSJune McEnroe 2020-11-04Remove modified sensitivity settingsJune McEnroe 2020-10-29Remove editJune McEnroe 2020-10-27Switch gr alias back to git rebaseJune McEnroe 2020-10-27Allow cd host: to cd to same path over sshJune McEnroe 2020-10-27Use SendEnv for cd host:pathJune McEnroe 2020-10-27Allow cd host:path over sshJune McEnroe 2020-10-07Use mandoc -T utf8 for text.June McEnroe 2020-09-20Add The Awakened KingdomJune McEnroe 2020-09-12Move /opt/local back, cheat port select to use system manJune McEnroe 2020-09-12Move /opt/local behind /usr againJune McEnroe 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe 2020-09-07Add SunglassesJune McEnroe 2020-09-06Add Between the BreathsJune McEnroe 2020-09-04Open /dev/tty in nudgeJune McEnroe 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe 2020-08-29Add tweets from retweetsJune McEnroe