summaryrefslogtreecommitdiff
path: root/editor_piece.c
blob: 572da7e01180af78ccc16755e9b3e55addaf6a3c (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <sys/mman.h>

#include <stddef.h>
#include <assert.h>
#include "mem.h"
#include "slice.h"
#include "vector.h"

#define SEGMENT_LEN 8192

DEFINE_VECTOR(Vec_voidp, void *);

struct add_buf {
	Vec_voidp *segments;
	size_t size;
};

static void ab_init(struct add_buf **ab)
{
	struct add_buf *nab = mem_malloc(sizeof *nab);

	Vec_voidp_init(&nab->segments);
	nab->size = 0;
	*ab = nab;
}

static void ab_free(struct add_buf *ab)
{
	size_t len = Vec_voidp_len(ab->segments);

	for (size_t i = 0; i < len; i++)
		munmap(Vec_voidp_get(ab->segments, i), SEGMENT_LEN);

	Vec_voidp_free(ab->segments);
	mem_free(ab);
}

inline static char *new_segment()
{
	return mmap(NULL, SEGMENT_LEN, PROT_READ | PROT_WRITE,
		    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}

static void ab_push(struct add_buf *ab, char ch)
{
	if (++ab->size > Vec_voidp_len(ab->segments) * SEGMENT_LEN)
		Vec_voidp_push(ab->segments, new_segment());

	char *seg = (char *)Vec_voidp_get(ab->segments, ab->size / SEGMENT_LEN);
	seg[ab->size % SEGMENT_LEN] = ch;
}

struct piece {
	size_t is_origin:1;

	/* padding */

	size_t start_index;
	size_t length;
};

struct ed_buf {
	Vec_voidp *ptab;
	const char *origin;
	struct add_buf *add;
};

void eb_init(struct ed_buf **eb)
{
	Vec_voidp *ptab;
	Vec_voidp_init(&ptab);

	struct ed_buf *neb = mem_malloc(sizeof *neb);
	neb->ptab = ptab;
	neb->origin = NULL;
	ab_init(&neb->add);

	*eb = neb;
}

void eb_free(struct ed_buf *eb)
{
	Vec_voidp_free(eb->ptab);
	ab_free(eb->add);
	
	free(eb);
}

void eb_insert(struct ed_buf *eb, int ch)
{
	assert("TODO" == 0);
}

void eb_backspace(struct ed_buf *eb)
{
	assert("TODO" == 0);
}

void eb_newline(struct ed_buf *eb)
{
	assert("TODO" == 0);
}


/* set cursor */
void eb_set_cur_prev_line(struct ed_buf *eb)
{
	assert("TODO" == 0);
}

void eb_set_cur_backward(struct ed_buf *eb)
{
	assert("TODO" == 0);
}

void eb_set_cur_next_line(struct ed_buf *eb)
{
	assert("TODO" == 0);
}

void eb_set_cur_forward(struct ed_buf *eb)
{
	assert("TODO" == 0);
}


/* get */
void eb_get_line_slice(const struct ed_buf *eb, size_t pos, struct slice *sl)
{
	assert("TODO" == 0);
}

size_t eb_get_cur_col(const struct ed_buf *eb)
{
	assert("TODO" == 0);
}

size_t eb_get_cur_row(const struct ed_buf *eb)
{
	assert("TODO" == 0);
}

size_t eb_get_line_num(const struct ed_buf *eb)
{
	assert("TODO" == 0);
}


/* io */
void eb_bind(struct ed_buf *eb, const char *path)
{
	assert("TODO" == 0);
}

void eb_load_file(struct ed_buf *eb)
{
	assert("TODO" == 0);
}

void eb_save_file(struct ed_buf *eb)
{
	assert("TODO" == 0);
}