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

/* $Id$ */
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
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 _INI_FILE_H
#define _INI_FILE_H

#include "genwrap.h"
41
#include "str_list.h"	/* strList_t */
42
43
44
#if !defined(NO_SOCKET_SUPPORT)
	#include "sockwrap.h"	/* inet_addr, SOCKET */
#endif
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
55
56
typedef struct {
	int			key_len;
	const char* key_prefix;
57
	const char* section_separator;
58
59
	const char* value_separator;
	const char*	bit_separator;
rswindell's avatar
rswindell committed
60
	const 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 */
deuce's avatar
deuce committed
69
DLLEXPORT str_list_t DLLCALL	iniReadSectionList(FILE*, const char* prefix);
70
/* Returns number (count) of sections */
deuce's avatar
deuce committed
71
DLLEXPORT size_t DLLCALL		iniReadSectionCount(FILE*, const char* prefix);
72
/* Read all key names and return as an allocated string list */
deuce's avatar
deuce committed
73
DLLEXPORT str_list_t DLLCALL	iniReadKeyList(FILE*, const char* section);
74
/* Read all key and value pairs and return as a named string list */
deuce's avatar
deuce committed
75
DLLEXPORT named_string_t** DLLCALL
76
			iniReadNamedStringList(FILE*, const char* section);
77

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

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

87
/* These functions read a single key of the specified type */
deuce's avatar
deuce committed
88
DLLEXPORT char* DLLCALL		iniReadString(FILE*, const char* section, const char* key
89
					,const char* deflt, char* value);
90
/* If the key doesn't exist, iniReadExistingString just returns NULL */
deuce's avatar
deuce committed
91
DLLEXPORT char* DLLCALL		iniReadExistingString(FILE*, const char* section, const char* key
92
					,const char* deflt, char* value);
deuce's avatar
deuce committed
93
DLLEXPORT str_list_t DLLCALL	iniReadStringList(FILE*, const char* section, const char* key
94
					,const char* sep, const char* deflt);
deuce's avatar
deuce committed
95
DLLEXPORT long DLLCALL		iniReadInteger(FILE*, const char* section, const char* key
96
					,long deflt);
deuce's avatar
deuce committed
97
DLLEXPORT ushort DLLCALL		iniReadShortInt(FILE*, const char* section, const char* key
98
					,ushort deflt);
deuce's avatar
deuce committed
99
DLLEXPORT ulong DLLCALL		iniReadLongInt(FILE*, const char* section, const char* key
100
					,ulong deflt);
deuce's avatar
deuce committed
101
DLLEXPORT int64_t DLLCALL		iniReadBytes(FILE*, const char* section, const char* key
102
					,ulong unit, int64_t deflt);
103
104
DLLEXPORT double DLLCALL		iniReadDuration(FILE*, const char* section, const char* key
					,double deflt);
deuce's avatar
deuce committed
105
DLLEXPORT double DLLCALL		iniReadFloat(FILE*, const char* section, const char* key
106
					,double deflt);
deuce's avatar
deuce committed
107
DLLEXPORT BOOL DLLCALL		iniReadBool(FILE*, const char* section, const char* key
108
					,BOOL deflt);
deuce's avatar
deuce committed
109
DLLEXPORT time_t DLLCALL		iniReadDateTime(FILE*, const char* section, const char* key
110
					,time_t deflt);
deuce's avatar
deuce committed
111
DLLEXPORT unsigned DLLCALL	iniReadEnum(FILE*, const char* section, const char* key
112
					,str_list_t names, unsigned deflt);
deuce's avatar
deuce committed
113
DLLEXPORT unsigned* DLLCALL	iniReadEnumList(FILE*, const char* section, const char* key
114
					,str_list_t names, unsigned* count, const char* sep, const char* deflt);
deuce's avatar
deuce committed
115
DLLEXPORT long DLLCALL		iniReadNamedInt(FILE*, const char* section, const char* key
116
					,named_long_t*, long deflt);
117
118
DLLEXPORT ulong DLLCALL		iniReadNamedLongInt(FILE*, const char* section, const char* key
					,named_ulong_t*, ulong deflt);
deuce's avatar
deuce committed
119
DLLEXPORT double DLLCALL		iniReadNamedFloat(FILE*, const char* section, const char* key
120
					,named_double_t*, double deflt);
deuce's avatar
deuce committed
121
DLLEXPORT ulong DLLCALL		iniReadBitField(FILE*, const char* section, const char* key
122
					,ini_bitdesc_t* bitdesc, ulong deflt);
123
#define		iniReadLogLevel(f,s,k,d) iniReadEnum(f,s,k,iniLogLevelStringList(),d)
124

125
/* Free string list returned from iniRead*List functions */
deuce's avatar
deuce committed
126
DLLEXPORT void* DLLCALL		iniFreeStringList(str_list_t list);
127

128
/* Free named string list returned from iniReadNamedStringList */
deuce's avatar
deuce committed
129
DLLEXPORT void* DLLCALL		iniFreeNamedStringList(named_string_t** list);
130

rswindell's avatar
rswindell committed
131
132

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

139
/* StringList functions */
deuce's avatar
deuce committed
140
141
142
143
DLLEXPORT str_list_t DLLCALL	iniGetSectionList(str_list_t list, const char* prefix);
DLLEXPORT size_t DLLCALL		iniGetSectionCount(str_list_t list, const char* prefix);
DLLEXPORT str_list_t DLLCALL	iniGetKeyList(str_list_t list, const char* section);
DLLEXPORT named_string_t** DLLCALL
144
			iniGetNamedStringList(str_list_t list, const char* section);
145

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

/* Return the string value (potentially string literals separated by colon rather than equal): */
deuce's avatar
deuce committed
153
DLLEXPORT char* DLLCALL		iniGetString(str_list_t, const char* section, const char* key
154
					,const char* deflt, char* value /* may be NULL */);
155
/* If the key doesn't exist, iniGetExistingString just returns NULL */
deuce's avatar
deuce committed
156
DLLEXPORT char* DLLCALL		iniGetExistingString(str_list_t, const char* section, const char* key
157
					,const char* deflt, char* value /* may be NULL */);
deuce's avatar
deuce committed
158
DLLEXPORT str_list_t DLLCALL	iniGetStringList(str_list_t, const char* section, const char* key
159
					,const char* sep, const char* deflt);
deuce's avatar
deuce committed
160
DLLEXPORT long DLLCALL		iniGetInteger(str_list_t, const char* section, const char* key
161
					,long deflt);
deuce's avatar
deuce committed
162
DLLEXPORT ushort DLLCALL		iniGetShortInt(str_list_t, const char* section, const char* key
163
					,ushort deflt);
deuce's avatar
deuce committed
164
DLLEXPORT ulong DLLCALL		iniGetLongInt(str_list_t, const char* section, const char* key
165
					,ulong deflt);
166
DLLEXPORT int64_t DLLCALL	iniGetBytes(str_list_t, const char* section, const char* key
167
					,ulong unit, int64_t deflt);
168
169
170
DLLEXPORT double DLLCALL	iniGetDuration(str_list_t, const char* section, const char* key
					,double deflt);
DLLEXPORT double DLLCALL	iniGetFloat(str_list_t, const char* section, const char* key
171
					,double deflt);
deuce's avatar
deuce committed
172
DLLEXPORT BOOL DLLCALL		iniGetBool(str_list_t, const char* section, const char* key
173
					,BOOL deflt);
deuce's avatar
deuce committed
174
DLLEXPORT time_t DLLCALL		iniGetDateTime(str_list_t, const char* section, const char* key
175
					,time_t deflt);
deuce's avatar
deuce committed
176
DLLEXPORT unsigned DLLCALL	iniGetEnum(str_list_t, const char* section, const char* key
177
					,str_list_t names, unsigned deflt);
deuce's avatar
deuce committed
178
DLLEXPORT unsigned* DLLCALL	iniGetEnumList(str_list_t, const char* section, const char* key
179
					,str_list_t names, unsigned* count, const char* sep, const char* deflt);
deuce's avatar
deuce committed
180
DLLEXPORT long DLLCALL		iniGetNamedInt(str_list_t, const char* section, const char* key
181
					,named_long_t*, long deflt);
182
183
DLLEXPORT ulong DLLCALL		iniGetNamedLongInt(str_list_t, const char* section, const char* key
					,named_ulong_t*, ulong deflt);
deuce's avatar
deuce committed
184
DLLEXPORT double DLLCALL		iniGetNamedFloat(str_list_t, const char* section, const char* key
185
					,named_double_t*, double deflt);
deuce's avatar
deuce committed
186
DLLEXPORT ulong DLLCALL		iniGetBitField(str_list_t, const char* section, const char* key
187
					,ini_bitdesc_t* bitdesc, ulong deflt);
deuce's avatar
deuce committed
188
DLLEXPORT str_list_t DLLCALL	iniGetSection(str_list_t, const char *section);
189
#define		iniGetLogLevel(l,s,k,d) iniGetEnum(l,s,k,iniLogLevelStringList(),d)
190
191

#if !defined(NO_SOCKET_SUPPORT)
deuce's avatar
deuce committed
192
DLLEXPORT ulong DLLCALL		iniReadIpAddress(FILE*, const char* section, const char* key
193
					,ulong deflt);
deuce's avatar
deuce committed
194
DLLEXPORT ulong DLLCALL		iniGetIpAddress(str_list_t, const char* section, const char* key
195
					,ulong deflt);
deuce's avatar
deuce committed
196
DLLEXPORT char* DLLCALL		iniSetIpAddress(str_list_t*, const char* section, const char* key, ulong value
197
					,ini_style_t*);
deuce's avatar
deuce committed
198
DLLEXPORT struct in6_addr DLLCALL	iniReadIp6Address(FILE*, const char* section, const char* key
deuce's avatar
deuce committed
199
					,struct in6_addr deflt);
deuce's avatar
deuce committed
200
DLLEXPORT struct in6_addr DLLCALL		iniGetIp6Address(str_list_t, const char* section, const char* key
deuce's avatar
deuce committed
201
					,struct in6_addr deflt);
deuce's avatar
deuce committed
202
DLLEXPORT char* DLLCALL		iniSetIp6Address(str_list_t*, const char* section, const char* key, struct in6_addr value
deuce's avatar
deuce committed
203
					,ini_style_t*);
deuce's avatar
deuce committed
204
DLLEXPORT int DLLCALL			iniGetSocketOptions(str_list_t, const char* section
205
					,SOCKET sock, char* error, size_t errlen);
206
#endif
207

deuce's avatar
deuce committed
208
DLLEXPORT void DLLCALL		iniSetDefaultStyle(ini_style_t);
209

deuce's avatar
deuce committed
210
DLLEXPORT char* DLLCALL		iniSetString(str_list_t*, const char* section, const char* key, const char* value
211
					,ini_style_t*);
rswindell's avatar
rswindell committed
212
213
DLLEXPORT char* DLLCALL		iniSetStringLiteral(str_list_t*, const char* section, const char* key, const char* value
					,ini_style_t*);
214
215
DLLEXPORT char* DLLCALL		iniSetValue(str_list_t*, const char* section, const char* key, const char* value
					,ini_style_t*);
deuce's avatar
deuce committed
216
DLLEXPORT char* DLLCALL		iniSetInteger(str_list_t*, const char* section, const char* key, long value
217
					,ini_style_t*);
deuce's avatar
deuce committed
218
DLLEXPORT char* DLLCALL		iniSetShortInt(str_list_t*, const char* section, const char* key, ushort value
219
					,ini_style_t*);
deuce's avatar
deuce committed
220
DLLEXPORT char* DLLCALL		iniSetLongInt(str_list_t*, const char* section, const char* key, ulong value
221
					,ini_style_t*);
deuce's avatar
deuce committed
222
DLLEXPORT char* DLLCALL		iniSetBytes(str_list_t*, const char* section, const char* key, ulong unit, int64_t value
223
					,ini_style_t*);
224
225
DLLEXPORT char* DLLCALL		iniSetDuration(str_list_t*, const char* section, const char* key, double value
					,ini_style_t*);
deuce's avatar
deuce committed
226
DLLEXPORT char* DLLCALL		iniSetHexInt(str_list_t*, const char* section, const char* key, ulong value
227
					,ini_style_t*);
deuce's avatar
deuce committed
228
DLLEXPORT char* DLLCALL		iniSetFloat(str_list_t*, const char* section, const char* key, double value
229
					,ini_style_t*);
deuce's avatar
deuce committed
230
DLLEXPORT char* DLLCALL		iniSetBool(str_list_t*, const char* section, const char* key, BOOL value
231
					,ini_style_t*);
deuce's avatar
deuce committed
232
DLLEXPORT char* DLLCALL		iniSetDateTime(str_list_t*, const char* section, const char* key, BOOL include_time, time_t
233
					,ini_style_t*);
deuce's avatar
deuce committed
234
DLLEXPORT char* DLLCALL		iniSetEnum(str_list_t*, const char* section, const char* key, str_list_t names
235
					,unsigned value, ini_style_t*);
deuce's avatar
deuce committed
236
DLLEXPORT char* DLLCALL		iniSetEnumList(str_list_t*, const char* section, const char* key 
237
					,const char* sep, str_list_t names, unsigned* values, unsigned count, ini_style_t*);
deuce's avatar
deuce committed
238
DLLEXPORT char* DLLCALL		iniSetNamedInt(str_list_t*, const char* section, const char* key, named_long_t*
239
					,long value, ini_style_t*);
240
241
242
243
DLLEXPORT char* DLLCALL		iniSetNamedHexInt(str_list_t*, const char* section, const char* key, named_ulong_t*
					,ulong value, ini_style_t*);
DLLEXPORT char* DLLCALL		iniSetNamedLongInt(str_list_t*, const char* section, const char* key, named_ulong_t*
					,ulong value, ini_style_t*);
deuce's avatar
deuce committed
244
DLLEXPORT char* DLLCALL		iniSetNamedFloat(str_list_t*, const char* section, const char* key, named_double_t*
245
					,double value, ini_style_t*);
deuce's avatar
deuce committed
246
DLLEXPORT char* DLLCALL		iniSetBitField(str_list_t*, const char* section, const char* key, ini_bitdesc_t*, ulong value
247
					,ini_style_t*);
deuce's avatar
deuce committed
248
DLLEXPORT char* DLLCALL		iniSetStringList(str_list_t*, const char* section, const char* key
249
					,const char* sep, str_list_t value, ini_style_t*);
250
#define		iniSetLogLevel(l,s,k,v,style) iniSetEnum(l,s,k,iniLogLevelStringList(),v,style)
rswindell's avatar
rswindell committed
251

deuce's avatar
deuce committed
252
DLLEXPORT size_t DLLCALL		iniAddSection(str_list_t*, const char* section
253
254
					,ini_style_t*);

deuce's avatar
deuce committed
255
DLLEXPORT size_t DLLCALL		iniAppendSection(str_list_t*, const char* section
256
257
					,ini_style_t*);

deuce's avatar
deuce committed
258
259
260
261
DLLEXPORT BOOL DLLCALL		iniSectionExists(str_list_t, const char* section);
DLLEXPORT BOOL DLLCALL		iniKeyExists(str_list_t, const char* section, const char* key);
DLLEXPORT BOOL DLLCALL		iniValueExists(str_list_t, const char* section, const char* key);
DLLEXPORT char* DLLCALL		iniPopKey(str_list_t*, const char* section, const char* key, char* value);
262
DLLEXPORT char* DLLCALL		iniPopString(str_list_t*, const char* section, const char* key, char* value);
deuce's avatar
deuce committed
263
264
265
DLLEXPORT BOOL DLLCALL		iniRemoveKey(str_list_t*, const char* section, const char* key);
DLLEXPORT BOOL DLLCALL		iniRemoveValue(str_list_t*, const char* section, const char* key);
DLLEXPORT BOOL DLLCALL		iniRemoveSection(str_list_t*, const char* section);
266
DLLEXPORT BOOL DLLCALL		iniRemoveSections(str_list_t*, const char* prefex);
deuce's avatar
deuce committed
267
DLLEXPORT BOOL DLLCALL		iniRenameSection(str_list_t*, const char* section, const char* newname);
268

269
270
271
/*
 * Too handy to leave internal
 */
deuce's avatar
deuce committed
272
DLLEXPORT unsigned* DLLCALL parseEnumList(const char* values, const char* sep, str_list_t names, unsigned* count);
273

274
275
276
277
278
#if defined(__cplusplus)
}
#endif

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