ini_file.h 9.65 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 2007 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
104
105
106
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);
107
108
ulong		iniReadBitField(FILE*, const char* section, const char* key
					,ini_bitdesc_t* bitdesc, ulong deflt);
109
#define		iniReadLogLevel(f,s,k,d) iniReadEnum(f,s,k,iniLogLevelStringList(),d)
110

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

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

rswindell's avatar
rswindell committed
117
118

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

125
/* StringList functions */
126
127
128
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);
129
named_string_t**
130
			iniGetNamedStringList(str_list_t list, const char* section);
131

132
char*		iniGetString(str_list_t, const char* section, const char* key
133
					,const char* deflt, char* value);
134
135
136
/* 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);
137
str_list_t	iniGetStringList(str_list_t, const char* section, const char* key
138
					,const char* sep, const char* deflt);
139
long		iniGetInteger(str_list_t, const char* section, const char* key
140
					,long deflt);
141
ushort		iniGetShortInt(str_list_t, const char* section, const char* key
142
					,ushort deflt);
143
144
ulong		iniGetLongInt(str_list_t, const char* section, const char* key
					,ulong deflt);
145
146
ulong		iniGetBytes(str_list_t, const char* section, const char* key
					,ulong unit, ulong deflt);
147
double		iniGetFloat(str_list_t, const char* section, const char* key
148
					,double deflt);
149
BOOL		iniGetBool(str_list_t, const char* section, const char* key
150
					,BOOL deflt);
151
152
time_t		iniGetDateTime(str_list_t, const char* section, const char* key
					,time_t deflt);
153
154
unsigned	iniGetEnum(str_list_t, const char* section, const char* key
					,str_list_t names, unsigned deflt);
155
156
157
158
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);
159
ulong		iniGetBitField(str_list_t, const char* section, const char* key
160
					,ini_bitdesc_t* bitdesc, ulong deflt);
161
str_list_t	iniGetSection(str_list_t, const char *section);
162
#define		iniGetLogLevel(l,s,k,d) iniGetEnum(l,s,k,iniLogLevelStringList(),d)
163
164
165
166
167
168
169
170

#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*);
171
172
int			iniGetSocketOptions(str_list_t, const char* section
					,SOCKET sock, char* error, size_t errlen);
173
#endif
174

175
176
void		iniSetDefaultStyle(ini_style_t);

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

207
208
209
size_t		iniAddSection(str_list_t*, const char* section
					,ini_style_t*);

210
211
212
size_t		iniAppendSection(str_list_t*, const char* section
					,ini_style_t*);

213
214
215
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);
216
BOOL		iniRemoveKey(str_list_t*, const char* section, const char* key);
217
BOOL		iniRemoveValue(str_list_t*, const char* section, const char* key);
rswindell's avatar
rswindell committed
218
BOOL		iniRemoveSection(str_list_t*, const char* section);
rswindell's avatar
rswindell committed
219
BOOL		iniRenameSection(str_list_t*, const char* section, const char* newname);
220

221
222
223
224
225
#if defined(__cplusplus)
}
#endif

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