summaryrefslogtreecommitdiff
path: root/src/glibext/configuration.h
blob: afefed19d7c3d41930ce562caecf25471a7b5750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

/* Chrysalide - Outil d'analyse de fichiers binaires
 * configuration.h - prototypes pour les éléments de configuration du programme
 *
 * Copyright (C) 2014-2019 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef _GLIBEXT_CONFIGURATION_H
#define _GLIBEXT_CONFIGURATION_H


#include <glib-object.h>
#include <stdbool.h>
#include <gdk/gdk.h>



/* ---------------------------- ELEMENT DE CONFIGURATION ---------------------------- */


/* Types de valeur pour élement de configuration */
typedef enum _ConfigParamType
{
    CPT_BOOLEAN,                            /* Valeur booléenne            */
    CPT_INTEGER,                            /* Valeur entière              */
    CPT_ULONG,                              /* Valeur entière positive     */
    CPT_STRING,                             /* Chaîne de caractère         */
    CPT_COLOR,                              /* Couleur avec transparence   */

    CPT_COUNT

} ConfigParamType;


/* Liste dess états d'un paramètre de configuration */
typedef enum _ConfigParamState
{
    CPS_UNDEFINED = (0 << 0),               /* Etat non déterminé          */
    CPS_CHANGED   = (1 << 0),               /* Modification utilisateur    */
    CPS_DEFAULT   = (1 << 1),               /* Valeur par défaut           */
    CPS_EMPTY     = (1 << 2)                /* Valeur vide                 */

} ConfigParamState;


#define G_TYPE_CFG_PARAM                (g_config_param_get_type())
#define G_CFG_PARAM(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_CFG_PARAM, GCfgParam))
#define G_IS_CFG_PARAM(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_CFG_PARAM))
#define G_CFG_PARAM_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_CFG_PARAM, GCfgParamClass))
#define G_IS_CFG_PARAM_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_CFG_PARAM))
#define G_CFG_PARAM_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_CFG_PARAM, GCfgParamClass))


/* Configuration générique quelconque (instance) */
typedef struct _GCfgParam GCfgParam;

/* Configuration générique quelconque (classe) */
typedef struct _GCfgParamClass GCfgParamClass;


/* Indique le type défini par la GLib pour les configurations génériques. */
GType g_config_param_get_type(void);

/* Crée un paramètre de configuration. */
GCfgParam *g_config_param_new(const char *, ConfigParamType, ...);

/* Crée un paramètre de configuration sans valeur. */
GCfgParam *g_config_param_new_empty(const char *, ConfigParamType);

/* Etablit une comparaison selon un chemin d'accès. */
gint g_config_param_compare(const GCfgParam *, const GCfgParam *);

/* Indique le chemin d'accès utilisé pour un paramètre. */
const char *g_config_param_get_path(const GCfgParam *);

/* Indique le type de valeur utilisée par un paramètre. */
ConfigParamType g_config_param_get_ptype(const GCfgParam *);

/* Indique le statut d'une valeur utilisée par un paramètre. */
ConfigParamState g_config_param_get_state(GCfgParam *);

/* Efface toute valeur courante d'un paramètre de configuration. */
void g_config_param_make_empty(GCfgParam *);

/* Réinitialise la valeur d'un paramètre de configuration. */
void g_config_param_reset(GCfgParam *);

/* Modifie la valeur courante d'un paramètre de configuration. */
void g_config_param_set_value(GCfgParam *, ...);

/* Indique la valeur courante d'un paramètre de configuration. */
void g_config_param_get_value(GCfgParam *, ...);



/* --------------------- PARTIES IMPREVISIBLES DE CONFIGURATION --------------------- */


#define G_TYPE_CFG_GROUP                (g_config_group_get_type())
#define G_CFG_GROUP(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_CFG_GROUP, GCfgGroup))
#define G_IS_CFG_GROUP(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_CFG_GROUP))
#define G_CFG_GROUP_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_CFG_GROUP, GCfgGroupClass))
#define G_IS_CFG_GROUP_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_CFG_GROUP))
#define G_CFG_GROUP_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_CFG_GROUP, GCfgGroupClass))


/* Groupe de paramètres non fixés à l'avance (instance) */
typedef struct _GCfgGroup GCfgGroup;

/* Groupe de paramètres non fixés à l'avance (classe) */
typedef struct _GCfgGroupClass GCfgGroupClass;


/* Indique le type défini par la GLib pour les groupes de paramètres non prévisibiles. */
GType g_config_group_get_type(void);

/* Crée un groupe de paramètres de configuration. */
GCfgGroup *g_config_group_new(const char *, ConfigParamType);



/* ----------------------- GESTION GENERIQUE DE CONFIGURATION ----------------------- */


#define G_TYPE_GEN_CONFIG                (g_generic_config_get_type())
#define G_GEN_CONFIG(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_GEN_CONFIG, GGenConfig))
#define G_IS_GEN_CONFIG(obj)             (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_GEN_CONFIG))
#define G_GEN_CONFIG_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_GEN_CONFIG, GGenConfigClass))
#define G_IS_GEN_CONFIG_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_GEN_CONFIG))
#define G_GEN_CONFIG_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_GEN_CONFIG, GGenConfigClass))


/* Configuration générique quelconque (instance) */
typedef struct _GGenConfig GGenConfig;

/* Configuration générique quelconque (classe) */
typedef struct _GGenConfigClass GGenConfigClass;


/* Indique le type défini par la GLib pour les configurations génériques. */
GType g_generic_config_get_type(void);

/* Crée un gestionnaire configuration générique. */
GGenConfig *g_generic_config_new(void);

/* Crée un gestionnaire configuration générique. */
GGenConfig *g_generic_config_new_from_file(const char *);

/* Indique le fichier utilisé pour l'enregistrement XML. */
const char *g_generic_config_get_filename(const GGenConfig *);

/* Met à disposition un encadrement des accès aux paramètres. */
void g_generic_config_lock_unlock(GGenConfig *, bool, bool);


#define g_generic_config_wlock(cfg) g_generic_config_lock_unlock(cfg, true, true);
#define g_generic_config_wunlock(cfg) g_generic_config_lock_unlock(cfg, true, false);

#define g_generic_config_rlock(cfg) g_generic_config_lock_unlock(cfg, false, true);
#define g_generic_config_runlock(cfg) g_generic_config_lock_unlock(cfg, false, false);


/* Lit la configuration depuis un fichier. */
bool g_generic_config_read(GGenConfig *);

/* Ecrit la configuration dans un fichier. */
bool g_generic_config_write(GGenConfig *);

/* Ajoute la définition d'un groupe à une configuration. */
void g_generic_config_add_group(GGenConfig *, GCfgGroup *);


#define g_generic_config_create_group(c, p, t)                  \
    ({                                                          \
        GCfgGroup *__group;                                     \
        __group = g_config_group_new(p, t);                     \
        g_generic_config_add_group(c, __group);                 \
    })


/* Retrouve un élément de configuration par son chemin. */
GCfgParam *_g_generic_config_search(GGenConfig *, const char *, bool);


#define g_generic_config_search(cfg, p) _g_generic_config_search(cfg, p, true)

#define g_generic_config_set_value(c, p, v)                     \
    ({                                                          \
        GCfgParam *__param;                                     \
        __param = g_generic_config_search(c, p);                \
        if (__param != NULL)                                    \
            g_config_param_set_value(__param, v);               \
        __param != NULL ? true : false;                         \
    })

#define g_generic_config_get_value(c, p, v)                     \
    ({                                                          \
        GCfgParam *__param;                                     \
        __param = g_generic_config_search(c, p);                \
        if (__param != NULL)                                    \
            g_config_param_get_value(__param, v);               \
        __param != NULL ? true : false;                         \
    })


/* Ajoute un paramètre à une configuration. */
GCfgParam *_g_generic_config_add_param(GGenConfig *, GCfgParam *, bool);


#define g_generic_config_add_param(c, p) _g_generic_config_add_param(c, p, true)

#define g_generic_config_create_param(c, p, t, d)               \
    ({                                                          \
        GCfgParam *__result;                                    \
        __result = g_config_param_new(p, t, d);                 \
        __result = g_generic_config_add_param(c, __result);     \
        __result;                                               \
    })


#define g_generic_config_create_param_if_not_exist(c, p, t, d)  \
    ({                                                          \
        GCfgParam *__result;                                    \
        __result = g_generic_config_search(c, p);               \
        if (__result == NULL)                                   \
        {                                                       \
            __result = g_config_param_new(p, t, d);             \
            __result = g_generic_config_add_param(c, __result); \
        }                                                       \
        __result;                                               \
    })


#define g_generic_config_create_or_udpdate_param(c, p, t, d, v) \
    ({                                                          \
        GCfgParam *__param;                                     \
        __param = g_generic_config_search(c, p);                \
        if (__param == NULL)                                    \
        {                                                       \
            __param = g_config_param_new(p, t, d);              \
            __param = g_generic_config_add_param(c, __param);   \
        }                                                       \
        if (__param != NULL)                                    \
            g_config_param_set_value(__param, v);               \
        __param != NULL ? true : false;                         \
    })


/* Retire un paramètre d'une configuration. */
void g_generic_config_delete_param(GGenConfig *, const char *);

/* Renvoie la liste des paramètres de configuration. */
GList *g_generic_config_list_params(const GGenConfig *);



#endif  /* _GLIBEXT_CONFIGURATION_H */