File: line_list.cc

package info (click to toggle)
aegis 4.22-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,952 kB
  • ctags: 11,163
  • sloc: cpp: 170,390; sh: 66,404; makefile: 25,776; yacc: 4,497; perl: 1,106; ansic: 492; awk: 366
file content (413 lines) | stat: -rw-r--r-- 11,038 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//
//	aegis - project change supervisor
//	Copyright (C) 2002-2005 Peter Miller;
//	All rights reserved.
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 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 General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
//
// MANIFEST: functions to manipulate line_lists
//

#include <common/ac/string.h>

#include <libaegis/change.h>
#include <common/error.h> // for assert
#include <aeannotate/line_list.h>
#include <common/mem.h>
#include <common/trace.h>


#ifdef DEBUG
#define CHECK(llp) \
	assert(llp->start1 <= llp->start2); \
	assert(llp->start1 + llp->length1 <= llp->start2); \
	assert((llp->length1 != 0) || (llp->start1 == 0)); \
	assert(llp->start2 <= llp->maximum); \
	assert(llp->start2 + llp->length2 <= llp->maximum); \
	assert((llp->length2 != 0) || (llp->start2 == llp->maximum)); \
	{ \
	size_t mj, mk; \
	for (mj = 0; mj < llp->length1; ++mj) \
	assert(llp->item[llp->start1 + mj].cp); \
	for (mk = 0; mk < llp->length2; ++mk) \
	assert(llp->item[llp->start2 + mk].cp); \
	}
#else
#define CHECK(llp)
#endif


void
line_list_constructor(line_list_t *llp)
{
    trace(("line_list_constructor(llp = %08lX)\n{\n", (long)llp));
    llp->maximum = 0;
    llp->start1 = 0;
    llp->length1 = 0;
    llp->start2 = 0;
    llp->length2 = 0;
    llp->item = 0;
    CHECK(llp);
    trace(("}\n"));
}


void
line_list_destructor(line_list_t *llp)
{
    trace(("line_list_destructor(llp = %08lX)\n{\n", (long)llp));
    line_list_clear(llp);
    if (llp->item)
	mem_free(llp->item);
    llp->maximum = 0;
    llp->start1 = 0;
    llp->length1 = 0;
    llp->start2 = 0;
    llp->length2 = 0;
    llp->item = 0;
    trace(("}\n"));
}


void
line_list_clear(line_list_t *llp)
{
    size_t	    j;

    trace(("line_list_clear(llp = %08lX)\n{\n", (long)llp));
    CHECK(llp);
    for (j = 0; j < llp->length1; ++j)
	line_destructor(llp->item + llp->start1 + j);
    llp->start1 = 0;
    llp->length1 = 0;
    for (j = 0; j < llp->length2; ++j)
	line_destructor(llp->item + llp->start2 + j);
    llp->start2 = llp->maximum;
    llp->length2 = 0;
    CHECK(llp);
    trace(("}\n"));
}


void
line_list_delete(line_list_t *llp, size_t first_line, size_t num_lines)
{
    trace(("line_list_delete(llp = %08lX, first_line = %ld, "
	"num_lines = %ld)\n{\n", (long)llp, (long)first_line, (long)num_lines));
    CHECK(llp);
    if (num_lines == 0)
	first_line = 0;
    assert(first_line < llp->length1 + llp->length2);
    assert(first_line + num_lines <= llp->length1 + llp->length2);

    while (num_lines)
    {
	size_t		second_line;

	trace(("llp->start1 = %ld\n", (long)llp->start1));
	trace(("llp->length1 = %ld\n", (long)llp->length1));
	if (first_line < llp->length1)
	{
	    size_t	    partial_num_lines;
	    size_t	    remainder_num_lines;
	    size_t	    j;

	    //
	    // Destroy those lines which fall into the first range.
	    //
	    trace(("destroy mid range1\n"));
	    partial_num_lines = num_lines;
	    if (first_line + num_lines > llp->length1)
		partial_num_lines = llp->length1 - first_line;
	    trace(("partial_num_lines = %ld\n", (long)partial_num_lines));
	    for (j = 0; j < partial_num_lines; ++j)
		line_destructor(llp->item + llp->start1 + first_line + j);

	    //
	    // Move the end of the first range to the beginning of the
	    // second range.
	    //
	    remainder_num_lines = llp->length1 - first_line - partial_num_lines;
	    trace(("remainder_num_lines = %ld\n", (long)remainder_num_lines));
	    memmove
	    (
	    	llp->item + llp->start2 - remainder_num_lines,
	    	llp->item + llp->start1 + llp->length1 - remainder_num_lines,
		remainder_num_lines * sizeof(llp->item[0])
	    );
	    llp->start2 -= remainder_num_lines;
	    llp->length2 += remainder_num_lines;

	    //
	    // Adjust the length of the first range.
	    //
	    if (first_line)
		llp->length1 = first_line;
	    else
	    {
		llp->start1 = 0;
		llp->length1 = 0;
	    }

	    //
	    // Adjust the number of lines are are deleting.
	    //
	    // Note that "first_line" does not move, because we have
	    // deleted lines from the start of the range, but not moved
	    // the front of the delete range.
	    //
	    num_lines -= partial_num_lines;
	    trace(("num_lines = %ld\n", (long)num_lines));
	    continue;
	}

	trace(("llp->start2 = %ld\n", (long)llp->start2));
	trace(("llp->length2 = %ld\n", (long)llp->length2));
	second_line = first_line - llp->length1;
	trace(("second_line = %ld\n", (long)second_line));
	if (second_line < llp->length2)
	{
	    size_t	    partial_num_lines;
	    size_t	    j;

	    //
	    // Move the beginning of the second range to the end of the
	    // first range.
	    //
	    memmove
	    (
	    	llp->item + llp->start1 + llp->length1,
	    	llp->item + llp->start2,
		second_line * sizeof(llp->item[0])
	    );
	    llp->length1 += second_line;
	    llp->start2 += second_line;
	    llp->length2 -= second_line;
	    second_line = 0;

	    //
	    // Destroy those lines which fall into the second range.
	    //
	    trace(("destroy mid range2\n"));
	    partial_num_lines = num_lines;
	    if (partial_num_lines > llp->length2)
		partial_num_lines = llp->length2;
	    for (j = 0; j < partial_num_lines; ++j)
		line_destructor(llp->item + llp->start2 + j);

	    //
	    // Adjust the length of the second range.
	    //
	    llp->start2 += partial_num_lines;
	    llp->length2 -= partial_num_lines;
	    if (llp->length2 == 0)
	    	llp->start2 = llp->maximum;

	    //
	    // Adjust the number of lines are are deleting.
	    //
	    num_lines -= partial_num_lines;
	    trace(("num_lines = %ld\n", (long)num_lines));
	    first_line += partial_num_lines;
	    trace(("first_line = %ld\n", (long)first_line));
	    continue;
	}

	//
	// Oops.  This isn't means to happen.  It should also have been
	// caught by the assert at the beginning of this function.
	//
	assert(0);
	break;
    }

    trace(("Checking...\n"));
    trace(("llp->start1 = %ld\n", (long)llp->start1));
    trace(("llp->length1 = %ld\n", (long)llp->length1));
    trace(("llp->start2 = %ld\n", (long)llp->start2));
    trace(("llp->length2 = %ld\n", (long)llp->length2));
    trace(("llp->maximum = %ld\n", (long)llp->maximum));

    CHECK(llp);
    trace(("}\n"));
}


void
line_list_insert(line_list_t *llp,
                 size_t first_line,
                 change_ty *cp,
                 string_ty *text)
{
    trace(("line_list_insert(llp = %08lX, first_line = %ld, cp = %08lX, "
	"text = %08lX)\n{\n", (long)llp, (long)first_line, (long)cp,
	(long)text));
    CHECK(llp);
    assert(first_line <= llp->length1 + llp->length2);
    assert(cp);
    assert(text);

    for (;;)
    {
	size_t		second_line;

	trace(("llp->start1 = %ld\n", (long)llp->start1));
	trace(("llp->length1 = %ld\n", (long)llp->length1));
	trace(("llp->start2 = %ld\n", (long)llp->start2));
	trace(("llp->length2 = %ld\n", (long)llp->length2));
	if (first_line <= llp->length1)
	{
	    size_t	    remainder_num_lines;

	    //
	    // Move the first range down to the beginning of the buffer.
	    //
	    if (llp->start1)
	    {
		memmove
		(
		    llp->item,
		    llp->item + llp->start1,
		    llp->length1 * sizeof(llp->item[0])
		);
		llp->start1 = 0;
	    }

	    //
	    // We need to grow if we have run out of room in the first
	    // range (in which case the second range will be empty).
	    //
	    assert(llp->start1 == 0);
	    if (llp->length1 == llp->start2)
	    {
		size_t		nbytes;

		trace(("growing...\n"));
		assert(llp->length2 == 0);
		assert(llp->start2 == llp->maximum);
		llp->maximum = llp->maximum * 2 + 4;
		nbytes = llp->maximum * sizeof(llp->item[0]);
		llp->item = (line_t *)mem_change_size(llp->item, nbytes);

		llp->start2 = llp->maximum;
		trace(("llp->start2 = %ld\n", (long)llp->start2));
	    }

	    //
	    // Move the end of the first range to the beginning of the
	    // second range.
	    //
	    remainder_num_lines = llp->length1 - first_line;
	    trace(("remainder_num_lines = %ld\n", (long)remainder_num_lines));
	    if (remainder_num_lines)
	    {
		memmove
		(
		    llp->item + llp->start2 - remainder_num_lines,
		    llp->item + llp->start1 + first_line,
		    remainder_num_lines * sizeof(llp->item[0])
		);
		llp->length1 = first_line;
		llp->start2 -= remainder_num_lines;
		llp->length2 += remainder_num_lines;
		trace(("llp->start1 = %ld\n", (long)llp->start1));
		trace(("llp->length1 = %ld\n", (long)llp->length1));
		trace(("llp->start2 = %ld\n", (long)llp->start2));
		trace(("llp->length2 = %ld\n", (long)llp->length2));
	    }

	    //
	    // Add the line to the end of the first range.
	    //
	    assert(llp->start1 == 0);
	    line_constructor
	    (
		llp->item // + llp->start1
 + first_line,
		cp,
		text
	    );

	    //
	    // Extend the first range to cover it.
	    //
	    // If it grows into the second range, rearrange things so
	    // that the first range contains both, and the second range
	    // is empty.
	    //
	    llp->length1++;
	    assert(llp->start1 == 0);
	    if (// llp->start1 +
 llp->length1 == llp->start2)
	    {
		llp->length1 += llp->length2;
		llp->start2 = llp->maximum;
		llp->length2 = 0;
	    }

	    //
	    // All done.
	    //
	    trace(("llp->start1 = %ld\n", (long)llp->start1));
	    trace(("llp->length1 = %ld\n", (long)llp->length1));
	    trace(("llp->start2 = %ld\n", (long)llp->start2));
	    trace(("llp->length2 = %ld\n", (long)llp->length2));
	    break;
	}

	trace(("llp->start2 = %ld\n", (long)llp->start2));
	trace(("llp->length2 = %ld\n", (long)llp->length2));
	second_line = first_line - llp->length1;
	trace(("second_line = %ld\n", (long)second_line));
	if (second_line <= llp->length2)
	{
	    //
	    // Move the beginning of the second range to the end of the
	    // first range.
	    //
	    memmove
	    (
	    	llp->item + llp->start1 + llp->length1,
	    	llp->item + llp->start2,
		second_line * sizeof(llp->item[0])
	    );
	    llp->length1 += second_line;
	    llp->start2 += second_line;
	    llp->length2 -= second_line;
	    second_line = 0;

	    //
	    // If the second range is now empty, move it to the end.
	    //
	    if (llp->length2 == 0)
		llp->start2 = llp->maximum;

	    //
	    // Go around again, now that we are the right shape.
	    //
	    continue;
	}

	//
	// Oops.  This isn't means to happen.  It should also have been
	// caught by the assert at the beginning of this function.
	//
	assert(0);
	break;
    }

    CHECK(llp);
    trace(("}\n"));
}