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 $
11 ** DESCRIPTION : Implementation of configuration class
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
23 **************************************************************************/
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.
31 Revision 1.1 2002/07/25 08:01:26 arjen
32 First checkin, AXE release 0.2
34 *****************************/
36 static const char *RCSID = "$Id: configuration.cpp,v 1.2 2002-09-28 06:48:46 arjen Exp $";
38 #include "configuration.h"
40 /*=========================================================================
41 ** NAME : configuration::read
44 ** RETURN VALUE : true if any config files were found
52 ** LAST MODIFIED : Jul 24, 2002
53 **=========================================================================
56 bool configuration::read(const String name)
62 // Try to read the config files.
64 filename = "/etc/" + name + ".conf";
65 system = xmlParseFile(filename);
69 filename = "/usr/local/etc/" + name + ".conf";
70 system = xmlParseFile(filename);
73 filename = getenv("HOME");
75 filename += name + ".conf";
76 user = xmlParseFile(filename);
78 // Check the root element, which must be the application name
84 root = xmlDocGetRootElement(system);
85 if (app_name != (const char *)root->name)
87 std::cerr << "Wrong config file.\n";
95 root = xmlDocGetRootElement(user);
96 if (app_name != (const char *)root->name)
98 std::cerr << "Wrong config file.\n";
104 return system != NULL || user != NULL;
107 /*=========================================================================
108 ** NAME : configuration::xmlFindTag
111 ** RETURN VALUE : Pointer to the XML element or NULL
119 ** LAST MODIFIED : Jul 24, 2002
120 **=========================================================================
123 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
125 xmlNodePtr element = NULL;
127 while (element == NULL && node != NULL)
129 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
139 /*=========================================================================
140 ** NAME : configuration::find_parameter
143 ** RETURN VALUE : The content of the parameter or an empty string
151 ** LAST MODIFIED : Jul 24, 2002
152 **=========================================================================
155 String configuration::find_parameter(const String section, const String parameter)
157 xmlNodePtr root_node;
158 xmlNodePtr section_node;
159 xmlNodePtr param_node;
161 String param_value("");
163 // First, we try to find the parameter in the system-wide config
167 root_node = xmlDocGetRootElement(system);
168 section_node = xmlFindTag(root_node->childs, section);
169 if (section_node != NULL)
171 param_node = xmlFindTag(section_node->childs, parameter);
172 if (param_node != NULL)
174 param_node = param_node->childs;
176 if (param_node != NULL)
178 param_value = (char *)param_node->content;
183 // If the parameter is also defined in the user config, it will override
184 // the system-wide value.
188 root_node = xmlDocGetRootElement(user);
189 section_node = xmlFindTag(root_node->childs, section);
190 if (section_node != NULL)
192 param_node = xmlFindTag(section_node->childs, parameter);
193 param_node = param_node->childs;
194 if (param_node != NULL)
196 param_value = (char *)param_node->content;