[go: up one dir, main page]

File: str_list.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (683 lines) | stat: -rw-r--r-- 12,908 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
 *	cook - file construction tool
 *	Copyright (C) 1990, 1991, 1992, 1993, 1994, 1997, 1998 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 lists of strings
 *
 * This file contains routines for mainpulating words and word lists.
 * Much of the functionality of cook uses these routines.
 */

#include <ac/ctype.h>
#include <ac/stddef.h>
#include <ac/string.h>
#include <ac/stdlib.h>
#include <ac/time.h>

#include <mem.h>
#include <str.h>
#include <str_list.h>
#include <trace.h> /* for assert */


/*
 * NAME
 *	string_list_append - append to a word list
 *
 * SYNOPSIS
 *	void string_list_append(string_list_ty *wlp, string_ty *wp);
 *
 * DESCRIPTION
 *	Wl_append is used to append to a word list.
 *
 * CAVEAT
 *	The word being appended IS copied.
 */

void
string_list_append(wlp, w)
	string_list_ty	*wlp;
	string_ty	*w;
{
	assert(wlp);
	assert(w);
	if (wlp->nstrings >= wlp->nstrings_max)
	{
		size_t		nbytes;

		wlp->nstrings_max = wlp->nstrings_max * 2 + 16;
		nbytes = wlp->nstrings_max * sizeof(string_ty *);
		wlp->string = mem_change_size(wlp->string, nbytes);
	}
	wlp->string[wlp->nstrings++] = str_copy(w);
}


/*
 * NAME
 *	string_list_append_list
 *
 * SYNOPSIS
 *	void string_list_append_list(string_list_ty *to, string_list_ty *from);
 *
 * DESCRIPTION
 *	The string_list_append_list function is used to append one
 *	string list (from) onto the end of another (to).
 */

void
string_list_append_list(to, from)
	string_list_ty	*to;
	const string_list_ty *from;
{
	size_t		j;

	for (j = 0; j < from->nstrings; ++j)
		string_list_append(to, from->string[j]);
}


/*
 * NAME
 *	string_list_prepend
 *
 * SYNOPSIS
 *	void string_list_prepend(string_list_ty *, string_ty *);
 *
 * DESCRIPTION
 *	The string_list_prepend function is used to insert a string at
 *	the beginning of a string list.
 */

void
string_list_prepend(wlp, w)
	string_list_ty	*wlp;
	string_ty	*w;
{
	ptrdiff_t	j;

	assert(wlp);
	assert(w);
	if (wlp->nstrings >= wlp->nstrings_max)
	{
		size_t		nbytes;

		wlp->nstrings_max = wlp->nstrings_max * 2 + 16;
		nbytes = wlp->nstrings_max * sizeof(string_ty *);
		wlp->string = mem_change_size(wlp->string, nbytes);
	}
	wlp->nstrings++;
	for (j = wlp->nstrings - 1; j > 0; --j)
		wlp->string[j] = wlp->string[j - 1];
	wlp->string[0] = str_copy(w);
}


/*
 * NAME
 *	string_list_destructor - free a word list
 *
 * SYNOPSIS
 *	void string_list_destructor(string_list_ty *wlp);
 *
 * DESCRIPTION
 *	Wl_free is used to free the contents of a word list
 *	when it is finished with.
 *
 * CAVEAT
 *	It is assumed that the contents of the word list were all
 *	created using strdup() or similar, and grown using string_list_append().
 */

void
string_list_destructor(wlp)
	string_list_ty	*wlp;
{
	size_t		j;

	for (j = 0; j < wlp->nstrings; j++)
		str_free(wlp->string[j]);
	if (wlp->string)
		mem_free(wlp->string);
	wlp->nstrings = 0;
	wlp->nstrings_max = 0;
	wlp->string = 0;
}


/*
 * NAME
 *	string_list_member - word list membership
 *
 * SYNOPSIS
 *	int string_list_member(string_list_ty *wlp, string_ty *wp);
 *
 * DESCRIPTION
 *	Wl_member is used to determine if the given word is
 *	contained in the given word list.
 *
 * RETURNS
 *	A zero if the word is not in the list,
 *	and a non-zero if it is.
 */

int
string_list_member(wlp, w)
	const string_list_ty *wlp;
	string_ty	*w;
{
	size_t		j;

	for (j = 0; j < wlp->nstrings; j++)
		if (str_equal(wlp->string[j], w))
			return 1;
	return 0;
}


/*
 * NAME
 *	string_list_intersect - word list intersection test
 *
 * SYNOPSIS
 *	int string_list_intersect(string_list_ty *wlp, string_list_ty *wp);
 *
 * DESCRIPTION
 *	Wl_intersect is used to determine if the given word is
 *	contained in the given word list.
 *
 * RETURNS
 *	A zero if the word is not in the list,
 *	and a non-zero if it is.
 */

int
string_list_intersect(wl1, wl2)
	const string_list_ty *wl1;
	const string_list_ty *wl2;
{
	size_t		j;

	if (wl1->nstrings > wl2->nstrings)
	{
		for (j = 0; j < wl2->nstrings; j++)
			if (string_list_member(wl1, wl2->string[j]))
				return 1;
	}
	else
	{
		for (j = 0; j < wl1->nstrings; j++)
			if (string_list_member(wl2, wl1->string[j]))
				return 1;
	}
	return 0;
}


/*
 * NAME
 *	string_list_copy_constructor - copy a word list
 *
 * SYNOPSIS
 *	void string_list_copy_constructor(string_list_ty *to, string_list_ty *from);
 *
 * DESCRIPTION
 *	Wl_copy is used to copy word lists.
 *
 * RETURNS
 *	A copy of the 'to' word list is placed in 'from'.
 *
 * CAVEAT
 *	It is the responsibility of the caller to ensure that the
 *	new word list is freed when finished with, by a call to string_list_destructor().
 */

void
string_list_copy_constructor(to, from)
	string_list_ty	*to;
	const string_list_ty *from;
{
	size_t		j;

	string_list_constructor(to);
	for (j = 0; j < from->nstrings; j++)
		string_list_append(to, str_copy(from->string[j]));
}


/*
 * NAME
 *	wl2str - form a string from a word list
 *
 * SYNOPSIS
 *	string_ty *wl2str(string_list_ty *wlp, int start, int stop, char *sep);
 *
 * DESCRIPTION
 *	Wl2str is used to form a string from a word list.
 *
 * RETURNS
 *	A pointer to the newly formed string in dynamic memory.
 *
 * CAVEAT
 *	It is the responsibility of the caller to ensure that the
 *	new string is freed when finished with, by a call to str_free().
 */

string_ty *
wl2str(wl, start, stop, sep)
	const string_list_ty *wl;
	int		start;
	int		stop;
	char		*sep;
{
	int		j;
	static char	*tmp;
	static size_t	tmplen;
	size_t		length;
	size_t		seplen;
	char		*pos;
	string_ty	*s;

	if (!sep)
		sep = " ";
	seplen = strlen(sep);
	length = 0;
	for (j = start; j <= stop && j < wl->nstrings; j++)
	{
		s = wl->string[j];
		if (s->str_length)
		{
			if (length)
				length += seplen;
			length += s->str_length;
		}
	}

	if (tmplen < length)
	{
		tmplen = length;
		tmp = mem_change_size(tmp, tmplen);
	}

	pos = tmp;
	for (j = start; j <= stop && j < wl->nstrings; j++)
	{
		s = wl->string[j];
		if (s->str_length)
		{
			if (pos != tmp)
			{
				memcpy(pos, sep, seplen);
				pos += seplen;
			}
			memcpy(pos, s->str_text, s->str_length);
			pos += s->str_length;
		}
	}

	s = str_n_from_c(tmp, length);
	return s;
}


/*
 * NAME
 *	str2wl - string to word list
 *
 * SYNOPSIS
 *	void str2wl(string_list_ty *wlp, string_ty *s, char *sep, int ewhite);
 *
 * DESCRIPTION
 *	Str2wl is used to form a word list from a string.
 *	wlp	- where to put the word list
 *	s	- string to break
 *	sep	- separators, default to " " if 0 given
 *	ewhite	- supress extra white space around separators
 *
 * RETURNS
 *	The string is broken on spaces into words,
 *	using strndup() and string_list_append().
 *
 * CAVEAT
 *	Quoting is not understood.
 */

void
str2wl(slp, s, sep, ewhite)
	string_list_ty	*slp;
	string_ty	*s;
	char		*sep;
	int		ewhite;
{
	char		*cp;
	int		more;

	if (!sep)
	{
		sep = " \t\n\f\r";
		ewhite = 1;
	}
	string_list_constructor(slp);
	cp = s->str_text;
	more = 0;
	while (*cp || more)
	{
		string_ty	*w;
		char		*cp1;
		char		*cp2;

		if (ewhite)
			while (isspace(*cp))
				cp++;
		if (!*cp && !more)
			break;
		more = 0;
		cp1 = cp;
		while (*cp && !strchr(sep, *cp))
			cp++;
		if (*cp)
		{
			cp2 = cp + 1;
			more = 1;
		}
		else
			cp2 = cp;
		if (ewhite)
			while (cp > cp1 && isspace(cp[-1]))
				cp--;
		w = str_n_from_c(cp1, cp - cp1);
		string_list_append(slp, w);
		str_free(w);
		cp = cp2;
	}
}


/*
 * NAME
 *	wl_insert - a insert a word into a list
 *
 * SYNOPSIS
 *	void wl_insert(string_list_ty *wlp, string_ty *wp);
 *
 * DESCRIPTION
 *	Wl_insert is similar to string_list_append, however it does not
 *	append the word unless it is not already in the list.
 *
 * CAVEAT
 *	If the word is inserted it is copied.
 */

void
string_list_append_unique(wlp, wp)
	string_list_ty	*wlp;
	string_ty	*wp;
{
	int		j;

	for (j = 0; j < wlp->nstrings; j++)
		if (str_equal(wlp->string[j], wp))
			return;
	string_list_append(wlp, wp);
}


/*
 * NAME
 *	string_list_append_list_unique
 *
 * SYNOPSIS
 *	void string_list_append_list_unique(string_list_ty *to, string_list_ty *from);
 *
 * DESCRIPTION
 *	The string_list_append_list_unique function is used to append
 *	the contents of one string list (from) to the end of another
 *	tring list (to).  Entries which duplicate items already present
 *	will be ignored.
 */

void
string_list_append_list_unique(to, from)
	string_list_ty	*to;
	const string_list_ty *from;
{
	size_t		j;

	for (j = 0; j < from->nstrings; ++j)
		string_list_append_unique(to, from->string[j]);
}


/*
 * NAME
 *	string_list_remove - remove list member
 *
 * SYNOPSIS
 *	void string_list_remove(string_list_ty *wlp, string_ty *wp);
 *
 * DESCRIPTION
 *	The string_list_remove function is used to delete a member of a word list.
 *
 * RETURNS
 *	void
 */

void
string_list_remove(wlp, wp)
	string_list_ty	*wlp;
	string_ty	*wp;
{
	size_t		j;
	size_t		k;

	for (j = 0; j < wlp->nstrings; ++j)
	{
		if (str_equal(wlp->string[j], wp))
		{
			wlp->nstrings--;
			for (k = j; k < wlp->nstrings; ++k)
				wlp->string[k] = wlp->string[k + 1];
			str_free(wp);
			break;
		}
	}
}


/*
 * NAME
 *	string_list_remove_list - remove list members
 *
 * SYNOPSIS
 *	void string_list_remove_list(string_list_ty *wlp, string_list_ty *wp);
 *
 * DESCRIPTION
 *	The string_list_remove_list function is used to delete all the
 *	members of a word list from another word list.
 *
 * RETURNS
 *	void
 */

void
string_list_remove_list(wlp, nuke)
	string_list_ty	*wlp;
	const string_list_ty *nuke;
{
	size_t		j;

	for (j = 0; j < nuke->nstrings; ++j)
		string_list_remove(wlp, nuke->string[j]);
}


/*
 * NAME
 *	string_list_constructor
 *
 * SYNOPSIS
 *	void string_list_constructor(string_list_ty *);
 *
 * DESCRIPTION
 *	The string_list_constructor function is used to prepare a string
 *	list for use.  It will be empty.
 *
 * CAVEAT
 *	This must be called on the string list before any other action
 *	is taken.  Use string_list_destructor when you are done.
 */

void
string_list_constructor(wlp)
	string_list_ty	*wlp;
{
	wlp->nstrings = 0;
	wlp->nstrings_max = 0;
	wlp->string = 0;
}


/*
 * NAME
 *	string_list_new
 *
 * DESCRIPTION
 *	string_list_ty *string_list_new(void);
 *
 * DESCRIPTION
 *	The string_list_new function is used to allocate a new  string
 *	list in dynamic memory.  It will be empty.
 *
 * RETURNS
 *	string_list_ty *
 *
 * CAVEAT
 *	Use string_list_delete when you are done.
 */

string_list_ty *
string_list_new()
{
	string_list_ty	*slp;

	slp = mem_alloc(sizeof(string_list_ty));
	string_list_constructor(slp);
	return slp;
}


/*
 * NAME
 *	string_list_new_copy
 *
 * SYNOPSIS
 *	string_list_ty *string_list_new_copy(string_list_ty *);
 *
 * DESCRIPTION
 *	The string_list_new_copy function is used to allocate a new copy
 *	of a string list in dynamic memory.
 *
 * RETURNS
 *	string_list_ty *
 *
 * CAVEAT
 *	Use string_list_delete when you are done.
 */

string_list_ty *
string_list_new_copy(from)
	const string_list_ty *from;
{
	string_list_ty	*slp;

	slp = mem_alloc(sizeof(string_list_ty));
	string_list_copy_constructor(slp, from);
	return slp;
	trace(("to silence warnings\n"));
}


/*
 * NAME
 *	string_list_delete
 *
 * SYNOPSIS
 *	void string_list_delete(string_list_ty *);
 *
 * DESCRIPTION
 *	The string_list_delete function is used to release the resources
 *	held by a string list in dynamic memory.
 */

void
string_list_delete(slp)
	string_list_ty	*slp;
{
	string_list_destructor(slp);
	mem_free(slp);
}


/*
 * NAME
 *	string_list_bool
 *
 * SYNOPSIS
 *	int string_list_bool(string_list_ty *);
 *
 * DESCRIPTION
 *	The string_list_bool function is used to perform a boolean
 *	evaluation on a list of strings.  If any return str_bool of
 *	true, the list is true.
 */

int
string_list_bool(slp)
	const string_list_ty *slp;
{
	size_t		j;

	for (j = 0; j < slp->nstrings; ++j)
		if (str_bool(slp->string[j]))
			return 1;
	return 0;
}


static int cmp _((const void *, const void *));

static int
cmp(va, vb)
	const void	*va;
	const void	*vb;
{
	string_ty	*a;
	string_ty	*b;

	a = *(string_ty **)va;
	b = *(string_ty **)vb;
	return strcmp(a->str_text, b->str_text);
}


void
string_list_sort(slp)
	string_list_ty	*slp;
{
	qsort(slp->string, slp->nstrings, sizeof(slp->string[0]), cmp);
}