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
|
/* $Id: textlist.c,v 1.5 2002/09/24 16:35:02 ukai Exp $ */
#include "textlist.h"
#include "indep.h"
#include "Str.h"
#include "gc.h"
/* General doubly linked list */
ListItem *
newListItem(void *s, ListItem *n, ListItem *p)
{
ListItem *it;
it = New(ListItem);
it->ptr = s;
it->next = n;
it->prev = p;
return it;
}
GeneralList *
newGeneralList()
{
GeneralList *tl = New(GeneralList);
tl->first = tl->last = NULL;
tl->nitem = 0;
return tl;
}
void
pushValue(GeneralList *tl, void *s)
{
ListItem *it;
if (s == NULL)
return;
it = newListItem(s, NULL, tl->last);
if (tl->first == NULL) {
tl->first = it;
tl->last = it;
tl->nitem = 1;
}
else {
tl->last->next = it;
tl->last = it;
tl->nitem++;
}
}
void *
popValue(GeneralList *tl)
{
ListItem *f;
if (tl == NULL || tl->first == NULL)
return NULL;
f = tl->first;
tl->first = f->next;
if (tl->first)
tl->first->prev = NULL;
else
tl->last = NULL;
tl->nitem--;
return f->ptr;
}
void *
rpopValue(GeneralList *tl)
{
ListItem *f;
if (tl == NULL || tl->last == NULL)
return NULL;
f = tl->last;
tl->last = f->prev;
if (tl->last)
tl->last->next = NULL;
else
tl->first = NULL;
tl->nitem--;
return f->ptr;
}
void
delValue(GeneralList *tl, ListItem *it)
{
if (it->prev)
it->prev->next = it->next;
else
tl->first = it->next;
if (it->next)
it->next->prev = it->prev;
else
tl->last = it->prev;
tl->nitem--;
}
GeneralList *
appendGeneralList(GeneralList *tl, GeneralList *tl2)
{
if (tl && tl2) {
if (tl2->first) {
if (tl->last) {
tl->last->next = tl2->first;
tl2->first->prev = tl->last;
tl->last = tl2->last;
tl->nitem += tl2->nitem;
}
else {
tl->first = tl2->first;
tl->last = tl2->last;
tl->nitem = tl2->nitem;
}
}
tl2->first = tl2->last = NULL;
tl2->nitem = 0;
}
return tl;
}
/* Line text list */
TextLine *
newTextLine(Str line, int pos)
{
TextLine *lbuf = New(TextLine);
if (line)
lbuf->line = line;
else
lbuf->line = Strnew();
lbuf->pos = pos;
return lbuf;
}
void
appendTextLine(TextLineList *tl, Str line, int pos)
{
TextLine *lbuf;
if (tl->last == NULL) {
pushTextLine(tl, newTextLine(Strdup(line), pos));
}
else {
lbuf = tl->last->ptr;
if (lbuf->line)
Strcat(lbuf->line, line);
else
lbuf->line = line;
lbuf->pos += pos;
}
}
|