ini_file.h 12.5 KB
Newer Older
1
2
/* ini_file.h */

3
/* Functions to parse ini (initialization / configuration) files */
4
5
6
7
8
9
10

/* $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 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
41
 *																			*
 * 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"
42
#include "str_list.h"	/* strList_t */
43
44
45
#if !defined(NO_SOCKET_SUPPORT)
	#include "sockwrap.h"	/* inet_addr, SOCKET */
#endif
46

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

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

55
56
57
typedef struct {
	int			key_len;
	const char* key_prefix;
58
	const char* section_separator;
59
60
61
62
	const char* value_separator;
	const char*	bit_separator;
} 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
/* These functions read a single key of the specified type */
deuce's avatar
deuce committed
82
DLLEXPORT char* DLLCALL		iniReadString(FILE*, const char* section, const char* key
83
					,const char* deflt, char* value);
84
/* If the key doesn't exist, iniReadExistingString just returns NULL */
deuce's avatar
deuce committed
85
DLLEXPORT char* DLLCALL		iniReadExistingString(FILE*, const char* section, const char* key
86
					,const char* deflt, char* value);
deuce's avatar
deuce committed
87
DLLEXPORT str_list_t DLLCALL	iniReadStringList(FILE*, const char* section, const char* key
88
					,const char* sep, const char* deflt);
deuce's avatar
deuce committed
89
DLLEXPORT long DLLCALL		iniReadInteger(FILE*, const char* section, const char* key
90
					,long deflt);
deuce's avatar
deuce committed
91
DLLEXPORT ushort DLLCALL		iniReadShortInt(FILE*, const char* section, const char* key
92
					,ushort deflt);
deuce's avatar
deuce committed
93
DLLEXPORT ulong DLLCALL		iniReadLongInt(FILE*, const char* section, const char* key
94
					,ulong deflt);
deuce's avatar
deuce committed
95
DLLEXPORT int64_t DLLCALL		iniReadBytes(FILE*, const char* section, const char* key
96
					,ulong unit, int64_t deflt);
97
98
DLLEXPORT double DLLCALL		iniReadDuration(FILE*, const char* section, const char* key
					,double deflt);
deuce's avatar
deuce committed
99
DLLEXPORT double DLLCALL		iniReadFloat(FILE*, const char* section, const char* key
100
					,double deflt);
deuce's avatar
deuce committed
101
DLLEXPORT BOOL DLLCALL		iniReadBool(FILE*, const char* section, const char* key
102
					,BOOL deflt);
deuce's avatar
deuce committed
103
DLLEXPORT time_t DLLCALL		iniReadDateTime(FILE*, const char* section, const char* key
104
					,time_t deflt);
deuce's avatar
deuce committed
105
DLLEXPORT unsigned DLLCALL	iniReadEnum(FILE*, const char* section, const char* key
106
					,str_list_t names, unsigned deflt);
deuce's avatar
deuce committed
107
DLLEXPORT unsigned* DLLCALL	iniReadEnumList(FILE*, const char* section, const char* key
108
					,str_list_t names, unsigned* count, const char* sep, const char* deflt);
deuce's avatar
deuce committed
109
DLLEXPORT long DLLCALL		iniReadNamedInt(FILE*, const char* section, const char* key
110
					,named_long_t*, long deflt);
deuce's avatar
deuce committed
111
DLLEXPORT double DLLCALL		iniReadNamedFloat(FILE*, const char* section, const char* key
112
					,named_double_t*, double deflt);
deuce's avatar
deuce committed
113
DLLEXPORT ulong DLLCALL		iniReadBitField(FILE*, const char* section, const char* key
114
					,ini_bitdesc_t* bitdesc, ulong deflt);
115
#define		iniReadLogLevel(f,s,k,d) iniReadEnum(f,s,k,iniLogLevelStringList(),d)
116

117
/* Free string list returned from iniRead*List functions */
deuce's avatar
deuce committed
118
DLLEXPORT void* DLLCALL		iniFreeStringList(str_list_t list);
119

120
/* Free named string list returned from iniReadNamedStringList */
deuce's avatar
deuce committed
121
DLLEXPORT void* DLLCALL		iniFreeNamedStringList(named_string_t** list);
122

rswindell's avatar
rswindell committed
123
124

/* File I/O Functions */
deuce's avatar
deuce committed
125
126
127
128
129
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
130

131
/* StringList functions */
deuce's avatar
deuce committed
132
133
134
135
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
136
			iniGetNamedStringList(str_list_t list, const char* section);
137

deuce's avatar
deuce committed
138
DLLEXPORT char* DLLCALL		iniGetString(str_list_t, const char* section, const char* key
139
					,const char* deflt, char* value /* may be NULL */);
140
/* If the key doesn't exist, iniGetExistingString just returns NULL */
deuce's avatar
deuce committed
141
DLLEXPORT char* DLLCALL		iniGetExistingString(str_list_t, const char* section, const char* key
142
					,const char* deflt, char* value /* may be NULL */);
deuce's avatar
deuce committed
143
DLLEXPORT str_list_t DLLCALL	iniGetStringList(str_list_t, const char* section, const char* key
144
					,const char* sep, const char* deflt);
deuce's avatar
deuce committed
145
DLLEXPORT long DLLCALL		iniGetInteger(str_list_t, const char* section, const char* key
146
					,long deflt);
deuce's avatar
deuce committed
147
DLLEXPORT ushort DLLCALL		iniGetShortInt(str_list_t, const char* section, const char* key
148
					,ushort deflt);
deuce's avatar
deuce committed
149
DLLEXPORT ulong DLLCALL		iniGetLongInt(str_list_t, const char* section, const char* key
150
					,ulong deflt);
151
DLLEXPORT int64_t DLLCALL	iniGetBytes(str_list_t, const char* section, const char* key
152
					,ulong unit, int64_t deflt);
153
154
155
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
156
					,double deflt);
deuce's avatar
deuce committed
157
DLLEXPORT BOOL DLLCALL		iniGetBool(str_list_t, const char* section, const char* key
158
					,BOOL deflt);
deuce's avatar
deuce committed
159
DLLEXPORT time_t DLLCALL		iniGetDateTime(str_list_t, const char* section, const char* key
160
					,time_t deflt);
deuce's avatar
deuce committed
161
DLLEXPORT unsigned DLLCALL	iniGetEnum(str_list_t, const char* section, const char* key
162
					,str_list_t names, unsigned deflt);
deuce's avatar
deuce committed
163
DLLEXPORT unsigned* DLLCALL	iniGetEnumList(str_list_t, const char* section, const char* key
164
					,str_list_t names, unsigned* count, const char* sep, const char* deflt);
deuce's avatar
deuce committed
165
DLLEXPORT long DLLCALL		iniGetNamedInt(str_list_t, const char* section, const char* key
166
					,named_long_t*, long deflt);
deuce's avatar
deuce committed
167
DLLEXPORT double DLLCALL		iniGetNamedFloat(str_list_t, const char* section, const char* key
168
					,named_double_t*, double deflt);
deuce's avatar
deuce committed
169
DLLEXPORT ulong DLLCALL		iniGetBitField(str_list_t, const char* section, const char* key
170
					,ini_bitdesc_t* bitdesc, ulong deflt);
deuce's avatar
deuce committed
171
DLLEXPORT str_list_t DLLCALL	iniGetSection(str_list_t, const char *section);
172
#define		iniGetLogLevel(l,s,k,d) iniGetEnum(l,s,k,iniLogLevelStringList(),d)
173
174

#if !defined(NO_SOCKET_SUPPORT)
deuce's avatar
deuce committed
175
DLLEXPORT ulong DLLCALL		iniReadIpAddress(FILE*, const char* section, const char* key
176
					,ulong deflt);
deuce's avatar
deuce committed
177
DLLEXPORT ulong DLLCALL		iniGetIpAddress(str_list_t, const char* section, const char* key
178
					,ulong deflt);
deuce's avatar
deuce committed
179
DLLEXPORT char* DLLCALL		iniSetIpAddress(str_list_t*, const char* section, const char* key, ulong value
180
					,ini_style_t*);
deuce's avatar
deuce committed
181
DLLEXPORT struct in6_addr DLLCALL	iniReadIp6Address(FILE*, const char* section, const char* key
deuce's avatar
deuce committed
182
					,struct in6_addr deflt);
deuce's avatar
deuce committed
183
DLLEXPORT struct in6_addr DLLCALL		iniGetIp6Address(str_list_t, const char* section, const char* key
deuce's avatar
deuce committed
184
					,struct in6_addr deflt);
deuce's avatar
deuce committed
185
DLLEXPORT char* DLLCALL		iniSetIp6Address(str_list_t*, const char* section, const char* key, struct in6_addr value
deuce's avatar
deuce committed
186
					,ini_style_t*);
deuce's avatar
deuce committed
187
DLLEXPORT int DLLCALL			iniGetSocketOptions(str_list_t, const char* section
188
					,SOCKET sock, char* error, size_t errlen);
189
#endif
190

deuce's avatar
deuce committed
191
DLLEXPORT void DLLCALL		iniSetDefaultStyle(ini_style_t);
192

deuce's avatar
deuce committed
193
DLLEXPORT char* DLLCALL		iniSetString(str_list_t*, const char* section, const char* key, const char* value
194
					,ini_style_t*);
deuce's avatar
deuce committed
195
DLLEXPORT char* DLLCALL		iniSetInteger(str_list_t*, const char* section, const char* key, long value
196
					,ini_style_t*);
deuce's avatar
deuce committed
197
DLLEXPORT char* DLLCALL		iniSetShortInt(str_list_t*, const char* section, const char* key, ushort value
198
					,ini_style_t*);
deuce's avatar
deuce committed
199
DLLEXPORT char* DLLCALL		iniSetLongInt(str_list_t*, const char* section, const char* key, ulong value
200
					,ini_style_t*);
deuce's avatar
deuce committed
201
DLLEXPORT char* DLLCALL		iniSetBytes(str_list_t*, const char* section, const char* key, ulong unit, int64_t value
202
					,ini_style_t*);
203
204
DLLEXPORT char* DLLCALL		iniSetDuration(str_list_t*, const char* section, const char* key, double value
					,ini_style_t*);
deuce's avatar
deuce committed
205
DLLEXPORT char* DLLCALL		iniSetHexInt(str_list_t*, const char* section, const char* key, ulong value
206
					,ini_style_t*);
deuce's avatar
deuce committed
207
DLLEXPORT char* DLLCALL		iniSetFloat(str_list_t*, const char* section, const char* key, double value
208
					,ini_style_t*);
deuce's avatar
deuce committed
209
DLLEXPORT char* DLLCALL		iniSetBool(str_list_t*, const char* section, const char* key, BOOL value
210
					,ini_style_t*);
deuce's avatar
deuce committed
211
DLLEXPORT char* DLLCALL		iniSetDateTime(str_list_t*, const char* section, const char* key, BOOL include_time, time_t
212
					,ini_style_t*);
deuce's avatar
deuce committed
213
DLLEXPORT char* DLLCALL		iniSetEnum(str_list_t*, const char* section, const char* key, str_list_t names
214
					,unsigned value, ini_style_t*);
deuce's avatar
deuce committed
215
DLLEXPORT char* DLLCALL		iniSetEnumList(str_list_t*, const char* section, const char* key 
216
					,const char* sep, str_list_t names, unsigned* values, unsigned count, ini_style_t*);
deuce's avatar
deuce committed
217
DLLEXPORT char* DLLCALL		iniSetNamedInt(str_list_t*, const char* section, const char* key, named_long_t*
218
					,long value, ini_style_t*);
deuce's avatar
deuce committed
219
DLLEXPORT char* DLLCALL		iniSetNamedFloat(str_list_t*, const char* section, const char* key, named_double_t*
220
					,double value, ini_style_t*);
deuce's avatar
deuce committed
221
DLLEXPORT char* DLLCALL		iniSetBitField(str_list_t*, const char* section, const char* key, ini_bitdesc_t*, ulong value
222
					,ini_style_t*);
deuce's avatar
deuce committed
223
DLLEXPORT char* DLLCALL		iniSetStringList(str_list_t*, const char* section, const char* key
224
					,const char* sep, str_list_t value, ini_style_t*);
225
#define		iniSetLogLevel(l,s,k,v,style) iniSetEnum(l,s,k,iniLogLevelStringList(),v,style)
rswindell's avatar
rswindell committed
226

deuce's avatar
deuce committed
227
DLLEXPORT size_t DLLCALL		iniAddSection(str_list_t*, const char* section
228
229
					,ini_style_t*);

deuce's avatar
deuce committed
230
DLLEXPORT size_t DLLCALL		iniAppendSection(str_list_t*, const char* section
231
232
					,ini_style_t*);

deuce's avatar
deuce committed
233
234
235
236
237
238
239
240
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);
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);
DLLEXPORT BOOL DLLCALL		iniRenameSection(str_list_t*, const char* section, const char* newname);
241

242
243
244
/*
 * Too handy to leave internal
 */
deuce's avatar
deuce committed
245
DLLEXPORT unsigned* DLLCALL parseEnumList(const char* values, const char* sep, str_list_t names, unsigned* count);
246

247
248
249
250
251
#if defined(__cplusplus)
}
#endif

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