ini_file.h 10.3 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 2008 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 */
69
str_list_t	iniReadSectionList(FILE*, const char* prefix);
70
/* Read all key names and return as an allocated string list */
71
str_list_t	iniReadKeyList(FILE*, const char* section);
72
73
/* Read all key and value pairs and return as a named string list */
named_string_t**
74
			iniReadNamedStringList(FILE*, const char* section);
75

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

79
/* These functions read a single key of the specified type */
80
81
char*		iniReadString(FILE*, const char* section, const char* key
					,const char* deflt, char* value);
82
83
84
/* If the key doesn't exist, iniReadExistingString just returns NULL */
char*		iniReadExistingString(FILE*, const char* section, const char* key
					,const char* deflt, char* value);
85
str_list_t	iniReadStringList(FILE*, const char* section, const char* key
86
87
88
89
90
					,const char* sep, const char* deflt);
long		iniReadInteger(FILE*, const char* section, const char* key
					,long deflt);
ushort		iniReadShortInt(FILE*, const char* section, const char* key
					,ushort deflt);
91
92
ulong		iniReadLongInt(FILE*, const char* section, const char* key
					,ulong deflt);
93
94
ulong		iniReadBytes(FILE*, const char* section, const char* key
					,ulong unit, ulong deflt);
95
96
97
98
double		iniReadFloat(FILE*, const char* section, const char* key
					,double deflt);
BOOL		iniReadBool(FILE*, const char* section, const char* key
					,BOOL deflt);
99
100
time_t		iniReadDateTime(FILE*, const char* section, const char* key
					,time_t deflt);
101
102
unsigned	iniReadEnum(FILE*, const char* section, const char* key
					,str_list_t names, unsigned deflt);
103
unsigned*	iniReadEnumList(FILE*, const char* section, const char* key
104
					,str_list_t names, unsigned* count, const char* sep, const char* deflt);
105
106
107
108
long		iniReadNamedInt(FILE*, const char* section, const char* key
					,named_long_t*, long deflt);
double		iniReadNamedFloat(FILE*, const char* section, const char* key
					,named_double_t*, double deflt);
109
110
ulong		iniReadBitField(FILE*, const char* section, const char* key
					,ini_bitdesc_t* bitdesc, ulong deflt);
111
#define		iniReadLogLevel(f,s,k,d) iniReadEnum(f,s,k,iniLogLevelStringList(),d)
112

113
/* Free string list returned from iniRead*List functions */
114
void*		iniFreeStringList(str_list_t list);
115

116
/* Free named string list returned from iniReadNamedStringList */
117
118
void*		iniFreeNamedStringList(named_string_t** list);

rswindell's avatar
rswindell committed
119
120

/* File I/O Functions */
121
char*		iniFileName(char* dest, size_t maxlen, const char* dir, const char* fname);
122
FILE*		iniOpenFile(const char* fname, BOOL create);
123
124
str_list_t	iniReadFile(FILE*);
BOOL		iniWriteFile(FILE*, const str_list_t);
125
BOOL		iniCloseFile(FILE*);
rswindell's avatar
rswindell committed
126

127
/* StringList functions */
128
129
130
str_list_t	iniGetSectionList(str_list_t list, const char* prefix);
size_t		iniGetSectionCount(str_list_t list, const char* prefix);
str_list_t	iniGetKeyList(str_list_t list, const char* section);
131
named_string_t**
132
			iniGetNamedStringList(str_list_t list, const char* section);
133

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

#if !defined(NO_SOCKET_SUPPORT)
ulong		iniReadIpAddress(FILE*, const char* section, const char* key
					,ulong deflt);
ulong		iniGetIpAddress(str_list_t, const char* section, const char* key
					,ulong deflt);
char*		iniSetIpAddress(str_list_t*, const char* section, const char* key, ulong value
					,ini_style_t*);
175
176
int			iniGetSocketOptions(str_list_t, const char* section
					,SOCKET sock, char* error, size_t errlen);
177
#endif
178

179
180
void		iniSetDefaultStyle(ini_style_t);

181
char*		iniSetString(str_list_t*, const char* section, const char* key, const char* value
182
					,ini_style_t*);
183
char*		iniSetInteger(str_list_t*, const char* section, const char* key, long value
184
					,ini_style_t*);
185
char*		iniSetShortInt(str_list_t*, const char* section, const char* key, ushort value
186
					,ini_style_t*);
187
188
char*		iniSetLongInt(str_list_t*, const char* section, const char* key, ulong value
					,ini_style_t*);
189
190
char*		iniSetBytes(str_list_t*, const char* section, const char* key, ulong unit, ulong value
					,ini_style_t*);
191
char*		iniSetHexInt(str_list_t*, const char* section, const char* key, ulong value
192
					,ini_style_t*);
193
char*		iniSetFloat(str_list_t*, const char* section, const char* key, double value
194
					,ini_style_t*);
195
char*		iniSetBool(str_list_t*, const char* section, const char* key, BOOL value
196
					,ini_style_t*);
197
198
char*		iniSetDateTime(str_list_t*, const char* section, const char* key, BOOL include_time, time_t
					,ini_style_t*);
199
200
char*		iniSetEnum(str_list_t*, const char* section, const char* key, str_list_t names
					,unsigned value, ini_style_t*);
201
202
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*);
203
204
205
206
char*		iniSetNamedInt(str_list_t*, const char* section, const char* key, named_long_t*
					,long value, ini_style_t*);
char*		iniSetNamedFloat(str_list_t*, const char* section, const char* key, named_double_t*
					,double value, ini_style_t*);
207
208
209
210
char*		iniSetBitField(str_list_t*, const char* section, const char* key, ini_bitdesc_t*, ulong value
					,ini_style_t*);
char*		iniSetStringList(str_list_t*, const char* section, const char* key
					,const char* sep, str_list_t value, ini_style_t*);
211
#define		iniSetLogLevel(l,s,k,v,style) iniSetEnum(l,s,k,iniLogLevelStringList(),v,style)
rswindell's avatar
rswindell committed
212

213
214
215
size_t		iniAddSection(str_list_t*, const char* section
					,ini_style_t*);

216
217
218
size_t		iniAppendSection(str_list_t*, const char* section
					,ini_style_t*);

219
220
221
BOOL		iniSectionExists(str_list_t, const char* section);
BOOL		iniKeyExists(str_list_t, const char* section, const char* key);
BOOL		iniValueExists(str_list_t, const char* section, const char* key);
222
char*		iniPopKey(str_list_t*, const char* section, const char* key, char* value);
223
BOOL		iniRemoveKey(str_list_t*, const char* section, const char* key);
224
BOOL		iniRemoveValue(str_list_t*, const char* section, const char* key);
rswindell's avatar
rswindell committed
225
BOOL		iniRemoveSection(str_list_t*, const char* section);
rswindell's avatar
rswindell committed
226
BOOL		iniRenameSection(str_list_t*, const char* section, const char* newname);
227

228
229
230
231
232
/*
 * Too handy to leave internal
 */
unsigned* parseEnumList(const char* values, const char* sep, str_list_t names, unsigned* count);

233
234
235
236
237
#if defined(__cplusplus)
}
#endif

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