summary refs log tree commit diff
path: root/bin/edi/edi.h
blob: f35af64aa285faabb42857decd9e263a0d7b539e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* 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 <stdio.h>
#include <stdlib.h>
#include <wchar.h>

struct Span {
	size_t at, to;
};
static inline struct Span spanNext(struct Span span, size_t len) {
	return (struct Span) { span.to, span.to + len };
}
static inline struct Span spanPrev(struct Span span, size_t len) {
	return (struct Span) { span.at - len, span.at };
}

struct Slice {
	const wchar_t *ptr;
	size_t len;
};

struct Buffer {
	size_t cap;
	struct Slice slice;
	struct Block {
		struct Block *prev;
		size_t len;
		wchar_t chars[];
	} *block;
};
struct Buffer bufferAlloc(size_t cap);
void bufferFree(struct Buffer *buf);
void bufferSlice(struct Buffer *buf);
void bufferPush(struct Buffer *buf, wchar_t ch);
void bufferPop(struct Buffer *buf);
wchar_t *bufferDest(struct Buffer *buf, size_t len);
void bufferTruncate(struct Buffer *buf, size_t len);

static const struct Table {
	size_t cap, len;
	struct Slice *slices;
} TableEmpty;
struct Table tableAlloc(size_t cap);
void tableFree(struct Table *table);
void tablePush(struct Table *table, struct Slice slice);
void tableReplace(struct Table *table, struct Slice old, struct Slice new);
struct Table tableInsert(const struct Table *prev, size_t at, struct Slice ins);
struct Table tableDelete(const struct Table *prev, struct Span del);

struct Iter {
	const struct Table *table;
	struct Span span;
	size_t slice;
	size_t at;
	wint_t ch;
};
struct Iter iter(const struct Table *table, size_t at);
struct Iter iterNext(struct Iter it);
struct Iter iterPrev(struct Iter it);

struct Log {
	size_t cap, len;
	size_t state;
	struct State {
		size_t prev, next;
		struct Table table;
	} *states;
};
struct Log logAlloc(size_t cap);
void logFree(struct Log *log);
void logPush(struct Log *log, struct Table table);
static inline struct Table *logTable(const struct Log *log) {
	if (log->state == log->len) return NULL;
	return &log->states[log->state].table;
}

struct Edit {
	struct Buffer buf;
	struct Log log;
};

enum Error {
	Ok,
	StoreMagic,
	StoreVersion,
	StoreEOF,
	FileNoPath,
	Errno,
};

enum Error storeWrite(FILE *stream, const struct Edit *edit);
enum Error storeRead(FILE *stream, struct Edit *edit);

struct File {
	char *path;
	size_t clean;
	struct Edit edit;
};
struct File fileAlloc(char *path);
void fileFree(struct File *file);
enum Error fileRead(struct File *file);
enum Error fileWrite(struct File *file);