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

/* 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)		*
 *																			*
rswindell's avatar
rswindell committed
11
 * Copyright 2005 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 38 39 40
 *																			*
 * 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.	*
 ****************************************************************************/

#ifndef _STR_LIST_H
#define _STR_LIST_H

41
#include <stdio.h>			/* FILE */
42 43 44 45 46 47
#include "gen_defs.h"

#if defined(__cplusplus)
extern "C" {
#endif

48
#define STR_LIST_LAST_INDEX	(~0)
49

50 51
typedef char** str_list_t;

52
/* Returns an allocated and terminated string list */
53
str_list_t	strListInit(void);
54 55

/* Frees the strings in the list (and the list itself) */
rswindell's avatar
rswindell committed
56
void		strListFree(str_list_t*);
57

rswindell's avatar
rswindell committed
58
/* Frees the strings in the list */
rswindell's avatar
rswindell committed
59
void		strListFreeStrings(str_list_t);
rswindell's avatar
rswindell committed
60

61
/* Pass a pointer to a string list, the string to add (append) */
62
/* Returns the updated list or NULL on error */
rswindell's avatar
rswindell committed
63
char*		strListAppend(str_list_t*, const char* str, size_t index);
64

65
/* Append a string list onto another string list */
rswindell's avatar
rswindell committed
66
size_t		strListAppendList(str_list_t*, const str_list_t append_list);
67 68

/* Inserts a string into the list at a specific index */
rswindell's avatar
rswindell committed
69
char*		strListInsert(str_list_t*, const char* str, size_t index);
70

71
/* Insert a string list into another string list */
rswindell's avatar
rswindell committed
72
size_t		strListInsertList(str_list_t*, const str_list_t append_list, size_t index);
73

74
/* Remove a string at a specific index */
rswindell's avatar
rswindell committed
75
char*		strListRemove(str_list_t*, size_t index);
76 77

/* Remove and free a string at a specific index */
rswindell's avatar
rswindell committed
78
BOOL		strListDelete(str_list_t*, size_t index);
79 80

/* Replace a string at a specific index */
rswindell's avatar
rswindell committed
81 82 83 84
char*		strListReplace(const str_list_t, size_t index, const char* str);

/* Swap the strings at index1 and index2 */
BOOL		strListSwap(const str_list_t, size_t index1, size_t index2);
85

86 87 88 89
/* Convenience macros for pushing, popping strings (LIFO stack) */
#define		strListPush(list, str)	strListAppend(list, str, STR_LIST_LAST_INDEX)
#define		strListPop(list)		strListRemove(list, STR_LIST_LAST_INDEX)

90 91
/* Add to an exiting or new string list by splitting specified string (str) */
/* into multiple strings, separated by one of the delimit characters */
rswindell's avatar
rswindell committed
92
str_list_t	strListSplit(str_list_t*, char* str, const char* delimit);
93 94

/* Same as above, but copies str to temporary heap buffer first */
rswindell's avatar
rswindell committed
95
str_list_t	strListSplitCopy(str_list_t*, const char* str, const char* delimit);
96

97
/* Merge 2 string lists (no copying of string data) */
rswindell's avatar
rswindell committed
98
size_t		strListMerge(str_list_t*, str_list_t append_list);
99

100
/* Count the number of strings in the list and returns the count */
rswindell's avatar
rswindell committed
101
size_t		strListCount(const str_list_t);
102

103
/* Sort the strings in the string list */
rswindell's avatar
rswindell committed
104 105
void		strListSortAlpha(str_list_t);
void		strListSortAlphaReverse(str_list_t);
106 107

/* Case-sensitive sorting */
rswindell's avatar
rswindell committed
108 109
void		strListSortAlphaCase(str_list_t);
void		strListSortAlphaCaseReverse(str_list_t);
rswindell's avatar
rswindell committed
110

111 112 113 114
/* Create/free NULL-terminated string block (e.g. for environment variable blocks) */
char*		strListCreateBlock(str_list_t);
void		strListFreeBlock(char*);

115 116 117 118 119 120
/************/
/* File I/O */
/************/

/* Read lines from file appending each line to string list */
/* Pass NULL list to have list allocated for you */
rswindell's avatar
rswindell committed
121
str_list_t	strListReadFile(FILE*, str_list_t*, size_t max_line_len);
122 123

/* Write to file (fp) each string in the list, optionally separated by separator (e.g. "\n") */
rswindell's avatar
rswindell committed
124
size_t		strListWriteFile(FILE*, const str_list_t, const char* separator);
125

126 127 128 129 130
#if defined(__cplusplus)
}
#endif

#endif	/* Don't add anything after this line */