fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * libini *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/libini/libini.h *
7 * Created: 2001-08-24 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2001-2010 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#ifndef LIBINI_H
24#define LIBINI_H 1
25
26
27#include <stdio.h>
28
29
30/*!***************************************************************************
31 * @short The value type identifiers
32 *****************************************************************************/
33#define INI_VAL_NONE 0
34#define INI_VAL_INT 1
35#define INI_VAL_STR 2
36
37
38/*!***************************************************************************
39 * @short The value struct
40 *****************************************************************************/
41typedef struct ini_val_s {
42 struct ini_val_s *next;
43
44 char *name;
45 unsigned type;
46
47 union {
48 char *str;
49 unsigned long u32;
50 } val;
51} ini_val_t;
52
53
54/*!***************************************************************************
55 * @short The ini section struct
56 *****************************************************************************/
57typedef struct ini_sct_s {
58 struct ini_sct_s *next;
59
60 struct ini_sct_s *parent;
61
62 char *name;
63
64 struct ini_sct_s *sub_head;
65 struct ini_sct_s *sub_tail;
66
67 ini_val_t *val_head;
68 ini_val_t *val_tail;
69} ini_sct_t;
70
71
72typedef struct {
73 unsigned cnt;
74 unsigned max;
75 char *str;
76} ini_strings_t;
77
78
79void ini_val_init (ini_val_t *val, const char *name);
80
81void ini_val_free (ini_val_t *val);
82
83ini_val_t *ini_val_new (const char *name);
84
85void ini_val_del (ini_val_t *val);
86
87void ini_val_set_none (ini_val_t *val);
88
89void ini_val_copy (ini_val_t *dst, const ini_val_t *src);
90
91/*!***************************************************************************
92 * @short Set a value to an unsigned long value
93 * @param val The value
94 * @param v The long value
95 *****************************************************************************/
96void ini_val_set_uint32 (ini_val_t *val, unsigned long v);
97
98/*!***************************************************************************
99 * @short Set a value to a long value
100 * @param val The value
101 * @param v The long value
102 *****************************************************************************/
103void ini_val_set_sint32 (ini_val_t *val, long v);
104
105/*!***************************************************************************
106 * @short Set a value to a boolean value
107 * @param val The value
108 * @param v The boolean value
109 *****************************************************************************/
110void ini_val_set_bool (ini_val_t *val, int v);
111
112/*!***************************************************************************
113 * @short Set a value to a string value
114 * @param val The value
115 * @param v The string value
116 *****************************************************************************/
117void ini_val_set_str (ini_val_t *val, const char *v);
118
119
120/*!***************************************************************************
121 * @short Get an unsigned long value
122 * @param val The value
123 * @retval v The uint32 value
124 * @return Zero if successful, nonzero otherwise
125 *****************************************************************************/
126int ini_val_get_uint32 (const ini_val_t *val, unsigned long *v);
127
128/*!***************************************************************************
129 * @short Get a long value
130 * @param val The value
131 * @retval v The sint32 value
132 * @return Zero if successful, nonzero otherwise
133 *****************************************************************************/
134int ini_val_get_sint32 (const ini_val_t *val, long *v);
135
136/*!***************************************************************************
137 * @short Get an unsigned int value
138 * @param val The value
139 * @retval v The uint16 value
140 * @return Zero if successful, nonzero otherwise
141 *****************************************************************************/
142int ini_val_get_uint16 (const ini_val_t *val, unsigned *v);
143
144/*!***************************************************************************
145 * @short Get an int value
146 * @param val The value
147 * @retval v The sint16 value
148 * @return Zero if successful, nonzero otherwise
149 *****************************************************************************/
150int ini_val_get_sint16 (const ini_val_t *val, int *v);
151
152/*!***************************************************************************
153 * @short Get a boolean value
154 * @param val The value
155 * @retval v The boolean value
156 * @return Zero if successful, nonzero otherwise
157 *****************************************************************************/
158int ini_val_get_bool (const ini_val_t *val, int *v);
159
160/*!***************************************************************************
161 * @short Get a string value
162 * @param val The value
163 * @return The string value or NULL on error
164 *****************************************************************************/
165const char *ini_val_get_str (const ini_val_t *val);
166
167
168/*!***************************************************************************
169 * @short Create a new section
170 * @param name The section name
171 * @return The new section or NULL on error
172 *****************************************************************************/
173ini_sct_t *ini_sct_new (const char *name);
174
175/*!***************************************************************************
176 * @short Delete a section list and all subsections
177 * @param sct The section
178 *****************************************************************************/
179void ini_sct_del (ini_sct_t *sct);
180
181ini_sct_t *ini_next_sct (ini_sct_t *sct, ini_sct_t *val, const char *name);
182ini_val_t *ini_next_val (ini_sct_t *sct, ini_val_t *val, const char *name);
183
184ini_sct_t *ini_get_sct (ini_sct_t *sct, const char *name, int add);
185ini_val_t *ini_get_val (ini_sct_t *sct, const char *name, int add);
186
187
188/*!***************************************************************************
189 * @short Set an unsigned long value
190 * @param sct The base section
191 * @param name The value name (as in "sect1.sect2.valname")
192 * @param v The value
193 * @return Zero if successful, nonzero otherwise
194 *****************************************************************************/
195int ini_set_uint32 (ini_sct_t *sct, const char *name, unsigned long v);
196
197/*!***************************************************************************
198 * @short Set a long value
199 * @param sct The base section
200 * @param name The value name (as in "sect1.sect2.valname")
201 * @param v The long value
202 * @return Zero if successful, nonzero otherwise
203 *****************************************************************************/
204int ini_set_sint32 (ini_sct_t *sct, const char *name, long v);
205
206/*!***************************************************************************
207 * @short Set a string value
208 * @param sct The base section
209 * @param name The value name (as in "sect1.sect2.valname")
210 * @param v The string value
211 * @return Zero if successful, nonzero otherwise
212 *****************************************************************************/
213int ini_set_str (ini_sct_t *sct, const char *name, const char *v);
214
215
216/*!***************************************************************************
217 * @short Get a value
218 * @param sct The base section
219 * @param name The value name (as in "sect1.sect2.valname")
220 * @retval ret The return value
221 * @return 0 if successful, nonzero otherwise
222 *****************************************************************************/
223int ini_get_uint32 (const ini_sct_t *sct, const char *name, unsigned long *ret, unsigned long def);
224
225/*!***************************************************************************
226 * @short Get a value
227 * @param sct The base section
228 * @param name The value name (as in "sect1.sect2.valname")
229 * @retval ret The return value
230 * @return 0 if successful, nonzero otherwise
231 *****************************************************************************/
232int ini_get_sint32 (const ini_sct_t *sct, const char *name, long *ret, long def);
233
234/*!***************************************************************************
235 * @short Get a value
236 * @param sct The base section
237 * @param name The value name (as in "sect1.sect2.valname")
238 * @retval ret The return value
239 * @return 0 if successful, nonzero otherwise
240 *****************************************************************************/
241int ini_get_uint16 (const ini_sct_t *sct, const char *name, unsigned *ret, unsigned def);
242
243/*!***************************************************************************
244 * @short Get a value
245 * @param sct The base section
246 * @param name The value name (as in "sect1.sect2.valname")
247 * @retval ret The return value
248 * @return 0 if successful, nonzero otherwise
249 *****************************************************************************/
250int ini_get_sint16 (const ini_sct_t *sct, const char *name, int *ret, int def);
251
252/*!***************************************************************************
253 * @short Get a boolean value
254 * @param val The value
255 * @retval v The boolean value
256 * @return Zero if successful, nonzero otherwise
257 *****************************************************************************/
258int ini_get_bool (const ini_sct_t *sct, const char *name, int *ret, int def);
259
260/*!***************************************************************************
261 * @short Get a string value
262 * @param sct The base section
263 * @param name The value name (as in "sect1.sect2.valname")
264 * @return The string or NULL on error
265 *****************************************************************************/
266int ini_get_string (const ini_sct_t *sct, const char *name, const char **ret, const char *def);
267
268
269void ini_str_init (ini_strings_t *is);
270void ini_str_free (ini_strings_t *is);
271int ini_str_add (ini_strings_t *is, const char *s1, const char *s2, const char *s3);
272int ini_str_eval (ini_strings_t *is, ini_sct_t *sct, int free);
273
274
275int ini_read_str (ini_sct_t *sct, const char *str);
276
277/*!***************************************************************************
278 * @short Read an ini tree from a file
279 * @param fp A C style stream that must be open for reading
280 * @return True on error
281 *****************************************************************************/
282int ini_read_fp (ini_sct_t *sct, FILE *fp, const char *fname);
283
284/*!***************************************************************************
285 * @short Read an ini tree from a file
286 * @param fname The file name
287 * @return True on error
288 *****************************************************************************/
289int ini_read (ini_sct_t *sct, const char *fname);
290
291
292/*!***************************************************************************
293 * @short Write an ini tree to a file
294 * @param fp A C style stream that must be open for writing
295 * @return Zero if successful, nonzero otherwise
296 *****************************************************************************/
297int ini_write_fp (FILE *fp, ini_sct_t *sct);
298
299/*!***************************************************************************
300 * @short Write an ini tree to a file
301 * @param fname The file name
302 * @return Zero if successful, nonzero otherwise
303 *****************************************************************************/
304int ini_write (const char *fname, ini_sct_t *sct);
305
306
307#endif