Mstdlib-1.24.0
m_conf.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_CONF_H__
25#define __M_CONF_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28
29#include <mstdlib/mstdlib.h>
30
31/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
32
33__BEGIN_DECLS
34
35/*! \addtogroup m_conf CONF
36 * \ingroup m_formats
37 *
38 * Wrapper around mstdlib's INI module for parsing configuration files and saving
39 * values. The file must be formatted as described in the INI module. This does
40 * not cover other file formats, such as JSON or XML.
41 *
42 * This module is used for reading values from a configuration file directly into
43 * the provided memory. If you want to hold the values in temporary memory for
44 * manipulation and retrieval, you should use the Settings module.
45 *
46 * You begin by building out all the key registrations, which specify the key to
47 * parse and where to store the value. There are multiple methods to handle the
48 * various data types that can be set. If you need to set a non-standardized data
49 * type like an enum or struct, you should use the custom registration method.
50 *
51 * When building a key registration, you can also specify validators (depending on
52 * the data type) and a default value to use if a value isn't specified in the
53 * config file.
54 *
55 * Every registration type has a corresponding conversion callback specific to it.
56 * If a callback is set with the registration, then that callback must do all the
57 * work of validating, converting, and storing the value.
58 *
59 * Once all the registrations are set, you send the call to run through them all
60 * at once. We made the design decision to set everything up first and then parse
61 * the values over parsing on the fly as keys are registered so that all errors
62 * would be contained in one area. Instead of needing to do error checking for
63 * every registration, you only have to check the outcome of M_conf_parse().
64 *
65 * Alternatively, you can access a key's value directly without setting up a
66 * registration.
67 *
68 * To received debug and/or error messages, you can register a callback that will
69 * be provided the message as well as the filename of the file currently being
70 * processed. This is optional.
71 *
72 * Example:
73 *
74 * \code{.c}
75 * M_int8 num1;
76 * M_uint32 num2;
77 *
78 * static void log_conf_debug(const char *path, const char *msg)
79 * {
80 * M_printf("[DEBUG] %s: %s\n", path, msg);
81 * }
82 * static void log_conf_error(const char *path, const char *msg)
83 * {
84 * M_printf("[ERROR] %s: %s\n", path, msg);
85 * }
86 *
87 * static M_bool handle_num2(M_uint32 *mem, const char *value, M_uint32 default_val)
88 * {
89 * M_int64 num;
90 *
91 * if (M_str_isempty(value)) {
92 * *mem = default_val;
93 * return M_TRUE;
94 * }
95 *
96 * num = M_str_to_int64(value);
97 * if (num < 0) {
98 * *mem = 0;
99 * } else if (num > 64) {
100 * *mem = 64;
101 * } else {
102 * *mem = num;
103 * }
104 *
105 * return M_TRUE;
106 * }
107 *
108 * static M_bool handle_flags(void *mem, const char *value)
109 * {
110 * M_uint64 *flags = mem;
111 * char **parts;
112 * size_t num_parts;
113 * size_t i;
114 *
115 * *flags = 0;
116 *
117 * parts = M_str_explode_str('|', value, &num_parts);
118 * if (parts == NULL)
119 * return M_FALSE;
120 *
121 * for (i=0; i<num_parts; i++) {
122 * *flags |= parse_flag(parts[i]);
123 * }
124 * M_str_explode_free(parts, num_parts);
125 *
126 * return M_TRUE;
127 * }
128 *
129 * static M_bool validate_data(void *data)
130 * {
131 * char *name_buf = data;
132 *
133 * if (M_str_len(name_buf) > 128) {
134 * return M_FALSE;
135 * }
136 *
137 * if (num2 < num1) {
138 * return M_FALSE;
139 * }
140 *
141 * return M_TRUE;
142 * }
143 *
144 * M_bool parse_conf(const char *path)
145 * {
146 * M_conf_t *conf;
147 * char err_buf[256];
148 * char name_buf[256];
149 * char *desc;
150 * M_bool active;
151 * M_uint64 flags;
152 * M_bool ret;
153 *
154 * conf = M_conf_create(path, M_FALSE, err_buf, sizeof(err_buf));
155 * if (conf == NULL) {
156 * M_printf("Error reading conf: %s", err_buf);
157 * return M_FALSE;
158 * }
159 *
160 * M_conf_add_debug_logger(conf, log_conf_debug);
161 * M_conf_add_error_logger(conf, log_conf_error);
162 *
163 * M_conf_register_buf(conf, "name", name_buf, sizeof(name_buf), "unknown", "^[a-zA-Z]$", NULL);
164 * M_conf_register_strdup(conf, "description", &desc, NULL, NULL, NULL);
165 * M_conf_register_int8(conf, "num1", &num1, 0, -10, 10, NULL);
166 * M_conf_register_uint32(conf, "num2", &num2, 16, 0, 0, handle_num2);
167 * M_conf_register_bool(conf, "active", &active, M_FALSE, NULL);
168 * M_conf_register_custom(conf, "flags", &flags, handle_flags);
169 *
170 * M_conf_register_validator(conf, validate_data, name_buf);
171 *
172 * ret = M_conf_parse(conf);
173 *
174 * M_conf_destroy(conf);
175 *
176 * if (ret) {
177 * M_printf("Successfully parsed conf");
178 * } else {
179 * M_printf("Error parsing conf");
180 * }
181 *
182 * return ret;
183 * }
184 * \endcode
185 *
186 * @{
187 */
188
189/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
190
191struct M_conf_t;
192typedef struct M_conf_t M_conf_t;
193
194/*! Callback prototype for logging messages while parsing values.
195 *
196 * \param[in] path Path of configuration file being read.
197 * \param[in] msg Message to log.
198 */
199typedef void (*M_conf_logger_t)(const char *path, const char *msg);
200
201/*! Callback prototype for manual string-to-string conversions.
202 *
203 * \param[in] buf Buffer to store the value.
204 * \param[in] buf_len Length of buffer.
205 * \param[in] value The value in the configuration file for the registered key. May be NULL.
206 * \param[in] default_val The default value as registered for the key.
207 *
208 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
209 */
210typedef M_bool (*M_conf_converter_buf_t)(char *buf, size_t buf_len, const char *value, const char *default_val);
211
212/*! Callback prototype for manual string-to-string conversions.
213 *
214 * \param[in] mem Memory address where the value should be stored. The caller is responsible for free'ing this.
215 * \param[in] value The value in the configuration file for the registered key. May be NULL.
216 * \param[in] default_val The default value as registered for the key.
217 *
218 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
219 */
220typedef M_bool (*M_conf_converter_strdup_t)(char **mem, const char *value, const char *default_val);
221
222/*! Callback prototype for manual string-to-integer conversions for signed 8-bit integers.
223 *
224 * \param[in] mem Memory address where the value should be stored.
225 * \param[in] value The value in the configuration file for the registered key. May be NULL.
226 * \param[in] default_val The default value as registered for the key.
227 *
228 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
229 */
230typedef M_bool (*M_conf_converter_int8_t)(M_int8 *mem, const char *value, M_int8 default_val);
231
232/*! Callback prototype for manual string-to-integer conversions for signed 16-bit integers.
233 *
234 * \param[in] mem Memory address where the value should be stored.
235 * \param[in] value The value in the configuration file for the registered key. May be NULL.
236 * \param[in] default_val The default value as registered for the key.
237 *
238 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
239 */
240typedef M_bool (*M_conf_converter_int16_t)(M_int16 *mem, const char *value, M_int16 default_val);
241
242/*! Callback prototype for manual string-to-integer conversions for signed 32-bit integers.
243 *
244 * \param[in] mem Memory address where the value should be stored.
245 * \param[in] value The value in the configuration file for the registered key. May be NULL.
246 * \param[in] default_val The default value as registered for the key.
247 *
248 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
249 */
250typedef M_bool (*M_conf_converter_int32_t)(M_int32 *mem, const char *value, M_int32 default_val);
251
252/*! Callback prototype for manual string-to-integer conversions for signed 64-bit integers.
253 *
254 * \param[in] mem Memory address where the value should be stored.
255 * \param[in] value The value in the configuration file for the registered key. May be NULL.
256 * \param[in] default_val The default value as registered for the key.
257 *
258 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
259 */
260typedef M_bool (*M_conf_converter_int64_t)(M_int64 *mem, const char *value, M_int64 default_val);
261
262/*! Callback prototype for manual string-to-integer conversions for unsigned 8-bit integers.
263 *
264 * \param[in] mem Memory address where the value should be stored.
265 * \param[in] value The value in the configuration file for the registered key. May be NULL.
266 * \param[in] default_val The default value as registered for the key.
267 *
268 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
269 */
270typedef M_bool (*M_conf_converter_uint8_t)(M_uint8 *mem, const char *value, M_uint8 default_val);
271
272/*! Callback prototype for manual string-to-integer conversions for unsigned 16-bit integers.
273 *
274 * \param[in] mem Memory address where the value should be stored.
275 * \param[in] value The value in the configuration file for the registered key. May be NULL.
276 * \param[in] default_val The default value as registered for the key.
277 *
278 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
279 */
280typedef M_bool (*M_conf_converter_uint16_t)(M_uint16 *mem, const char *value, M_uint16 default_val);
281
282/*! Callback prototype for manual string-to-integer conversions for unsigned 32-bit integers.
283 *
284 * \param[in] mem Memory address where the value should be stored.
285 * \param[in] value The value in the configuration file for the registered key. May be NULL.
286 * \param[in] default_val The default value as registered for the key.
287 *
288 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
289 */
290typedef M_bool (*M_conf_converter_uint32_t)(M_uint32 *mem, const char *value, M_uint32 default_val);
291
292/*! Callback prototype for manual string-to-integer conversions for unsigned 64-bit integers.
293 *
294 * \param[in] mem Memory address where the value should be stored.
295 * \param[in] value The value in the configuration file for the registered key. May be NULL.
296 * \param[in] default_val The default value as registered for the key.
297 *
298 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
299 */
300typedef M_bool (*M_conf_converter_uint64_t)(M_uint64 *mem, const char *value, M_uint64 default_val);
301
302/*! Callback prototype for manual string-to-integer conversions for size_t integers.
303 *
304 * \param[in] mem Memory address where the value should be stored.
305 * \param[in] value The value in the configuration file for the registered key. May be NULL.
306 * \param[in] default_val The default value as registered for the key.
307 *
308 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
309 */
310typedef M_bool (*M_conf_converter_sizet_t)(size_t *mem, const char *value, size_t default_val);
311
312/*! Callback prototype for manual string-to-boolean conversions.
313 *
314 * \param[in] mem Memory address where the value should be stored.
315 * \param[in] value The value in the configuration file for the registered key. May be NULL.
316 * \param[in] default_val The default value as registered for the key.
317 *
318 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
319 */
320typedef M_bool (*M_conf_converter_bool_t)(M_bool *mem, const char *value, M_bool default_val);
321
322/*! Callback prototype for custom conversions. This is used for manually validating and setting a value with
323 * M_conf_register_custom();
324 *
325 * \param[in] mem Memory address where the value should be stored. May be NULL if address was not registered.
326 * \param[in] value The value in the configuration file for the registered key. May be NULL.
327 *
328 * \return M_TRUE if the conversion was successful. Otherwise, M_FALSE.
329 */
330typedef M_bool (*M_conf_converter_custom_t)(void *mem, const char *value);
331
332/*! Callback prototype for validating arbitrary data.
333 *
334 * \param[in] data Arbitrary data, as registered with the callback. May be NULL.
335 *
336 * \return M_TRUE if the validation was successful. Otherwise, M_FALSE.
337 */
338typedef M_bool (*M_conf_validator_t)(void *data);
339
340/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
341
342/*! Create a new M_conf_t object with the specified ini.
343 *
344 * \param[in] path Path to the ini file.
345 * \param[in] allow_multiple M_TRUE to allow a single key to have multiple values. Otherwise, M_FALSE.
346 * \param[in] errbuf Buffer to hold error message. Only populated if return is NULL.
347 * \param[in] errbuf_len Size of buffer for holding error message.
348 *
349 * \return A newly allocated M_conf_t object, or NULL on error.
350 */
351M_API M_conf_t *M_conf_create(const char *path, M_bool allow_multiple, char *errbuf, size_t errbuf_len);
352
353
354/*! Destroy an M_conf_t object and free the memory.
355 *
356 * Before destruction, all unused keys will be logged with the debug loggers (if any).
357 *
358 * \param[in] conf The M_conf_t object to destroy.
359 */
360M_API void M_conf_destroy(M_conf_t *conf);
361
362
363/*! Add a debug logger to this conf object that will be passed various debug messages while parsing the values.
364 *
365 * \param[in] conf The M_conf_t object to attach this logger to.
366 * \param[in] debug_logger The logging callback that will receive the error message.
367 */
368M_API M_bool M_conf_add_debug_logger(M_conf_t *conf, M_conf_logger_t debug_logger);
369
370
371/*! Add an error logger to this conf object that will be passed any errors that happen while parsing the values.
372 *
373 * \param[in] conf The M_conf_t object to attach this logger to.
374 * \param[in] error_logger The logging callback that will receive the error message.
375 */
376M_API M_bool M_conf_add_error_logger(M_conf_t *conf, M_conf_logger_t error_logger);
377
378
379/*! Go through the key registrations and set the values at the specified locations.
380 *
381 * \param[in] conf The M_conf_t object to use.
382 *
383 * \return M_TRUE if the registrations were processed successfully. Otherwise, M_FALSE. This fails if any of
384 * the registrations fail, like if a value does not pass the regex check or is outside of the min/max
385 * bounds.
386 */
387M_API M_bool M_conf_parse(M_conf_t *conf);
388
389
390/*! Get a list of keys from the ini file that were not used.
391 *
392 * \param[in] conf The M_conf_t object to use.
393 *
394 * \return A list of keys.
395 */
397
398
399/*! Get a list of the section in the ini file.
400 *
401 * \param[in] conf The M_conf_t object to use.
402 *
403 * \return A list of sections.
404 */
406
407
408/*! Get the value for the provided key in the ini file. If a single key is allowed to have multiple values, then this
409 * returns the first value.
410 *
411 * \param[in] conf The M_conf_t object to use.
412 * \param[in] key The key to look up.
413 *
414 * \return The value for the key.
415 */
416M_API const char *M_conf_get_value(M_conf_t *conf, const char *key);
417
418
419/*! Get all the values for the provided key in the ini file.
420 *
421 * \param[in] conf The M_conf_t object to use.
422 * \param[in] key The key to look up.
423 *
424 * \return A list of values for the key.
425 */
426M_API M_list_str_t *M_conf_get_values(M_conf_t *conf, const char *key);
427
428
429/*! Register a key that will have its value stored in the provided char buf.
430 *
431 * \param[in] conf M_conf_t object to use.
432 * \param[in] key Key to register.
433 * \param[out] buf Buffer where the value will be stored.
434 * \param[in] buf_len Length of buffer.
435 * \param[in] default_val Default value to store, if a value is not set in the ini file. Pass NULL for no default.
436 * \param[in] regex Regular expression to check the value against. Matching is done in a case-insensitive
437 * fashion. If the check fails, then M_conf_parse() will also fail and the default value will
438 * be stored. Pass NULL to skip check.
439 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
440 * to the callback, which must do all validation/conversion. The value passed to the callback
441 * can be NULL. Pass NULL if not needed.
442 *
443 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
444 * M_FALSE only if any of the arguments are invalid.
445 */
446M_API M_bool M_conf_register_buf(M_conf_t *conf, const char *key, char *buf, size_t buf_len, const char *default_val, const char *regex, M_conf_converter_buf_t converter);
447
448
449/*! Register a key that will have its value stored at the provided address as an allocated string.
450 *
451 * \param[in] conf M_conf_t object to use.
452 * \param[in] key Key to register.
453 * \param[out] address Address where the value will be stored. The caller is responsible for free'ing this memory.
454 * \param[in] default_val Default value to store, if a value is not set in the ini file. Pass NULL for no default.
455 * \param[in] regex Regular expression to check the value against. Matching is done in a case-insensitive
456 * fashion. If the check fails, then M_conf_parse() will also fail and the default value will
457 * be stored. Pass NULL to skip check.
458 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
459 * to the callback, which must do all validation/conversion. The value passed to the callback
460 * can be NULL. Pass NULL if not needed.
461 *
462 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
463 * M_FALSE only if any of the arguments are invalid.
464 */
465M_API M_bool M_conf_register_strdup(M_conf_t *conf, const char *key, char **address, const char *default_val, const char *regex, M_conf_converter_strdup_t converter);
466
467
468/*! Register a key that will have its value stored at the provided address as a signed 8-bit integer.
469 *
470 * \param[in] conf M_conf_t object to use.
471 * \param[in] key Key to register.
472 * \param[out] mem Memory where the value will be stored.
473 * \param[in] default_val Default value to store, if a value is not set in the ini file.
474 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
475 * fail and the default value to be stored.
476 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
477 * to fail and the default value to be stored.
478 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
479 * to the callback, which must do all validation/conversion. The value passed to the callback
480 * can be NULL. Pass NULL if not needed.
481 *
482 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
483 * M_FALSE only if any of the arguments are invalid.
484 */
485M_API M_bool M_conf_register_int8(M_conf_t *conf, const char *key, M_int8 *mem, M_int8 default_val, M_int8 min_val, M_int8 max_val, M_conf_converter_int8_t converter);
486
487
488/*! Register a key that will have its value stored at the provided address as a signed 16-bit integer.
489 *
490 * \param[in] conf M_conf_t object to use.
491 * \param[in] key Key to register.
492 * \param[out] mem Memory where the value will be stored.
493 * \param[in] default_val Default value to store, if a value is not set in the ini file.
494 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
495 * fail and the default value to be stored.
496 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
497 * to fail and the default value to be stored.
498 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
499 * to the callback, which must do all validation/conversion. The value passed to the callback
500 * can be NULL. Pass NULL if not needed.
501 *
502 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
503 * M_FALSE only if any of the arguments are invalid.
504 */
505M_API M_bool M_conf_register_int16(M_conf_t *conf, const char *key, M_int16 *mem, M_int16 default_val, M_int16 min_val, M_int16 max_val, M_conf_converter_int16_t converter);
506
507
508/*! Register a key that will have its value stored at the provided address as a signed 32-bit integer.
509 *
510 * \param[in] conf M_conf_t object to use.
511 * \param[in] key Key to register.
512 * \param[out] mem Memory where the value will be stored.
513 * \param[in] default_val Default value to store, if a value is not set in the ini file.
514 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
515 * fail and the default value to be stored.
516 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
517 * to fail and the default value to be stored.
518 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
519 * to the callback, which must do all validation/conversion. The value passed to the callback
520 * can be NULL. Pass NULL if not needed.
521 *
522 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
523 * M_FALSE only if any of the arguments are invalid.
524 */
525M_API M_bool M_conf_register_int32(M_conf_t *conf, const char *key, M_int32 *mem, M_int32 default_val, M_int32 min_val, M_int32 max_val, M_conf_converter_int32_t converter);
526
527
528/*! Register a key that will have its value stored at the provided address as a signed 64-bit integer.
529 *
530 * \param[in] conf M_conf_t object to use.
531 * \param[in] key Key to register.
532 * \param[out] mem Memory where the value will be stored.
533 * \param[in] default_val Default value to store, if a value is not set in the ini file.
534 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
535 * fail and the default value to be stored.
536 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
537 * to fail and the default value to be stored.
538 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
539 * to the callback, which must do all validation/conversion. The value passed to the callback
540 * can be NULL. Pass NULL if not needed.
541 *
542 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
543 * M_FALSE only if any of the arguments are invalid.
544 */
545M_API M_bool M_conf_register_int64(M_conf_t *conf, const char *key, M_int64 *mem, M_int64 default_val, M_int64 min_val, M_int64 max_val, M_conf_converter_int64_t converter);
546
547
548/*! Register a key that will have its value stored at the provided address as an unsigned 8-bit integer.
549 *
550 * \param[in] conf M_conf_t object to use.
551 * \param[in] key Key to register.
552 * \param[out] mem Memory where the value will be stored.
553 * \param[in] default_val Default value to store, if a value is not set in the ini file.
554 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
555 * fail and the default value to be stored.
556 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
557 * to fail and the default value to be stored.
558 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
559 * to the callback, which must do all validation/conversion. The value passed to the callback
560 * can be NULL. Pass NULL if not needed.
561 *
562 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
563 * M_FALSE only if any of the arguments are invalid.
564 */
565M_API M_bool M_conf_register_uint8(M_conf_t *conf, const char *key, M_uint8 *mem, M_uint8 default_val, M_uint8 min_val, M_uint8 max_val, M_conf_converter_uint8_t converter);
566
567
568/*! Register a key that will have its value stored at the provided address as an unsigned 16-bit integer.
569 *
570 * \param[in] conf M_conf_t object to use.
571 * \param[in] key Key to register.
572 * \param[out] mem Memory where the value will be stored.
573 * \param[in] default_val Default value to store, if a value is not set in the ini file.
574 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
575 * fail and the default value to be stored.
576 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
577 * to fail and the default value to be stored.
578 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
579 * to the callback, which must do all validation/conversion. The value passed to the callback
580 * can be NULL. Pass NULL if not needed.
581 *
582 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
583 * M_FALSE only if any of the arguments are invalid.
584 */
585M_API M_bool M_conf_register_uint16(M_conf_t *conf, const char *key, M_uint16 *mem, M_uint16 default_val, M_uint16 min_val, M_uint16 max_val, M_conf_converter_uint16_t converter);
586
587
588/*! Register a key that will have its value stored at the provided address as an unsigned 32-bit integer.
589 *
590 * \param[in] conf M_conf_t object to use.
591 * \param[in] key Key to register.
592 * \param[out] mem Memory where the value will be stored.
593 * \param[in] default_val Default value to store, if a value is not set in the ini file.
594 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
595 * fail and the default value to be stored.
596 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
597 * to fail and the default value to be stored.
598 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
599 * to the callback, which must do all validation/conversion. The value passed to the callback
600 * can be NULL. Pass NULL if not needed.
601 *
602 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
603 * M_FALSE only if any of the arguments are invalid.
604 */
605M_API M_bool M_conf_register_uint32(M_conf_t *conf, const char *key, M_uint32 *mem, M_uint32 default_val, M_uint32 min_val, M_uint32 max_val, M_conf_converter_uint32_t converter);
606
607
608/*! Register a key that will have its value stored at the provided address as an unsigned 64-bit integer.
609 *
610 * \param[in] conf M_conf_t object to use.
611 * \param[in] key Key to register.
612 * \param[out] mem Memory where the value will be stored.
613 * \param[in] default_val Default value to store, if a value is not set in the ini file.
614 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
615 * fail and the default value to be stored.
616 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
617 * to fail and the default value to be stored.
618 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
619 * to the callback, which must do all validation/conversion. The value passed to the callback
620 * can be NULL. Pass NULL if not needed.
621 *
622 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
623 * M_FALSE only if any of the arguments are invalid.
624 */
625M_API M_bool M_conf_register_uint64(M_conf_t *conf, const char *key, M_uint64 *mem, M_uint64 default_val, M_uint64 min_val, M_uint64 max_val, M_conf_converter_uint64_t converter);
626
627
628/*! Register a key that will have its value stored at the provided address as a size_t integer.
629 *
630 * \param[in] conf M_conf_t object to use.
631 * \param[in] key Key to register.
632 * \param[out] mem Memory where the value will be stored.
633 * \param[in] default_val Default value to store, if a value is not set in the ini file.
634 * \param[in] min_val Minimum allowed value. A value in the ini file less than this will cause M_conf_parse() to
635 * fail and the default value to be stored.
636 * \param[in] max_val Maximum allowed value. A value in the ini file greater than this will cause M_conf_parse()
637 * to fail and the default value to be stored.
638 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
639 * to the callback, which must do all validation/conversion. The value passed to the callback
640 * can be NULL. Pass NULL if not needed.
641 *
642 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
643 * M_FALSE only if any of the arguments are invalid.
644 */
645M_API M_bool M_conf_register_sizet(M_conf_t *conf, const char *key, size_t *mem, size_t default_val, size_t min_val, size_t max_val, M_conf_converter_sizet_t converter);
646
647
648/*! Register a key that will have its value parsed for boolean truthfulness and stored at the provided address.
649 *
650 * \param[in] conf M_conf_t object to use.
651 * \param[in] key Key to register.
652 * \param[out] mem Memory where the value will be stored.
653 * \param[in] default_val Default value to store, if a value is not set in the ini file.
654 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
655 * to the callback, which must do all validation/conversion. The value passed to the callback
656 * can be NULL. Pass NULL if not needed.
657 *
658 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
659 * M_FALSE only if any of the arguments are invalid.
660 */
661M_API M_bool M_conf_register_bool(M_conf_t *conf, const char *key, M_bool *mem, M_bool default_val, M_conf_converter_bool_t converter);
662
663
664/*! Register a key that will have its value manually validated and converted. This passes the value straight to the
665 * converter callback and does not do any validation or conversion internally.
666 *
667 * \param[in] conf M_conf_t object to use.
668 * \param[in] key Key to register.
669 * \param[out] mem Memory where the value will be stored. Pass NULL if not needed.
670 * \param[in] converter Callback for manual conversion. The value will be pulled out of the ini and passed directly
671 * to the callback, which must do all validation/conversion. The value passed to the callback
672 * can be NULL. This must be used.
673 *
674 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently, this returns
675 * M_FALSE only if any of the arguments are invalid.
676 */
677M_API M_bool M_conf_register_custom(M_conf_t *conf, const char *key, void *mem, M_conf_converter_custom_t converter);
678
679
680/*! Register a validation callback. All registered validators are called after M_conf_parse() sets the registered keys.
681 * This can be used, for example, if you want to validate that one key's value is greater than another key's value, or
682 * if you want to print a debug statement for a certain key or keys. This can also be used to run a hook after the
683 * registrations are set.
684 *
685 * \param[in] conf M_conf_t object to use.
686 * \param[in] validator Callback for validating stored values.
687 * \param[in] data Reference for passing in data in the callback. May be NULL if not needed.
688 *
689 * \return M_TRUE if the registration was successful. Otherwise, M_FALSE. Currently,
690 * this returns M_FALSE only if the callback is invalid.
691 */
692M_API M_bool M_conf_register_validator(M_conf_t *conf, M_conf_validator_t validator, void *data);
693
694
695/*! @} */
696
697__END_DECLS
698
699#endif /* __M_CONF_H__ */
M_bool(* M_conf_converter_sizet_t)(size_t *mem, const char *value, size_t default_val)
Definition: m_conf.h:310
M_bool M_conf_register_int32(M_conf_t *conf, const char *key, M_int32 *mem, M_int32 default_val, M_int32 min_val, M_int32 max_val, M_conf_converter_int32_t converter)
M_bool M_conf_parse(M_conf_t *conf)
M_bool(* M_conf_converter_uint16_t)(M_uint16 *mem, const char *value, M_uint16 default_val)
Definition: m_conf.h:280
M_bool(* M_conf_converter_uint64_t)(M_uint64 *mem, const char *value, M_uint64 default_val)
Definition: m_conf.h:300
M_bool M_conf_register_uint8(M_conf_t *conf, const char *key, M_uint8 *mem, M_uint8 default_val, M_uint8 min_val, M_uint8 max_val, M_conf_converter_uint8_t converter)
M_bool(* M_conf_converter_int32_t)(M_int32 *mem, const char *value, M_int32 default_val)
Definition: m_conf.h:250
void(* M_conf_logger_t)(const char *path, const char *msg)
Definition: m_conf.h:199
M_bool M_conf_register_custom(M_conf_t *conf, const char *key, void *mem, M_conf_converter_custom_t converter)
M_list_str_t * M_conf_unused_keys(M_conf_t *conf)
M_bool M_conf_register_validator(M_conf_t *conf, M_conf_validator_t validator, void *data)
M_bool(* M_conf_converter_bool_t)(M_bool *mem, const char *value, M_bool default_val)
Definition: m_conf.h:320
M_bool(* M_conf_converter_int8_t)(M_int8 *mem, const char *value, M_int8 default_val)
Definition: m_conf.h:230
void M_conf_destroy(M_conf_t *conf)
M_bool(* M_conf_validator_t)(void *data)
Definition: m_conf.h:338
M_bool(* M_conf_converter_custom_t)(void *mem, const char *value)
Definition: m_conf.h:330
M_bool M_conf_register_buf(M_conf_t *conf, const char *key, char *buf, size_t buf_len, const char *default_val, const char *regex, M_conf_converter_buf_t converter)
M_bool M_conf_add_error_logger(M_conf_t *conf, M_conf_logger_t error_logger)
M_conf_t * M_conf_create(const char *path, M_bool allow_multiple, char *errbuf, size_t errbuf_len)
M_bool M_conf_register_uint32(M_conf_t *conf, const char *key, M_uint32 *mem, M_uint32 default_val, M_uint32 min_val, M_uint32 max_val, M_conf_converter_uint32_t converter)
M_bool(* M_conf_converter_buf_t)(char *buf, size_t buf_len, const char *value, const char *default_val)
Definition: m_conf.h:210
const char * M_conf_get_value(M_conf_t *conf, const char *key)
M_bool M_conf_register_int64(M_conf_t *conf, const char *key, M_int64 *mem, M_int64 default_val, M_int64 min_val, M_int64 max_val, M_conf_converter_int64_t converter)
M_list_str_t * M_conf_get_sections(M_conf_t *conf)
M_bool M_conf_register_strdup(M_conf_t *conf, const char *key, char **address, const char *default_val, const char *regex, M_conf_converter_strdup_t converter)
struct M_conf_t M_conf_t
Definition: m_conf.h:192
M_bool M_conf_add_debug_logger(M_conf_t *conf, M_conf_logger_t debug_logger)
M_list_str_t * M_conf_get_values(M_conf_t *conf, const char *key)
M_bool M_conf_register_bool(M_conf_t *conf, const char *key, M_bool *mem, M_bool default_val, M_conf_converter_bool_t converter)
M_bool M_conf_register_uint16(M_conf_t *conf, const char *key, M_uint16 *mem, M_uint16 default_val, M_uint16 min_val, M_uint16 max_val, M_conf_converter_uint16_t converter)
M_bool M_conf_register_sizet(M_conf_t *conf, const char *key, size_t *mem, size_t default_val, size_t min_val, size_t max_val, M_conf_converter_sizet_t converter)
M_bool(* M_conf_converter_uint32_t)(M_uint32 *mem, const char *value, M_uint32 default_val)
Definition: m_conf.h:290
M_bool M_conf_register_int16(M_conf_t *conf, const char *key, M_int16 *mem, M_int16 default_val, M_int16 min_val, M_int16 max_val, M_conf_converter_int16_t converter)
M_bool M_conf_register_int8(M_conf_t *conf, const char *key, M_int8 *mem, M_int8 default_val, M_int8 min_val, M_int8 max_val, M_conf_converter_int8_t converter)
M_bool M_conf_register_uint64(M_conf_t *conf, const char *key, M_uint64 *mem, M_uint64 default_val, M_uint64 min_val, M_uint64 max_val, M_conf_converter_uint64_t converter)
M_bool(* M_conf_converter_int16_t)(M_int16 *mem, const char *value, M_int16 default_val)
Definition: m_conf.h:240
M_bool(* M_conf_converter_uint8_t)(M_uint8 *mem, const char *value, M_uint8 default_val)
Definition: m_conf.h:270
M_bool(* M_conf_converter_strdup_t)(char **mem, const char *value, const char *default_val)
Definition: m_conf.h:220
M_bool(* M_conf_converter_int64_t)(M_int64 *mem, const char *value, M_int64 default_val)
Definition: m_conf.h:260
struct M_list_str M_list_str_t
Definition: m_list_str.h:80