-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.c
134 lines (114 loc) · 2.63 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
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
#include "line.h"
void initialize_line(line **l)
{
*l = (line*)malloc(sizeof(line));
(*l)->prev = (*l)->next = NULL;
}
void destroy_line(line *l)
{
if (l->s)
free(l->s);
free(l);
}
uint lines_lengths(line *l)
{
uint res = 0;
while (l)
{
++res;
l = l->next;
}
return res;
}
void set_prev_line(line *l, line *prev)
{
if (l == NULL)
return;
l->prev = prev;
}
void set_next_line(line *l, line *next)
{
if (l == NULL)
return;
l->next = next;
}
void merge_lines(line *l, line *r)
{
set_next_line(l, r);
set_prev_line(r, l);
}
line *advance(line *l, uint n)
{
uint i = 0;
for (i = 0; l->next && i < n; ++i)
l = l->next;
return l;
}
void delete_range(line *from, line *to)
{
line *temp;
merge_lines(from->prev, to->next);
while (from != to)
{
temp = from->next;
destroy_line(from);
from = temp;
}
destroy_line(to);
}
void insert_lines(line *after, line *source)
{
line *end_of_source, *temp_next;
if (!source || !after)
return;
end_of_source = source;
temp_next = after->next;
while (end_of_source->next)
end_of_source = end_of_source->next;
merge_lines(after, source);
merge_lines(end_of_source, temp_next);
}
line *wstring_to_lines(const wchar_t *wstring, uint *new_size, line **new_finish)
{
line *begin;
line *current;
uint current_capacity;
uint current_position;
uint wstring_position = 0;
initialize_line(&begin);
current_capacity = 2;
current_position = 0;
begin->s = (wchar_t*)malloc(sizeof(wchar_t) * current_capacity);
begin->s[0] = WEOS;
*new_finish = begin;
current = begin;
*new_size = 1;
while (wstring[wstring_position] != WEOS)
{
if (wstring[wstring_position] != WNEWLINE)
{
if (!(current_position + 1 < current_capacity))
{
current_capacity <<= 1;
wstring_resize(¤t->s, current_capacity);
}
current->s[current_position] = wstring[wstring_position];
current->s[++current_position] = WEOS;
}
else
{
line *new_line;
initialize_line(&new_line);
*new_finish = new_line;
current_capacity = 2;
current_position = 0;
new_line->s = (wchar_t*)malloc(sizeof(wchar_t) * current_capacity);
new_line->s[0] = WEOS;
merge_lines(current, new_line);
current = new_line;
++*new_size;
}
++wstring_position;
}
return begin;
}