| 12
 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
 
 | /*
 *	cook - file construction tool
 *	Copyright (C) 1997, 2001 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 wide strings
 */
#include <ac/string.h>
#include <wstr_list.h>
#include <mem.h>
/*
 * NAME
 *	wstring_list_append - append to a wide string list
 *
 * SYNOPSIS
 *	void wstring_list_append(wstring_list_ty *wlp, wstring_ty *wp);
 *
 * DESCRIPTION
 *	The wstring_list_append function is used to append to a wide
 *	string list.
 *
 * CAVEAT
 *	The wide string being appended IS copied.
 */
void
wstring_list_append(wlp, w)
	wstring_list_ty	*wlp;
	wstring_ty	*w;
{
	size_t		nbytes;
	if (wlp->nitems >= wlp->nitems_max)
	{
		/*
		 * always 8 less than a power of 2, which is
		 * most efficient for many memory allocators
		 */
		wlp->nitems_max = wlp->nitems_max * 2 + 8;
		nbytes = wlp->nitems_max * sizeof(wstring_ty *);
		wlp->item = mem_change_size(wlp->item, nbytes);
	}
	wlp->item[wlp->nitems++] = wstr_copy(w);
}
void
wstring_list_prepend(wlp, w)
	wstring_list_ty	*wlp;
	wstring_ty	*w;
{
	size_t		nbytes;
	size_t		j;
	if (wlp->nitems >= wlp->nitems_max)
	{
		/*
		 * always 8 less than a power of 2, which is
		 * most efficient for many memory allocators
		 */
		wlp->nitems_max = wlp->nitems_max * 2 + 8;
		nbytes = wlp->nitems_max * sizeof(wstring_ty *);
		wlp->item = mem_change_size(wlp->item, nbytes);
	}
	for (j = wlp->nitems; j > 0; --j)
		wlp->item[j] = wlp->item[j - 1];
	wlp->nitems++;
	wlp->item[0] = wstr_copy(w);
}
/*
 * NAME
 *	wstring_list_free - free a wide string list
 *
 * SYNOPSIS
 *	void wstring_list_free(wstring_list_ty *wlp);
 *
 * DESCRIPTION
 *	The wstring_list_free function is used to free the contents of a
 *	wide string list when it is finished with.
 *
 * CAVEAT
 *	It is assumed that the contents of the wide string list were all
 *	created using strdup() or similar, and grown using
 *	wstring_list_append().
 */
void
wstring_list_free(wlp)
	wstring_list_ty		*wlp;
{
	size_t		j;
	for (j = 0; j < wlp->nitems; j++)
		wstr_free(wlp->item[j]);
	if (wlp->item)
		mem_free(wlp->item);
	wlp->nitems = 0;
	wlp->nitems_max = 0;
	wlp->item = 0;
}
/*
 * NAME
 *	wstring_list_member - wide string list membership
 *
 * SYNOPSIS
 *	int wstring_list_member(wstring_list_ty *wlp, wstring_ty *wp);
 *
 * DESCRIPTION
 *	The wstring_list_member function is used to determine if the
 *	given wide string is contained in the given wide string list.
 *
 * RETURNS
 *	A zero if the wide string is not in the list, and a non-zero if it
 *	is.
 */
int
wstring_list_member(wlp, w)
	wstring_list_ty		*wlp;
	wstring_ty	*w;
{
	size_t		j;
	for (j = 0; j < wlp->nitems; j++)
		if (wstr_equal(wlp->item[j], w))
			return 1;
	return 0;
}
/*
 * NAME
 *	wstring_list_copy - copy a wide string list
 *
 * SYNOPSIS
 *	void wstring_list_copy(wstring_list_ty *to, wstring_list_ty *from);
 *
 * DESCRIPTION
 *	The wstring_listl_copy function is used to copy wide string lists.
 *
 * RETURNS
 *	A copy of the 'to' wide string list is placed in 'from'.
 *
 * CAVEAT
 *	It is the responsibility of the caller to ensure that the new
 *	wide string list is freed when finished with, by a call to
 *	wstring_list_free().
 */
void
wstring_list_copy(to, from)
	wstring_list_ty		*to;
	wstring_list_ty		*from;
{
	size_t		j;
	wstring_list_zero(to);
	for (j = 0; j < from->nitems; j++)
		wstring_list_append(to, wstr_copy(from->item[j]));
}
/*
 * NAME
 *	wstring_list_to_wstring - form a string from a wide string list
 *
 * SYNOPSIS
 *	wstring_ty *wstring_list_to_wstring(wstring_list_ty *wlp, int start,
 *		int stop, char *sep);
 *
 * DESCRIPTION
 *	The wstring_list_to_wstring function is used to form a string
 *	from a wide string 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 free().
 */
wstring_ty *
wstring_list_to_wstring(wl, start, stop, sep)
	wstring_list_ty	*wl;
	int		start;
	int		stop;
	char		*sep;
{
	int		j, k;
	static wchar_t	*tmp;
	static size_t	tmplen;
	size_t		length;
	size_t		seplen;
	wchar_t		*pos;
	wstring_ty	*s;
	if (!sep)
		sep = " ";
	seplen = strlen(sep);
	length = 0;
	for (j = start; j <= stop && j < wl->nitems; ++j)
	{
		s = wl->item[j];
		if (s->wstr_length)
		{
			if (length)
				length += seplen;
			length += s->wstr_length;
		}
	}
	if (tmplen < length)
	{
		tmplen = length;
		tmp = mem_change_size(tmp, tmplen * sizeof(wchar_t));
	}
	pos = tmp;
	for (j = start; j <= stop && j < wl->nitems; j++)
	{
		s = wl->item[j];
		if (s->wstr_length)
		{
			if (pos != tmp)
			{
				for (k = 0; k < seplen; ++k)
					*pos++ = sep[k];
			}
			memcpy
			(
				pos,
				s->wstr_text,
				s->wstr_length * sizeof(wchar_t)
			);
			pos += s->wstr_length;
		}
	}
	s = wstr_n_from_wc(tmp, length);
	return s;
}
static int wc_find _((char *, wchar_t));
static int
wc_find(s, c)
	char		*s;
	wchar_t		c;
{
	while (*s)
	{
		if (*s == c)
			return 1;
		++s;
	}
	return 0;
}
/*
 * NAME
 *	wstring_to_wstring_list - string to wide string list
 *
 * SYNOPSIS
 *	void wstring_to_wstring_list(wstring_list_ty *wlp, wstring_ty *s,
 *		char *sep, int ewhite);
 *
 * DESCRIPTION
 *	The wstring_to_wstring_list function is used to form a wide string
 *	list from a string.
 *
 * ARGUMENTS
 *	wlp	- where to put the wide string 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 wstring_list_append().
 *
 * CAVEAT
 *	Quoting is not understood.
 */
void
wstring_to_wstring_list(slp, s, sep, ewhite)
	wstring_list_ty	*slp;
	wstring_ty	*s;
	char		*sep;
	int		ewhite;
{
	static char	white[] = " \t\n\f\r";
	wchar_t		*cp;
	int		more;
	if (!sep)
	{
		sep = white;
		ewhite = 1;
	}
	wstring_list_zero(slp);
	cp = s->wstr_text;
	more = 0;
	while (*cp || more)
	{
		wstring_ty	*w;
		wchar_t		*cp1;
		wchar_t		*cp2;
		if (ewhite)
			while (wc_find(white, *cp))
				cp++;
		if (!*cp && !more)
			break;
		more = 0;
		cp1 = cp;
		while (*cp && !wc_find(sep, *cp))
			cp++;
		if (*cp)
		{
			cp2 = cp + 1;
			more = 1;
		}
		else
			cp2 = cp;
		if (ewhite)
			while (cp > cp1 && wc_find(white, cp[-1]))
				cp--;
		w = wstr_n_from_wc(cp1, cp - cp1);
		wstring_list_append(slp, w);
		wstr_free(w);
		cp = cp2;
	}
}
/*
 * NAME
 *	wstring_list_insert - a insert a wide string into a list
 *
 * SYNOPSIS
 *	void wstring_list_insert(wstring_list_ty *wlp, wstring_ty *wp);
 *
 * DESCRIPTION
 *	The wstring_list_insert function is similar to the
 *	wstring_list_append function, however it does not append the
 *	wide string unless it is not already in the list.
 *
 * CAVEAT
 *	If the wide string is inserted it is copied.
 */
void
wstring_list_append_unique(wlp, wp)
	wstring_list_ty	*wlp;
	wstring_ty	*wp;
{
	size_t		j;
	for (j = 0; j < wlp->nitems; j++)
		if (wstr_equal(wlp->item[j], wp))
			return;
	wstring_list_append(wlp, wp);
}
/*
 * NAME
 *	wstring_list_delete - remove list member
 *
 * SYNOPSIS
 *	void wstring_list_delete(wstring_list_ty *wlp, wstring_ty *wp);
 *
 * DESCRIPTION
 *	The wstring_list_delete function is used to delete a member of a
 *	wide string list.
 *
 * RETURNS
 *	void
 */
void
wstring_list_delete(wlp, wp)
	wstring_list_ty	*wlp;
	wstring_ty	*wp;
{
	size_t		j;
	size_t		k;
	for (j = 0; j < wlp->nitems; ++j)
	{
		if (wstr_equal(wlp->item[j], wp))
		{
			wlp->nitems--;
			for (k = j; k < wlp->nitems; ++k)
				wlp->item[k] = wlp->item[k + 1];
			wstr_free(wp);
			break;
		}
	}
}
void
wstring_list_zero(wlp)
	wstring_list_ty	*wlp;
{
	wlp->nitems = 0;
	wlp->nitems_max = 0;
	wlp->item = 0;
}
int
wstring_list_equal(a, b)
	wstring_list_ty	*a;
	wstring_list_ty	*b;
{
	size_t		j, k;
	for (j = 0; j < a->nitems; ++j)
	{
		for (k = 0; k < b->nitems; ++k)
			if (wstr_equal(a->item[j], b->item[k]))
				break;
		if (k >= b->nitems)
			return 0;
	}
	for (j = 0; j < b->nitems; ++j)
	{
		for (k = 0; k < a->nitems; ++k)
			if (wstr_equal(b->item[j], a->item[k]))
				break;
		if (k >= a->nitems)
			return 0;
	}
	return 1;
}
int
wstring_list_subset(a,b)
	wstring_list_ty	*a;
	wstring_list_ty	*b;
{
	size_t		j, k;
	/*
	 * test if "a is a subset of b"
	 */
	if (a->nitems > b->nitems)
		return 0;
	for (j = 0; j < a->nitems; ++j)
	{
		for (k = 0; k < b->nitems; ++k)
			if (wstr_equal(a->item[j], b->item[k]))
				break;
		if (k >= b->nitems)
			return 0;
	}
	return 1;
}
 |