77726210b8a824a8ec7c0866e9e49b3409433d6a
[AXE.git] / src / configuration.cpp
1
2 /**************************************************************************
3 **  (c) Copyright 2002, Andromeda Technology & Automation
4 ***************************************************************************
5 ** MODULE INFORMATION *
6 ***********************
7 **      FILE NAME      : configuration.cpp
8 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
9 **      VERSION NUMBER : $Revision: 1.2 $
10 **
11 **  DESCRIPTION      :  Implementation of configuration class
12 **
13 **  EXPORTED OBJECTS : 
14 **  LOCAL    OBJECTS : 
15 **  MODULES  USED    :
16 ***************************************************************************
17 **  ADMINISTRATIVE INFORMATION *
18 ********************************
19 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
20 **      CREATION DATE   : Jul 23, 2002
21 **      LAST UPDATE     : Jul 24, 2002
22 **      MODIFICATIONS   : 
23 **************************************************************************/
24
25 /*****************************
26    $Log: configuration.cpp,v $
27    Revision 1.2  2002-09-28 06:48:46  arjen
28    Bugfix: In configuration::find_parameter(), check if the parameter node exists
29    before retrieving its contents.
30
31    Revision 1.1  2002/07/25 08:01:26  arjen
32    First checkin, AXE release 0.2
33
34 *****************************/
35
36 static const char *RCSID = "$Id: configuration.cpp,v 1.2 2002-09-28 06:48:46 arjen Exp $";
37
38 #include "configuration.h"
39
40 /*=========================================================================
41 **  NAME           : configuration::read
42 **  SYNOPSIS       :
43 **  PARAMETERS     :
44 **  RETURN VALUE   : true if any config files were found
45 **
46 **  DESCRIPTION    : 
47 **
48 **  VARS USED      :
49 **  VARS CHANGED   :
50 **  FUNCTIONS USED :
51 **  SEE ALSO       :
52 **  LAST MODIFIED  : Jul 24, 2002
53 **=========================================================================
54 */
55
56 bool configuration::read(const String name)
57 {
58    String     filename;
59
60    app_name   = name;
61
62    //   Try to read the config files.
63  
64    filename = "/etc/" + name + ".conf";
65    system = xmlParseFile(filename);
66
67    if (system == NULL)
68    {
69       filename = "/usr/local/etc/" + name + ".conf";
70       system = xmlParseFile(filename);
71    }
72
73    filename = getenv("HOME");
74    filename += "/.";
75    filename += name + ".conf";
76    user = xmlParseFile(filename);
77
78    //  Check the root element, which must be the application name
79
80    xmlNodePtr   root;
81
82    if (system != NULL)
83    {
84       root = xmlDocGetRootElement(system);
85       if (app_name != (const char *)root->name)
86       {
87          std::cerr << "Wrong config file.\n";
88          xmlFreeDoc(system);
89          system = NULL;
90       }
91    }
92
93    if (user != NULL)
94    {
95       root = xmlDocGetRootElement(user);
96       if (app_name != (const char *)root->name)
97       {
98          std::cerr << "Wrong config file.\n";
99          xmlFreeDoc(user);
100          user = NULL;
101       }
102    }
103
104    return system != NULL || user != NULL;
105 }
106
107 /*=========================================================================
108 **  NAME           : configuration::xmlFindTag
109 **  SYNOPSIS       :
110 **  PARAMETERS     :
111 **  RETURN VALUE   : Pointer to the XML element or NULL
112 **
113 **  DESCRIPTION    : 
114 **
115 **  VARS USED      :
116 **  VARS CHANGED   :
117 **  FUNCTIONS USED :
118 **  SEE ALSO       :
119 **  LAST MODIFIED  : Jul 24, 2002
120 **=========================================================================
121 */
122
123 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
124 {
125    xmlNodePtr   element = NULL;
126
127    while (element == NULL && node != NULL)
128    {
129       if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
130       {
131          element = node;
132       }
133       node = node->next;
134    }
135
136    return element;
137 }
138
139 /*=========================================================================
140 **  NAME           : configuration::find_parameter
141 **  SYNOPSIS       :
142 **  PARAMETERS     :
143 **  RETURN VALUE   : The content of the parameter or an empty string
144 **
145 **  DESCRIPTION    : 
146 **
147 **  VARS USED      :
148 **  VARS CHANGED   :
149 **  FUNCTIONS USED :
150 **  SEE ALSO       :
151 **  LAST MODIFIED  : Jul 24, 2002
152 **=========================================================================
153 */
154
155 String configuration::find_parameter(const String section, const String parameter)
156 {
157    xmlNodePtr     root_node;
158    xmlNodePtr     section_node;
159    xmlNodePtr     param_node;
160
161    String         param_value("");
162
163    //  First, we try to find the parameter in the system-wide config
164
165    if (system)
166    {
167       root_node    = xmlDocGetRootElement(system);
168       section_node = xmlFindTag(root_node->childs, section);
169       if (section_node != NULL)
170       {
171          param_node = xmlFindTag(section_node->childs, parameter);
172          if (param_node != NULL)
173          {
174             param_node = param_node->childs;
175          }
176          if (param_node != NULL)
177          {
178             param_value = (char *)param_node->content;
179          }
180       }
181    }
182
183    //  If the parameter is also defined in the user config, it will override
184    //  the system-wide value.
185
186    if (user)
187    {
188       root_node    = xmlDocGetRootElement(user);
189       section_node = xmlFindTag(root_node->childs, section);
190       if (section_node != NULL)
191       {
192          param_node = xmlFindTag(section_node->childs, parameter);
193          param_node = param_node->childs;
194          if (param_node != NULL)
195          {
196             param_value = (char *)param_node->content;
197          }
198       }
199    }
200
201    return param_value;
202 }