6c2b7bc4f150af611a3be743b4d66fbbdd27651d
[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.1 $
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.1  2002-07-25 08:01:26  arjen
28    First checkin, AXE release 0.2
29
30 *****************************/
31
32 static const char *RCSID = "$Id: configuration.cpp,v 1.1 2002-07-25 08:01:26 arjen Exp $";
33
34 #include "configuration.h"
35
36 /*=========================================================================
37 **  NAME           : configuration::read
38 **  SYNOPSIS       :
39 **  PARAMETERS     :
40 **  RETURN VALUE   : true if any config files were found
41 **
42 **  DESCRIPTION    : 
43 **
44 **  VARS USED      :
45 **  VARS CHANGED   :
46 **  FUNCTIONS USED :
47 **  SEE ALSO       :
48 **  LAST MODIFIED  : Jul 24, 2002
49 **=========================================================================
50 */
51
52 bool configuration::read(const String name)
53 {
54    String     filename;
55
56    app_name   = name;
57
58    //   Try to read the config files.
59  
60    filename = "/etc/" + name + ".conf";
61    system = xmlParseFile(filename);
62
63    if (system == NULL)
64    {
65       filename = "/usr/local/etc/" + name + ".conf";
66       system = xmlParseFile(filename);
67    }
68
69    filename = getenv("HOME");
70    filename += "/.";
71    filename += name + ".conf";
72    user = xmlParseFile(filename);
73
74    //  Check the root element, which must be the application name
75
76    xmlNodePtr   root;
77
78    if (system != NULL)
79    {
80       root = xmlDocGetRootElement(system);
81       if (app_name != (char *)root->name)
82       {
83          cerr << "Wrong config file.\n";
84          xmlFreeDoc(system);
85          system = NULL;
86       }
87    }
88
89    if (user != NULL)
90    {
91       root = xmlDocGetRootElement(user);
92       if (app_name != (char *)root->name)
93       {
94          cerr << "Wrong config file.\n";
95          xmlFreeDoc(user);
96          user = NULL;
97       }
98    }
99
100    return system != NULL || user != NULL;
101 }
102
103 /*=========================================================================
104 **  NAME           : configuration::xmlFindTag
105 **  SYNOPSIS       :
106 **  PARAMETERS     :
107 **  RETURN VALUE   : Pointer to the XML element or NULL
108 **
109 **  DESCRIPTION    : 
110 **
111 **  VARS USED      :
112 **  VARS CHANGED   :
113 **  FUNCTIONS USED :
114 **  SEE ALSO       :
115 **  LAST MODIFIED  : Jul 24, 2002
116 **=========================================================================
117 */
118
119 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
120 {
121    xmlNodePtr   element = NULL;
122
123    while (element == NULL && node != NULL)
124    {
125       if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
126       {
127          element = node;
128       }
129       node = node->next;
130    }
131
132    return element;
133 }
134
135 /*=========================================================================
136 **  NAME           : configuration::find_parameter
137 **  SYNOPSIS       :
138 **  PARAMETERS     :
139 **  RETURN VALUE   : The content of the parameter or an empty string
140 **
141 **  DESCRIPTION    : 
142 **
143 **  VARS USED      :
144 **  VARS CHANGED   :
145 **  FUNCTIONS USED :
146 **  SEE ALSO       :
147 **  LAST MODIFIED  : Jul 24, 2002
148 **=========================================================================
149 */
150
151 String configuration::find_parameter(const String section, const String parameter)
152 {
153    xmlNodePtr     root_node;
154    xmlNodePtr     section_node;
155    xmlNodePtr     param_node;
156
157    String         param_value("");
158
159    //  First, we try to find the parameter in the system-wide config
160
161    if (system)
162    {
163       root_node    = xmlDocGetRootElement(system);
164       section_node = xmlFindTag(root_node->childs, section);
165       if (section_node != NULL)
166       {
167          param_node = xmlFindTag(section_node->childs, parameter);
168          param_node = param_node->childs;
169          if (param_node != NULL)
170          {
171             param_value = (char *)param_node->content;
172          }
173       }
174    }
175
176    //  If the parameter is also defined in the user config, it will override
177    //  the system-wide value.
178
179    if (user)
180    {
181       root_node    = xmlDocGetRootElement(user);
182       section_node = xmlFindTag(root_node->childs, section);
183       if (section_node != NULL)
184       {
185          param_node = xmlFindTag(section_node->childs, parameter);
186          param_node = param_node->childs;
187          if (param_node != NULL)
188          {
189             param_value = (char *)param_node->content;
190          }
191       }
192    }
193
194    return param_value;
195 }