str_list.c 11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* str_list.c */

/* Functions to deal with NULL-terminated string lists */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2009 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

rswindell's avatar
rswindell committed
38
#include <stdlib.h>		/* malloc and qsort */
39
#include <string.h>		/* strtok */
40
41
42
#if defined(_WIN32)
 #include <malloc.h>    /* alloca() on Win32 */
#endif
43
#include "genwrap.h"	/* stricmp */
44
45
#include "str_list.h"

46
str_list_t strListInit(void)
47
{
48
	str_list_t list;
49

50
	if((list=(str_list_t)malloc(sizeof(char*)))==NULL)
51
52
53
54
55
56
		return(NULL);

	list[0]=NULL;	/* terminated by default */
	return(list);
}

57
size_t strListCount(const str_list_t list)
58
59
60
{
	size_t i;

61
	COUNT_LIST_ITEMS(list,i);
62
63
64
65

	return(i);
}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
int strListIndexOf(const str_list_t list, const char* str)
{
	size_t		i;

	if(list==NULL)
		return -1;

	for(i=0; list[i]!=NULL; i++) {
		if(list[i]==str)
			return i;
	}
	
	return -1;
}

81
static char* str_list_append(str_list_t* list, char* str, size_t index)
82
{
83
	str_list_t lp;
84

85
	if((lp=(str_list_t)realloc(*list,sizeof(char*)*(index+2)))==NULL)
86
87
88
		return(NULL);

	*list=lp;
89
90
91
	lp[index++]=str;
	lp[index]=NULL;	/* terminate list */

92
	return(str);
93
94
}

95
static char* str_list_insert(str_list_t* list, char* str, size_t index)
96
97
98
99
100
101
{
	size_t	i;
	size_t	count;
	str_list_t lp;

	count = strListCount(*list);
102
103
104
105
	if(index > count)	/* invalid index, do nothing */
		return(NULL);

	count++;
106
107
108
109
	if((lp=(str_list_t)realloc(*list,sizeof(char*)*(count+1)))==NULL)
		return(NULL);

	*list=lp;
110
111
	for(i=count; i>index; i--)
		lp[i]=lp[i-1];
112
	lp[index]=str;
113

114
	return(str);
115
116
}

117
char* strListRemove(str_list_t* list, size_t index)
118
{
119
	char*	str;
120
121
122
123
124
	size_t	i;
	size_t	count;
	str_list_t lp;

	count = strListCount(*list);
125
126
127
128

	if(index==STR_LIST_LAST_INDEX && count)
		index = count-1;

129
130
131
132
133
134
135
136
	if(index >= count)	/* invalid index, do nothing */
		return(NULL);

	count--;
	if((lp=(str_list_t)realloc(*list,sizeof(char*)*(count+1)))==NULL)
		return(NULL);

	*list=lp;
137
	str=lp[index];
138
139
140
141
	for(i=index; i<count; i++)
		lp[i]=lp[i+1];
	lp[count]=NULL;

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	return(str);
}

BOOL strListDelete(str_list_t* list, size_t index)
{
	char*	str;

	if((str=strListRemove(list, index))==NULL)
		return(FALSE);

	free(str);

	return(TRUE);
}

char* strListReplace(const str_list_t list, size_t index, const char* str)
{
	char*	buf;
	size_t	count;

rswindell's avatar
rswindell committed
162
163
164
	if(str==NULL)
		return(NULL);

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	count = strListCount(list);

	if(index==STR_LIST_LAST_INDEX && count)
		index = count-1;

	if(index >= count)	/* invalid index, do nothing */
		return(NULL);

	if((buf=(char*)realloc(list[index],strlen(str)+1))==NULL)
		return(NULL);

	list[index]=buf;
	strcpy(buf,str);

	return(buf);
180
181
}

rswindell's avatar
rswindell committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
BOOL strListSwap(const str_list_t list, size_t index1, size_t index2)
{
	char*	tmp;
	size_t	count;

	count = strListCount(list);

	if(index1==STR_LIST_LAST_INDEX && count)
		index1 = count-1;

	if(index2==STR_LIST_LAST_INDEX && count)
		index2 = count-1;

	if(index1 >= count || index2 >= count || index1 == index2)	
		return(FALSE);	/* invalid index, do nothing */

	tmp=list[index1];
	list[index1]=list[index2];
	list[index2]=tmp;

	return(TRUE);
}

205
char* strListAppend(str_list_t* list, const char* str, size_t index)
206
207
208
{
	char* buf;

rswindell's avatar
rswindell committed
209
210
211
	if(str==NULL)
		return(NULL);

rswindell's avatar
rswindell committed
212
	if((buf=strdup(str))==NULL)
213
214
		return(NULL);

215
	if(index==STR_LIST_LAST_INDEX)
rswindell's avatar
rswindell committed
216
		index=strListCount(*list);
217

rswindell's avatar
rswindell committed
218
	return(str_list_append(list,buf,index));
219
220
}

221
size_t	strListAppendList(str_list_t* list, const str_list_t add_list)
222
{
223
224
	size_t	i;
	size_t	count;
225

226
227
	count=strListCount(*list);
	for(i=0; add_list[i]!=NULL; i++)
228
		strListAppend(list,add_list[i],count++);
229

230
	return(count);
231
232
}

233
char* strListInsert(str_list_t* list, const char* str, size_t index)
234
235
236
{
	char* buf;

rswindell's avatar
rswindell committed
237
238
239
	if(str==NULL)
		return(NULL);

rswindell's avatar
rswindell committed
240
	if((buf=strdup(str))==NULL)
241
242
243
244
245
		return(NULL);

	return(str_list_insert(list,buf,index));
}

246
size_t strListInsertList(str_list_t* list, const str_list_t add_list, size_t index)
247
248
249
{
	size_t	i;

250
	for(i=0; add_list[i]!=NULL; i++)
251
252
		if(strListInsert(list,add_list[i],index++)==NULL)
			break;
253

254
	return(i);
255
256
}

257
str_list_t strListSplit(str_list_t* lp, char* str, const char* delimit)
258
{
259
	size_t	count;
260
	char*	token;
261
	char*	tmp;
262
	str_list_t	list;
263

rswindell's avatar
rswindell committed
264
265
266
	if(str==NULL || delimit==NULL)
		return(NULL);

267
268
	if(lp==NULL) {
		if((list = strListInit())==NULL)
269
			return(NULL);
270
271
272
273
		lp=&list;
		count=0;
	} else
		count=strListCount(*lp);
274

275
	for(token = strtok_r(str, delimit, &tmp); token!=NULL; token=strtok_r(NULL, delimit, &tmp))
276
277
		if(strListAppend(lp, token, count++)==NULL)
			break;
278

279
	return(*lp);
280
281
282
283
}

str_list_t strListSplitCopy(str_list_t* list, const char* str, const char* delimit)
{
284
285
	char*		buf;
	str_list_t	new_list;
286

287
	if(str==NULL || delimit==NULL)
rswindell's avatar
rswindell committed
288
289
		return(NULL);

290
	if((buf=strdup(str))==NULL)
291
292
		return(NULL);

293
	new_list=strListSplit(list,buf,delimit);
294
295
296

	free(buf);

297
298
299
300
	if(list!=NULL)
		*list = new_list;

	return(new_list);
301
302
}

303
size_t strListMerge(str_list_t* list, str_list_t add_list)
304
{
305
306
	size_t	i;
	size_t	count;
307

308
	count=strListCount(*list);
309
	for(i=0; add_list[i]!=NULL; i++)
310
		str_list_append(list,add_list[i],count++);
311

312
	return(i);
313
314
}

315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
char* strListCombine(str_list_t list, char* buf, size_t maxlen, const char* delimit)
{
	size_t	i;
	char*	end;
	char*	ptr;

	if(list==NULL || maxlen<1)
		return(NULL);

	if(buf==NULL)
		if((buf=(char*)malloc(maxlen))==NULL)
			return(NULL);

	*buf=0;
	end=buf+maxlen;
	for(i=0, ptr=buf; list[i]!=NULL && buf<end; i++)
		ptr += safe_snprintf(ptr, end-ptr, "%s%s", i ? delimit:"", list[i]);

	return(buf);
}

336
337
338
339
340
341
342
#if defined(_WIN32)
	#define QSORT_CALLBACK_TYPE	_cdecl
#else
	#define QSORT_CALLBACK_TYPE
#endif

static int QSORT_CALLBACK_TYPE strListCompareAlpha(const void *arg1, const void *arg2)
rswindell's avatar
rswindell committed
343
344
345
346
{
   return stricmp(*(char**)arg1, *(char**)arg2);
}

347
static int QSORT_CALLBACK_TYPE strListCompareAlphaReverse(const void *arg1, const void *arg2)
rswindell's avatar
rswindell committed
348
349
350
351
{
   return stricmp(*(char**)arg2, *(char**)arg1);
}

352
static int QSORT_CALLBACK_TYPE strListCompareAlphaCase(const void *arg1, const void *arg2)
rswindell's avatar
rswindell committed
353
354
355
356
{
   return strcmp(*(char**)arg1, *(char**)arg2);
}

357
static int QSORT_CALLBACK_TYPE strListCompareAlphaCaseReverse(const void *arg1, const void *arg2)
rswindell's avatar
rswindell committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
{
   return strcmp(*(char**)arg2, *(char**)arg1);
}

void strListSortAlpha(str_list_t list)
{
	qsort(list,strListCount(list),sizeof(char*),strListCompareAlpha);
}

void strListSortAlphaReverse(str_list_t list)
{
	qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaReverse);
}

void strListSortAlphaCase(str_list_t list)
{
	qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCase);
}

void strListSortAlphaCaseReverse(str_list_t list)
{
	qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCaseReverse);
}

rswindell's avatar
rswindell committed
382
void strListFreeStrings(str_list_t list)
383
384
385
{
	size_t i;

386
	FREE_LIST_ITEMS(list,i);
rswindell's avatar
rswindell committed
387
}
388

rswindell's avatar
rswindell committed
389
390
391
392
void strListFree(str_list_t* list)
{
	if(*list!=NULL) {
		strListFreeStrings(*list);
393
		FREE_AND_NULL(*list);
394
395
	}
}
396

397
static str_list_t str_list_read_file(FILE* fp, str_list_t* lp, size_t max_line_len)
398
{
399
	char*		buf=NULL;
400
	size_t		count;
401
	str_list_t	list;
402
403
404
405

	if(max_line_len<1)
		max_line_len=2048;

406
407
	if(lp==NULL) {
		if((list = strListInit())==NULL)
408
			return(NULL);
409
		lp=&list;
410
411
	}

412
413
414
	if(fp!=NULL) {
		count=strListCount(*lp);
		while(!feof(fp)) {
415
			if(buf==NULL && (buf=(char*)malloc(max_line_len+1))==NULL)
416
417
418
419
420
421
				return(NULL);
			
			if(fgets(buf,max_line_len+1,fp)==NULL)
				break;
			strListAppend(lp, buf, count++);
		}
422
	}
423
424
	if(buf)
		free(buf);
425

426
	return(*lp);
427
428
}

429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
size_t strListInsertFile(FILE* fp, str_list_t* lp, size_t index, size_t max_line_len)
{
	str_list_t	list;
	size_t		count;

	if((list=str_list_read_file(fp, NULL, max_line_len)) == NULL)
		return(0);

	count = strListInsertList(lp, list, index);

	strListFree(&list);

	return(count);
}

str_list_t strListReadFile(FILE* fp, str_list_t* lp, size_t max_line_len)
{
	return str_list_read_file(fp,lp,max_line_len);
}

449
450
451
452
453
454
455
456
457
458
size_t strListWriteFile(FILE* fp, const str_list_t list, const char* separator)
{
	size_t		i;

	if(list==NULL)
		return(0);

	for(i=0; list[i]!=NULL; i++) {
		if(fputs(list[i],fp)==EOF)
			break;
rswindell's avatar
rswindell committed
459
		if(separator!=NULL && fputs(separator,fp)==EOF)
460
461
462
463
464
			break;
	}
	
	return(i);
}
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
size_t strListBlockLength(char* block)
{
	char*	p=block;
	size_t	str_len;
	size_t	block_len=0;

	if(block==NULL)
		return(0);

	/* calculate total block length */
	while((str_len=strlen(p))!=0) {
		block_len+=(str_len + 1);
		p+=(str_len + 1);
	}
	/* block must be double-NULL terminated */
	if(!block_len)
		block_len=1;
	block_len++;

	return(block_len);
}

char* strListCopyBlock(char* block)
{
	char*	p;
	size_t	block_len;
	
	if((block_len=strListBlockLength(block))==0)
		return(NULL);

	if((p=(char*)malloc(block_len))==NULL)
		return(NULL);
	memcpy(p, block, block_len);
	return(p);
}

char* strListAppendBlock(char* block, str_list_t list)
503
504
505
{
	char*	p;
	size_t	str_len;
506
	size_t	block_len;	
507
508
	size_t	i;

509
510
511
	if((block_len=strListBlockLength(block))!=0)
		block_len--;	/* Over-write existing NULL terminator */

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
	for(i=0; list[i]!=NULL; i++) {
		str_len=strlen(list[i]);
		if(str_len==0)
			continue;	/* can't include empty strings in block */
		if((p=(char*)realloc(block, block_len + str_len + 1))==NULL) {
			FREE_AND_NULL(block);
			return(block);
		}
		block=p;
		strcpy(block + block_len, list[i]);
		block_len += (str_len + 1);
	}

	/* block must be double-NULL terminated */
	if(!block_len)
		block_len=1;
	block_len++;
	if((p=(char*)realloc(block, block_len))==NULL) {
		FREE_AND_NULL(block);
		return(block);
	}
	block=p;
	memset(block + (block_len-2), 0, 2);

	return(block);
}

539
540
541
542
543
char* strListCreateBlock(str_list_t list)
{
	return(strListAppendBlock(NULL,list));
}

544
545
void strListFreeBlock(char* block)
{
546
547
	if(block!=NULL)
		free(block);	/* this must be done here for Windows-DLL reasons */
548
}