str_list.h 4.69 KB
Newer Older
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
/* 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)		*
 *																			*
 * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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) */
56
void		strListFree(str_list_t* list);
57

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

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

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

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

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

74
/* Remove a string at a specific index */
75 76 77 78 79 80 81
char*		strListRemove(str_list_t* list, size_t index);

/* Remove and free a string at a specific index */
BOOL		strListDelete(str_list_t* list, size_t index);

/* Replace a string at a specific index */
char*		strListReplace(const str_list_t list, size_t index, const char* str);
82

83 84 85 86
/* 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)

87 88 89 90 91 92
/* Add to an exiting or new string list by splitting specified string (str) */
/* into multiple strings, separated by one of the delimit characters */
str_list_t	strListSplit(str_list_t* list, char* str, const char* delimit);

/* Same as above, but copies str to temporary heap buffer first */
str_list_t	strListSplitCopy(str_list_t* list, const char* str, const char* delimit);
93

94
/* Merge 2 string lists (no copying of string data) */
95
size_t		strListMerge(str_list_t* list, str_list_t append_list);
96

97
/* Count the number of strings in the list and returns the count */
98
size_t		strListCount(const str_list_t list);
99

100
/* Sort the strings in the string list */
rswindell's avatar
rswindell committed
101 102
void		strListSortAlpha(str_list_t list);
void		strListSortAlphaReverse(str_list_t list);
103 104

/* Case-sensitive sorting */
rswindell's avatar
rswindell committed
105 106 107
void		strListSortAlphaCase(str_list_t list);
void		strListSortAlphaCaseReverse(str_list_t list);

108 109 110 111 112 113
/************/
/* File I/O */
/************/

/* Read lines from file appending each line to string list */
/* Pass NULL list to have list allocated for you */
114
str_list_t	strListReadFile(FILE* fp, str_list_t* list, size_t max_line_len);
115 116 117 118

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

119 120 121 122 123
#if defined(__cplusplus)
}
#endif

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