Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

ini_file.h 14.5 KB
Newer Older
1
/* Functions to parse ini (initialization / configuration) files */
2

3
/* $Id: ini_file.h,v 1.60 2020/04/03 18:41:45 rswindell Exp $ */
rswindell's avatar
rswindell committed
4
// vi: tabstop=4
5 6 7 8 9

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
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
 *																			*
 * 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 _INI_FILE_H
#define _INI_FILE_H

40 41 42
#if !defined(NO_SOCKET_SUPPORT)
	#include "sockwrap.h"	/* inet_addr, SOCKET */
#endif
43 44
#include "genwrap.h"
#include "str_list.h"	/* strList_t */
45

46
#define INI_MAX_VALUE_LEN	1024		/* Maximum value length, includes '\0' */
rswindell's avatar
rswindell committed
47
#define ROOT_SECTION		NULL
48

49 50 51 52 53
typedef struct {
	ulong		bit;
	const char*	name;
} ini_bitdesc_t;

54
typedef struct {
55 56 57 58 59 60
	int		key_len;
	char*	key_prefix;
	char*	section_separator;
	char*	value_separator;
	char*	bit_separator;
	char*	literal_separator;
61 62
} ini_style_t;

63 64 65 66
#if defined(__cplusplus)
extern "C" {
#endif

67
/* Read all section names and return as an allocated string list */
68
/* Optionally (if prefix!=NULL), returns a subset of section names */
69
DLLEXPORT str_list_t 	iniReadSectionList(FILE*, const char* prefix);
70
/* Returns number (count) of sections */
71
DLLEXPORT size_t 		iniReadSectionCount(FILE*, const char* prefix);
72
/* Read all key names and return as an allocated string list */
73
DLLEXPORT str_list_t 	iniReadKeyList(FILE*, const char* section);
74
/* Read all key and value pairs and return as a named string list */
75 76
DLLEXPORT named_string_t** 
						iniReadNamedStringList(FILE*, const char* section);
77

78
/* Return the supported Log Levels in a string list - for *LogLevel macros */
79
DLLEXPORT str_list_t 	iniLogLevelStringList(void);
80

81
/* Return the unparsed/converted value */
82 83 84 85
DLLEXPORT char* 		iniReadValue(FILE*, const char* section, const char* key
							,const char* deflt, char* value);
DLLEXPORT char* 		iniReadExistingValue(FILE*, const char* section, const char* key
							,const char* deflt, char* value);
86

87
/* These functions read a single key of the specified type */
88 89
DLLEXPORT char* 		iniReadString(FILE*, const char* section, const char* key
							,const char* deflt, char* value);
90
/* If the key doesn't exist, iniReadExistingString just returns NULL */
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
DLLEXPORT char* 		iniReadExistingString(FILE*, const char* section, const char* key
							,const char* deflt, char* value);
DLLEXPORT str_list_t 	iniReadStringList(FILE*, const char* section, const char* key
							,const char* sep, const char* deflt);
DLLEXPORT long 			iniReadInteger(FILE*, const char* section, const char* key
							,long deflt);
DLLEXPORT ushort 		iniReadShortInt(FILE*, const char* section, const char* key
							,ushort deflt);
DLLEXPORT ulong 		iniReadLongInt(FILE*, const char* section, const char* key
							,ulong deflt);
DLLEXPORT int64_t 		iniReadBytes(FILE*, const char* section, const char* key
							,ulong unit, int64_t deflt);
DLLEXPORT double 		iniReadDuration(FILE*, const char* section, const char* key
							,double deflt);
DLLEXPORT double 		iniReadFloat(FILE*, const char* section, const char* key
							,double deflt);
DLLEXPORT BOOL 			iniReadBool(FILE*, const char* section, const char* key
							,BOOL deflt);
DLLEXPORT time_t 		iniReadDateTime(FILE*, const char* section, const char* key
							,time_t deflt);
DLLEXPORT unsigned 		iniReadEnum(FILE*, const char* section, const char* key
							,str_list_t names, unsigned deflt);
DLLEXPORT unsigned* 	iniReadEnumList(FILE*, const char* section, const char* key
							,str_list_t names, unsigned* count, const char* sep, const char* deflt);
DLLEXPORT int* 			iniReadIntList(FILE*, const char* section, const char* key
							,unsigned* count, const char* sep, const char* deflt);
DLLEXPORT long 			iniReadNamedInt(FILE*, const char* section, const char* key
							,named_long_t*, long deflt);
DLLEXPORT ulong 		iniReadNamedLongInt(FILE*, const char* section, const char* key
							,named_ulong_t*, ulong deflt);
DLLEXPORT double 		iniReadNamedFloat(FILE*, const char* section, const char* key
							,named_double_t*, double deflt);
DLLEXPORT ulong 		iniReadBitField(FILE*, const char* section, const char* key
							,ini_bitdesc_t* bitdesc, ulong deflt);
125
#define		iniReadLogLevel(f,s,k,d) iniReadEnum(f,s,k,iniLogLevelStringList(),d)
126

127
/* Free string list returned from iniRead*List functions */
128
DLLEXPORT void* 		iniFreeStringList(str_list_t list);
129

130
/* Free named string list returned from iniReadNamedStringList */
131
DLLEXPORT void* 		iniFreeNamedStringList(named_string_t** list);
132

rswindell's avatar
rswindell committed
133 134

/* File I/O Functions */
135 136 137 138 139
DLLEXPORT char* 		iniFileName(char* dest, size_t maxlen, const char* dir, const char* fname);
DLLEXPORT FILE* 		iniOpenFile(const char* fname, BOOL create);
DLLEXPORT str_list_t 	iniReadFile(FILE*);
DLLEXPORT BOOL 			iniWriteFile(FILE*, const str_list_t);
DLLEXPORT BOOL 			iniCloseFile(FILE*);
rswindell's avatar
rswindell committed
140

141
/* StringList functions */
142 143 144 145 146
DLLEXPORT str_list_t 	iniGetSectionList(str_list_t list, const char* prefix);
DLLEXPORT size_t 		iniGetSectionCount(str_list_t list, const char* prefix);
DLLEXPORT str_list_t 	iniGetKeyList(str_list_t list, const char* section);
DLLEXPORT named_string_t** 
						iniGetNamedStringList(str_list_t list, const char* section);
147

148
/* Return the unparsed value (string literals not supported): */
149 150 151 152
DLLEXPORT char* 		iniGetValue(str_list_t, const char* section, const char* key
							,const char* deflt, char* value /* may be NULL */);
DLLEXPORT char* 		iniGetExistingValue(str_list_t, const char* section, const char* key
							,const char* deflt, char* value /* may be NULL */);
153 154

/* Return the string value (potentially string literals separated by colon rather than equal): */
155 156
DLLEXPORT char* 		iniGetString(str_list_t, const char* section, const char* key
							,const char* deflt, char* value /* may be NULL */);
157
/* If the key doesn't exist, iniGetExistingString just returns NULL */
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
DLLEXPORT char* 		iniGetExistingString(str_list_t, const char* section, const char* key
							,const char* deflt, char* value /* may be NULL */);
DLLEXPORT str_list_t 	iniGetStringList(str_list_t, const char* section, const char* key
							,const char* sep, const char* deflt);
DLLEXPORT long 			iniGetInteger(str_list_t, const char* section, const char* key
							,long deflt);
DLLEXPORT ushort 		iniGetShortInt(str_list_t, const char* section, const char* key
							,ushort deflt);
DLLEXPORT ulong 		iniGetLongInt(str_list_t, const char* section, const char* key
							,ulong deflt);
DLLEXPORT int64_t 		iniGetBytes(str_list_t, const char* section, const char* key
							,ulong unit, int64_t deflt);
DLLEXPORT double 		iniGetDuration(str_list_t, const char* section, const char* key
							,double deflt);
DLLEXPORT double 		iniGetFloat(str_list_t, const char* section, const char* key
							,double deflt);
DLLEXPORT BOOL 			iniGetBool(str_list_t, const char* section, const char* key
							,BOOL deflt);
DLLEXPORT time_t 		iniGetDateTime(str_list_t, const char* section, const char* key
							,time_t deflt);
DLLEXPORT unsigned 		iniGetEnum(str_list_t, const char* section, const char* key
							,str_list_t names, unsigned deflt);
DLLEXPORT unsigned* 	iniGetEnumList(str_list_t, const char* section, const char* key
							,str_list_t names, unsigned* count, const char* sep, const char* deflt);
DLLEXPORT int* 			iniGetIntList(str_list_t, const char* section, const char* key
							,unsigned* count, const char* sep, const char* deflt);
DLLEXPORT long 			iniGetNamedInt(str_list_t, const char* section, const char* key
							,named_long_t*, long deflt);
DLLEXPORT ulong 		iniGetNamedLongInt(str_list_t, const char* section, const char* key
							,named_ulong_t*, ulong deflt);
DLLEXPORT double 		iniGetNamedFloat(str_list_t, const char* section, const char* key
							,named_double_t*, double deflt);
DLLEXPORT ulong 		iniGetBitField(str_list_t, const char* section, const char* key
							,ini_bitdesc_t* bitdesc, ulong deflt);
DLLEXPORT str_list_t 	iniGetSection(str_list_t, const char *section);
193
#define		iniGetLogLevel(l,s,k,d) iniGetEnum(l,s,k,iniLogLevelStringList(),d)
194 195

#if !defined(NO_SOCKET_SUPPORT)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
DLLEXPORT ulong 		iniReadIpAddress(FILE*, const char* section, const char* key
							,ulong deflt);
DLLEXPORT ulong 		iniGetIpAddress(str_list_t, const char* section, const char* key
							,ulong deflt);
DLLEXPORT char* 		iniSetIpAddress(str_list_t*, const char* section, const char* key, ulong value
							,ini_style_t*);
DLLEXPORT struct in6_addr
						iniReadIp6Address(FILE*, const char* section, const char* key
							,struct in6_addr deflt);
DLLEXPORT struct in6_addr
						iniGetIp6Address(str_list_t, const char* section, const char* key
							,struct in6_addr deflt);
DLLEXPORT char* 		iniSetIp6Address(str_list_t*, const char* section, const char* key, struct in6_addr value
							,ini_style_t*);
DLLEXPORT int 			iniGetSocketOptions(str_list_t, const char* section
							,SOCKET sock, char* error, size_t errlen);
212
#endif
213

214
DLLEXPORT void 			iniSetDefaultStyle(ini_style_t);
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
DLLEXPORT char* 		iniSetString(str_list_t*, const char* section, const char* key, const char* value
							,ini_style_t*);
DLLEXPORT char* 		iniSetStringLiteral(str_list_t*, const char* section, const char* key, const char* value
							,ini_style_t*);
DLLEXPORT char* 		iniSetValue(str_list_t*, const char* section, const char* key, const char* value
							,ini_style_t*);
DLLEXPORT char* 		iniSetInteger(str_list_t*, const char* section, const char* key, long value
							,ini_style_t*);
DLLEXPORT char* 		iniSetShortInt(str_list_t*, const char* section, const char* key, ushort value
							,ini_style_t*);
DLLEXPORT char* 		iniSetLongInt(str_list_t*, const char* section, const char* key, ulong value
							,ini_style_t*);
DLLEXPORT char* 		iniSetBytes(str_list_t*, const char* section, const char* key, ulong unit, int64_t value
							,ini_style_t*);
DLLEXPORT char* 		iniSetDuration(str_list_t*, const char* section, const char* key, double value
							,ini_style_t*);
DLLEXPORT char* 		iniSetHexInt(str_list_t*, const char* section, const char* key, ulong value
							,ini_style_t*);
DLLEXPORT char* 		iniSetFloat(str_list_t*, const char* section, const char* key, double value
							,ini_style_t*);
DLLEXPORT char* 		iniSetBool(str_list_t*, const char* section, const char* key, BOOL value
							,ini_style_t*);
DLLEXPORT char* 		iniSetDateTime(str_list_t*, const char* section, const char* key, BOOL include_time, time_t
							,ini_style_t*);
DLLEXPORT char* 		iniSetEnum(str_list_t*, const char* section, const char* key, str_list_t names
							,unsigned value, ini_style_t*);
DLLEXPORT char* 		iniSetEnumList(str_list_t*, const char* section, const char* key
							,const char* sep, str_list_t names, unsigned* values, unsigned count, ini_style_t*);
DLLEXPORT char* 		iniSetNamedInt(str_list_t*, const char* section, const char* key, named_long_t*
							,long value, ini_style_t*);
DLLEXPORT char* 		iniSetNamedHexInt(str_list_t*, const char* section, const char* key, named_ulong_t*
							,ulong value, ini_style_t*);
DLLEXPORT char* 		iniSetNamedLongInt(str_list_t*, const char* section, const char* key, named_ulong_t*
							,ulong value, ini_style_t*);
DLLEXPORT char* 		iniSetNamedFloat(str_list_t*, const char* section, const char* key, named_double_t*
							,double value, ini_style_t*);
DLLEXPORT char* 		iniSetBitField(str_list_t*, const char* section, const char* key, ini_bitdesc_t*, ulong value
							,ini_style_t*);
DLLEXPORT char* 		iniSetStringList(str_list_t*, const char* section, const char* key
							,const char* sep, str_list_t value, ini_style_t*);
256
#define		iniSetLogLevel(l,s,k,v,style) iniSetEnum(l,s,k,iniLogLevelStringList(),v,style)
257 258
DLLEXPORT char*			iniSetIntList(str_list_t*, const char* section, const char* key
							,const char* sep, int* value, unsigned count, ini_style_t*);
rswindell's avatar
rswindell committed
259

260 261
DLLEXPORT size_t 		iniAddSection(str_list_t*, const char* section
							,ini_style_t*);
262

263 264
DLLEXPORT size_t 		iniAppendSection(str_list_t*, const char* section
							,ini_style_t*);
265

266 267
DLLEXPORT size_t 		iniAppendSectionWithKeys(str_list_t*, const char* section, const str_list_t keys
							,ini_style_t*);
268

269 270 271 272 273 274 275 276 277 278
DLLEXPORT BOOL 			iniSectionExists(str_list_t, const char* section);
DLLEXPORT BOOL 			iniKeyExists(str_list_t, const char* section, const char* key);
DLLEXPORT BOOL 			iniValueExists(str_list_t, const char* section, const char* key);
DLLEXPORT char* 		iniPopKey(str_list_t*, const char* section, const char* key, char* value);
DLLEXPORT char* 		iniPopString(str_list_t*, const char* section, const char* key, char* value);
DLLEXPORT BOOL 			iniRemoveKey(str_list_t*, const char* section, const char* key);
DLLEXPORT BOOL 			iniRemoveValue(str_list_t*, const char* section, const char* key);
DLLEXPORT BOOL 			iniRemoveSection(str_list_t*, const char* section);
DLLEXPORT BOOL 			iniRemoveSections(str_list_t*, const char* prefix);
DLLEXPORT BOOL 			iniRenameSection(str_list_t*, const char* section, const char* newname);
279
DLLEXPORT BOOL 			iniSortSections(str_list_t*, BOOL sort_keys);
280

281 282
DLLEXPORT BOOL 			iniHasInclude(const str_list_t);

283 284 285
/*
 * Too handy to leave internal
 */
286 287
DLLEXPORT unsigned*		parseEnumList(const char* values, const char* sep, str_list_t names, unsigned* count);
DLLEXPORT int*			parseIntList(const char* values, const char* sep, unsigned* count);
288

289 290 291 292 293
#if defined(__cplusplus)
}
#endif

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