summaryrefslogtreecommitdiff
path: root/src/configuration.c
blob: f7cdfbea3ccba89ab95ab6d7dc7c9cce886deb9d (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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * configuration.c - éléments de configuration du programme
 *
 * Copyright (C) 2009 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "configuration.h"


#include <malloc.h>
#include <stdlib.h>
#include <string.h>


#include "xml.h"
#include "common/extstr.h"




/* Paramètres de configuration */
struct _configuration
{
    config_param *params;                   /* Paramètres à consulter      */
    unsigned int count;                     /* Quantité de ces paramètres  */

    char *filename;                         /* Fichier externe             */

    xmlDocPtr xdoc;                         /* Document XML de configurat° */
    xmlXPathContextPtr context;             /* Contexte de recherche XPath */

};





/******************************************************************************
*                                                                             *
*  Paramètres  : name   = désignation de la configuration.                    *
*                params = tableau de paramètres à consulter / mettre à jour.  *
*                count  = taille du tableau en question.                      *
*                                                                             *
*  Description : Charge la configuration principale.                          *
*                                                                             *
*  Retour      : Configuration prête à emploi.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

configuration *load_configuration(const char *name, config_param *params, unsigned int count)
{
    configuration *result;                  /* Structure à retourner       */
    char *home;                             /* Répertoire de l'utilisateur */

    result = (configuration *)calloc(1, sizeof(configuration));

    result->params = params;
    result->count = count;

    /* FIXME : créer le répertoire, et en XDG */
    home = strdup(getenv("HOME") != NULL ? getenv("HOME") : "");
    result->filename = stradd(home, "/.openida/");
    result->filename = stradd(result->filename, name);
    result->filename = stradd(result->filename, ".xml");

    if (!open_xml_file(result->filename, &result->xdoc, &result->context))
        create_new_xml_file(&result->xdoc, &result->context);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : config = configuration à libérer de la mémoire.              *
*                                                                             *
*  Description : Décharge la configuration principale.                        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void unload_configuration(configuration *config)
{
    unsigned int i;                         /* Boucle de parcours          */

    printf("Writing '%s'\n", config->filename);

    for (i = 0; i < config->count; i++)
        switch (1)
        {
            case 1:
                add_content_to_node(config->xdoc, config->context,
                                    config->params[i].path, config->params[i].cur.string);
                break;

        }

    save_xml_file(config->xdoc, config->filename);

    free(config);

}