summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-11-24 01:10:07 -0500
committerJune McEnroe <june@causal.agency>2018-11-24 01:10:07 -0500
commit510285776ecbe1417582b717201221ed3289ac8d (patch)
treed30102b1645bdef975c861aa9ae783c0a9b9ce20
parentSet default .asm, .h, .sh syntax modes for vim (diff)
downloadsrc-510285776ecbe1417582b717201221ed3289ac8d.tar.gz
src-510285776ecbe1417582b717201221ed3289ac8d.zip
Add premature serialization to edi
-rw-r--r--bin/edi/Makefile1
-rw-r--r--bin/edi/edi.h12
-rw-r--r--bin/edi/store.c92
3 files changed, 104 insertions, 1 deletions
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 ed03bf21..751b250a 100644
--- a/bin/edi/edi.h
+++ b/bin/edi/edi.h
@@ -14,6 +14,8 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdbool.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <wchar.h>
 
@@ -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..f32ef297
--- /dev/null
+++ b/bin/edi/store.c
@@ -0,0 +1,92 @@
+/* Copyright (C) 2018  June McEnroe <june@causal.agency>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+
+#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);
+}