/
line.c
87 lines (73 loc) · 1.2 KB
/
line.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "edna.h"
int
changeline(Line *ln, String *s)
{
if (!ln->str) {
ln->str = str_alloc();
if (!ln->str) return ENOMEM;
}
vec_truncate(ln->str, 0);
return vec_join(ln->str, s);
}
size_t
getlineno(Line *targ)
{
size_t lineno;
lineno = 0;
while ((targ = getprev(targ))) ++lineno;
return lineno;
}
Line *
getnext(Line *li)
{
if (!li) return NULL;
return li->next;
}
Line *
getprev(Line *li)
{
if (!li) return NULL;
return li->prev;
}
void
freelines(Line *start, Line *stop)
{
Line *cur, *next, *prev, *tmp;
prev = getprev(start);
cur = start;
next = getnext(cur);
while (cur && cur != stop) {
tmp = getnext(next);
str_free(cur->str);
free(cur);
cur = next;
next = tmp;
}
linklines(prev, stop);
}
void
linklines(Line *left, Line *right)
{
if (left) left->next = right;
if (right) right->prev = left;
}
Line*
makeline(void)
{
return calloc(1, sizeof(Line));
}
Line *
walk(Line *cur, int offset)
{
Line *li = cur;
if (offset < 0) {
while ((li = getprev(li))) if (!++offset) return li;
return NULL;
} else if (offset > 0) {
while ((li = getnext(li))) if (!--offset) return li;
return NULL;
} else return cur;
}