My Project
Loading...
Searching...
No Matches
config_file.h
Go to the documentation of this file.
1/*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2015 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
25#ifndef CONFIG_FILE_H
26#define CONFIG_FILE_H
27
28#include <assert.h>
29#include <stdbool.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#ifndef COMPILE_TIME_ASSERT
41#define COMPILE_TIME_ASSERT(name, cond) typedef int assert_ ## name[(cond) * 2 - 1]
42#endif
43
44#ifndef COUNTOF
51#define COUNTOF(a) (sizeof(a) / sizeof(*(a)))
52#endif
53
54/* string type */
55
66typedef union
67{
71 char short_buf[16];
72
78 char *long_buf;
80
82COMPILE_TIME_ASSERT(string_short_buf_sufficient, sizeof(char *) + 1 <= COUNTOF(((ConfigString *)NULL)->short_buf));
86#define CONFIG_STRING_LONG_TAG(s) ((s).short_buf[COUNTOF((s).short_buf) - 1])
95static inline const char *config_string_to_cstr(const ConfigString *string)
96{
97 assert(string != NULL);
98 char is_long = CONFIG_STRING_LONG_TAG(*string);
99 return is_long ?
100 string->long_buf :
101 string->short_buf;
102}
103
104/* config types */
105
109typedef struct
110{
115
123 unsigned int values_count;
124
132 union
133 {
140
145 } v;
147
177
181typedef struct
182{
186 unsigned int sections_count;
187
194} Config;
195
196/* config manipulators */
197
204extern void config_init(Config *config);
205
212extern void config_deinit(Config *config);
213
221extern bool config_parse(Config *config, FILE *file);
222
230extern void config_write(const Config *config, FILE *file);
231
232/* config section accessors/manipulators -- by type, name */
233
235extern ConfigSection *config_add_section_len(Config *config, const char *type, size_t type_len, const char *name, size_t name_len);
236
245static inline ConfigSection *config_add_section(Config *config, const char *type, const char *name)
246{
247 assert(type != NULL);
248 return config_add_section_len(config, type, strlen(type), name, name == NULL ? 0 : strlen(name));
249}
250
251// TODO: extern Config *config_remove_section(Config *config, unsigned int i);
252
262extern ConfigSection *config_find_sections(Config *config, const char *type, ConfigSection **save);
263
272extern ConfigSection *config_find_section(Config *config, const char *type, const char *name);
273
282extern ConfigSection *config_find_or_add_section(Config *config, const char *type, const char *name);
283
284/* config option accessors/manipulators -- by key */
285
287extern ConfigOption *config_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len);
288
298static inline ConfigOption *config_set_option(ConfigSection *section, const char *key, const char *value)
299{
300 assert(key != NULL);
301 return config_set_option_len(section, key, strlen(key), value, value == NULL ? 0 : strlen(value));
302}
303
311extern ConfigOption *config_get_option(const ConfigSection *section, const char *key);
312
314extern ConfigOption *config_get_or_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len);
315
325static inline ConfigOption *config_get_or_set_option(ConfigSection *section, const char *key, const char *value)
326{
327 assert(key != NULL);
328 return config_get_or_set_option_len(section, key, strlen(key), value, value == NULL ? 0 : strlen(value));
329}
330
332extern void config_set_string_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len);
333
342static inline void config_set_string_option(ConfigSection *section, const char *key, const char *value)
343{
344 assert(key != NULL);
345 config_set_string_option_len(section, key, strlen(key), value, value == NULL ? 0 : strlen(value));
346}
347
356extern bool config_get_string_option(const ConfigSection *section, const char *key, const char **out_value);
357
367extern const char *config_get_or_set_string_option(ConfigSection *section, const char *key, const char *value);
368
372typedef enum
373{
374 ZERO_ONE = 0,
375 NO_YES = 1,
376 OFF_ON = 2,
377 FALSE_TRUE = 3,
379
389extern void config_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style);
390
399extern bool config_get_bool_option(const ConfigSection *section, const char *key, bool *out_value);
400
411extern bool config_get_or_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style);
412
421extern void config_set_int_option(ConfigSection *section, const char *key, int value);
422
431extern bool config_get_int_option(const ConfigSection *section, const char *key, int *out_value);
432
442extern int config_get_or_set_int_option(ConfigSection *section, const char *key, int value);
443
453extern void config_set_uint_option(ConfigSection *section, const char *key, unsigned int value);
454
463extern bool config_get_uint_option(const ConfigSection *section, const char *key, unsigned int *out_value);
464
474extern unsigned int config_get_or_set_uint_option(ConfigSection *section, const char *key, unsigned int value);
475
476/* config option accessors/manipulators -- by reference */
477
479extern ConfigOption *config_set_value_len(ConfigOption *option, const char *value, size_t value_len);
480
488static inline ConfigOption *config_set_value(ConfigOption *option, const char *value)
489{
490 return config_set_value_len(option, value, value == NULL ? 0 : strlen(value));
491}
492
494extern ConfigOption *config_add_value_len(ConfigOption *option, const char *value, size_t value_len);
495
503static inline ConfigOption *config_add_value(ConfigOption *option, const char *value)
504{
505 assert(value != NULL);
506 return config_add_value_len(option, value, strlen(value));
507}
508
516extern ConfigOption *config_remove_value(ConfigOption *option, unsigned int i);
517
524extern const char *config_get_value(const ConfigOption *option);
525
532static inline bool config_is_value_list(const ConfigOption *option)
533{
534 assert(option != NULL);
535 return option->values_count > 0 ||
536 config_string_to_cstr(&option->v.value) == NULL;
537}
538
546static inline unsigned int config_get_value_count(const ConfigOption *option)
547{
548 assert(option != NULL);
549 return option->values_count == 0 ?
550 (config_string_to_cstr(&option->v.value) == NULL ? 0 : 1) :
551 option->values_count;
552}
553
560#define foreach_option_value(string_value, option) \
561 for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
562 for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
563 *_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
564 *_value = _values_begin; _value < _values_end; ++_value) \
565 for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)
566
574#define foreach_option_i_value(i, string_value, option) \
575 for (unsigned int (i) = 0; (i) == 0; (i) = ~0) \
576 for (ConfigOption *_option = (option); _option != NULL; _option = NULL) \
577 for (ConfigString *_values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0], \
578 *_values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count], \
579 *_value = _values_begin; _value < _values_end; ++_value, (i) = _value - _values_begin) \
580 for (const char *(string_value) = config_string_to_cstr(_value); (string_value) != NULL; (string_value) = NULL)
581
585#define foreach_remove_option_value() \
586 { \
587 extern void config_oom(void); \
588 unsigned int _value_i = _value - _values_begin; \
589 if (config_remove_value(_option, _value_i) == NULL) \
590 config_oom(); \
591 _values_begin = _option->values_count == 0 ? &_option->v.value : &_option->v.values[0]; \
592 _values_end = _option->values_count == 0 ? _values_begin + 1 : &_option->v.values[_option->values_count]; \
593 _value = _values_begin + _value_i - 1; \
594 }
595
596#endif
ConfigSection * config_add_section_len(Config *config, const char *type, size_t type_len, const char *name, size_t name_len)
Definition config_file.c:149
int config_get_or_set_int_option(ConfigSection *section, const char *key, int value)
Get an integer value of an 'item' option by key, setting the option if it was invalid or creating the...
Definition config_file.c:439
bool config_parse(Config *config, FILE *file)
Parse a configuration from a file.
Definition config_file.c:768
#define COMPILE_TIME_ASSERT(name, cond)
Cause compile error if compile-time computable condition fails.
Definition config_file.h:41
ConfigSection * config_find_sections(Config *config, const char *type, ConfigSection **save)
Iterate sections by type.
Definition config_file.c:168
unsigned int config_get_or_set_uint_option(ConfigSection *section, const char *key, unsigned int value)
Get an unsigned integer value of an 'item' option by key, setting the option if it was invalid or cre...
Definition config_file.c:478
ConfigSection * config_find_section(Config *config, const char *type, const char *name)
Find a section by type and name.
Definition config_file.c:189
void config_init(Config *config)
Initialize a configuration.
Definition config_file.c:98
ConfigOption * config_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:290
ConfigOption * config_set_value_len(ConfigOption *option, const char *value, size_t value_len)
Definition config_file.c:487
void config_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style)
Set a boolean value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:369
void config_deinit(Config *config)
Release any memory allocated inside a configuration.
Definition config_file.c:106
const char * config_get_value(const ConfigOption *option)
Get the value of an 'item' option.
Definition config_file.c:569
ConfigOption * config_get_or_set_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:316
void config_set_uint_option(ConfigSection *section, const char *key, unsigned int value)
Set an unsigned integer value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:446
ConfigSection * config_find_or_add_section(Config *config, const char *type, const char *name)
Find a section by type and name, creating the section if it did not exist.
Definition config_file.c:209
ConfigBoolStyle
The styles of boolean values.
Definition config_file.h:373
void config_write(const Config *config, FILE *file)
Write a configuration to a file.
Definition config_file.c:987
bool config_get_or_set_bool_option(ConfigSection *section, const char *key, bool value, ConfigBoolStyle style)
Get a boolean value of an 'item' option by key, setting the option if it was invalid or creating the ...
Definition config_file.c:400
void config_set_string_option_len(ConfigSection *section, const char *key, size_t key_len, const char *value, size_t value_len)
Definition config_file.c:329
ConfigOption * config_add_value_len(ConfigOption *option, const char *value, size_t value_len)
Definition config_file.c:498
ConfigOption * config_get_option(const ConfigSection *section, const char *key)
Get an option by key.
Definition config_file.c:303
#define COUNTOF(a)
Calculate the number of elements in a fixed-length array.
Definition config_file.h:51
ConfigOption * config_remove_value(ConfigOption *option, unsigned int i)
Remove a value from a 'list' option.
Definition config_file.c:531
const char * config_get_or_set_string_option(ConfigSection *section, const char *key, const char *value)
Get a string value of an 'item' option by key, setting the option if it was invalid or creating the o...
Definition config_file.c:354
bool config_get_int_option(const ConfigSection *section, const char *key, int *out_value)
Get an integer value of an 'item' option by key.
Definition config_file.c:418
bool config_get_bool_option(const ConfigSection *section, const char *key, bool *out_value)
Get a boolean value of an 'item' option by key.
Definition config_file.c:375
bool config_get_string_option(const ConfigSection *section, const char *key, const char **out_value)
Get a string value of an 'item' option by key.
Definition config_file.c:335
bool config_get_uint_option(const ConfigSection *section, const char *key, unsigned int *out_value)
Get an unsigned integer value of an 'item' option by key.
Definition config_file.c:457
void config_set_int_option(ConfigSection *section, const char *key, int value)
Set an integer value of an 'item' option by key, creating the option if necessary.
Definition config_file.c:407
A configuration consisting of sections.
Definition config_file.h:182
unsigned int sections_count
The number of sections in the configuration.
Definition config_file.h:186
ConfigSection * sections
The sections in the configuration.
Definition config_file.h:193
An option consisting of one (an item) or many (a list) values.
Definition config_file.h:110
unsigned int values_count
The number of values in the option if it is a 'list' option.
Definition config_file.h:123
ConfigString * values
The values of a non-empty 'list' option.
Definition config_file.h:144
union ConfigOption::@4 v
The value or values.
ConfigString key
The key of the option.
Definition config_file.h:114
ConfigString value
The value of an 'item' option or an empty 'list' option.
Definition config_file.h:139
A section consisting of options.
Definition config_file.h:152
unsigned int options_count
The number of options in the section.
Definition config_file.h:168
ConfigOption * options
The options in the section.
Definition config_file.h:175
ConfigString type
The type of the section.
Definition config_file.h:156
ConfigString name
The optional name of the section.
Definition config_file.h:163
A short-string-optimizing string type.
Definition config_file.h:67
char * long_buf
The buffer for long strings.
Definition config_file.h:78