1 /*
2  * Copyright (c) 2014 Andriy Grytsenko <andrej@rep.kiev.ua>
3  *
4  * This file is a part of LXPanel project.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 /* This is a config file parser with API similar to one used by libconfig
22    for convenience but contents of the file is the own config format
23    therefore it is much more restricted than libconfig is.
24    Strings cannot be numeric and are not quoted (similarly to INI file format).
25    Groups cannot be inside other group but only inside an anonymous list.
26    That anonymous list is the only list type which is supported and there
27    can be only one anonymous member in any group. */
28 
29 #ifndef __CONF_H__
30 #define __CONF_H__ 1
31 
32 #include <glib.h>
33 #include <stdio.h>
34 
35 G_BEGIN_DECLS
36 
37 typedef struct _config_setting_t config_setting_t;
38 typedef struct _PanelConf PanelConf;
39 
40 typedef enum
41 {
42     PANEL_CONF_TYPE_GROUP,
43     PANEL_CONF_TYPE_INT,
44     PANEL_CONF_TYPE_STRING,
45     PANEL_CONF_TYPE_LIST
46 } PanelConfType;
47 
48 typedef void (*PanelConfSaveHook)(const config_setting_t * setting, FILE * f, gpointer user_data);
49 
50 PanelConf *config_new(void);
51 void config_destroy(PanelConf * config);
52 gboolean config_read_file(PanelConf * config, const char * filename);
53 gboolean config_write_file(PanelConf * config, const char * filename);
54 char * config_setting_to_string(const config_setting_t * setting);
55 
56 config_setting_t * config_root_setting(const PanelConf * config);
57 config_setting_t * config_setting_get_member(const config_setting_t * setting, const char * name);
58 config_setting_t * config_setting_get_elem(const config_setting_t * setting, unsigned int index);
59 const char * config_setting_get_name(const config_setting_t * setting);
60 config_setting_t * config_setting_get_parent(const config_setting_t * setting);
61 
62 int config_setting_get_int(const config_setting_t * setting);
63 const char * config_setting_get_string(const config_setting_t * setting);
64 
65 gboolean config_setting_lookup_int(const config_setting_t * setting,
66                                    const char * name, int * value);
67 gboolean config_setting_lookup_string(const config_setting_t * setting,
68                                       const char * name, const char ** value);
69 
70 config_setting_t * config_setting_add(config_setting_t * parent, const char * name, PanelConfType type);
71 
72 gboolean config_setting_move_member(config_setting_t * setting, config_setting_t * parent, const char * name);
73 gboolean config_setting_move_elem(config_setting_t * setting, config_setting_t * parent, int index);
74 
75 gboolean config_setting_set_int(config_setting_t * setting, int value);
76 gboolean config_setting_set_string(config_setting_t * setting, const char * value);
77 gboolean config_setting_remove(config_setting_t * parent, const char * name);
78 gboolean config_setting_remove_elem(config_setting_t * parent, unsigned int index);
79 gboolean config_setting_destroy(config_setting_t * setting);
80 
81 #define config_group_set_int(_group,_name,_value) \
82         config_setting_set_int(config_setting_add(_group, _name, \
83                                                   PANEL_CONF_TYPE_INT), \
84                                _value)
85 #define config_group_set_string(_group,_name,_value) \
86         config_setting_set_string(config_setting_add(_group, _name, \
87                                                      PANEL_CONF_TYPE_STRING), \
88                                _value)
89 #define config_group_add_subgroup(_group,_name) \
90         config_setting_add(config_setting_add(_group, "", PANEL_CONF_TYPE_LIST), \
91                            _name, PANEL_CONF_TYPE_GROUP)
92 
93 PanelConfType config_setting_type(const config_setting_t * setting);
94 #define config_setting_is_group(_s) config_setting_type(_s) == PANEL_CONF_TYPE_GROUP
95 #define config_setting_is_list(_s) config_setting_type(_s) == PANEL_CONF_TYPE_LIST
96 #define config_setting_is_scalar(_s) \
97         (config_setting_type(_s) == PANEL_CONF_TYPE_INT || \
98          config_setting_type(_s) == PANEL_CONF_TYPE_STRING)
99 
100 void config_setting_set_save_hook(config_setting_t * setting, PanelConfSaveHook hook, gpointer user_data);
101 
102 G_END_DECLS
103 
104 #endif /* __CONF_H__ */
105