c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef SETTINGS_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define SETTINGS_H
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainenenum setting_type {
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen SET_STR,
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen SET_INT,
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen SET_BOOL
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen};
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainenstruct setting_def {
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen enum setting_type type;
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen const char *name;
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen size_t offset;
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen};
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen#define DEF_STRUCT_STR(name, struct_name) \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen { SET_STR + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen ((struct struct_name *)0)->name, const char *), \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen #name, offsetof(struct struct_name, name) }
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen#define DEF_STRUCT_INT(name, struct_name) \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen { SET_INT + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen ((struct struct_name *)0)->name, unsigned int), \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen #name, offsetof(struct struct_name, name) }
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen#define DEF_STRUCT_BOOL(name, struct_name) \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen { SET_BOOL + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen ((struct struct_name *)0)->name, bool), \
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen #name, offsetof(struct struct_name, name) }
a84eb0599fa1d796206eaed65c4e3239f0799276Timo Sirainen
0cb57ee35d4cab9c03434d7abf312c081ed554d4Timo Sirainen/* Return error message. When closing section, key = NULL, value = NULL. */
837f363f04402adf2f8081e6a6cb734e77a0520dTimo Sirainentypedef const char *settings_callback_t(const char *key, const char *value,
837f363f04402adf2f8081e6a6cb734e77a0520dTimo Sirainen void *context);
837f363f04402adf2f8081e6a6cb734e77a0520dTimo Sirainen
0cb57ee35d4cab9c03434d7abf312c081ed554d4Timo Sirainen/* Return TRUE if we want to go inside the section */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainentypedef bool settings_section_callback_t(const char *type, const char *name,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen void *context, const char **errormsg);
0cb57ee35d4cab9c03434d7abf312c081ed554d4Timo Sirainen
59151b71059df1190acd75d8717ed04a7920c862Timo Sirainenextern settings_section_callback_t *null_settings_section_callback;
59151b71059df1190acd75d8717ed04a7920c862Timo Sirainen
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainenconst char *
7744586e3e0fd60158abfbb03a233d3bd8d6c48bTimo Sirainenparse_setting_from_defs(pool_t pool, const struct setting_def *defs, void *base,
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen const char *key, const char *value);
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainenbool settings_read_i(const char *path, const char *section,
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen settings_callback_t *callback,
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen settings_section_callback_t *sect_callback, void *context,
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen const char **error_r)
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen ATTR_NULL(2, 4, 5);
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen#define settings_read(path, section, callback, sect_callback, context, error_r) \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen settings_read_i(path + \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen CALLBACK_TYPECHECK(callback, const char *(*)( \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen const char *, const char *, typeof(context))) + \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen CALLBACK_TYPECHECK(sect_callback, bool (*)( \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen const char *, const char *, typeof(context), \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen const char **)), \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen section, (settings_callback_t *)callback, \
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen (settings_section_callback_t *)sect_callback, context, error_r)
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen#define settings_read_nosection(path, callback, context, error_r) \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen settings_read_i(path + \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen CALLBACK_TYPECHECK(callback, const char *(*)( \
27a44fcfd8d19bffe0f267f20a2b5d3fe7600fddTimo Sirainen const char *, const char *, typeof(context))), \
803197abb1cc0e81abb668c026c22394bfef820dTimo Sirainen NULL, (settings_callback_t *)callback, NULL, context, error_r)
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen
9e49712b9cddbf47568ea4f3676e59b151489356Timo Sirainen#endif