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.3 $
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 : Nov 02, 2002
23 **************************************************************************/
25 /*****************************
26 $Log: configuration.cpp,v $
27 Revision 1.3 2002-11-02 14:35:15 arjen
29 BugFix: segfault when finding a parameters that is not in the user-sepcific
32 Revision 1.2 2002/09/28 06:48:46 arjen
33 Bugfix: In configuration::find_parameter(), check if the parameter node exists
34 before retrieving its contents.
36 Revision 1.1 2002/07/25 08:01:26 arjen
37 First checkin, AXE release 0.2
39 *****************************/
41 static const char *RCSID = "$Id: configuration.cpp,v 1.3 2002-11-02 14:35:15 arjen Exp $";
43 #include "configuration.h"
45 /*=========================================================================
46 ** NAME : configuration::read
49 ** RETURN VALUE : true if any config files were found
57 ** LAST MODIFIED : Jul 24, 2002
58 **=========================================================================
61 bool configuration::read(const String name)
67 // Try to read the config files.
69 filename = "/etc/" + name + ".conf";
70 system = xmlParseFile(filename);
74 filename = "/usr/local/etc/" + name + ".conf";
75 system = xmlParseFile(filename);
78 filename = getenv("HOME");
80 filename += name + ".conf";
81 user = xmlParseFile(filename);
83 // Check the root element, which must be the application name
89 root = xmlDocGetRootElement(system);
90 if (app_name != (const char *)root->name)
92 std::cerr << "Wrong config file.\n";
100 root = xmlDocGetRootElement(user);
101 if (app_name != (const char *)root->name)
103 std::cerr << "Wrong config file.\n";
109 return system != NULL || user != NULL;
112 /*=========================================================================
113 ** NAME : configuration::xmlFindTag
116 ** RETURN VALUE : Pointer to the XML element or NULL
124 ** LAST MODIFIED : Jul 24, 2002
125 **=========================================================================
128 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
130 xmlNodePtr element = NULL;
132 while (element == NULL && node != NULL)
134 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
144 /*=========================================================================
145 ** NAME : configuration::find_parameter
148 ** RETURN VALUE : The content of the parameter or an empty string
156 ** LAST MODIFIED : Nov 02, 2002
157 **=========================================================================
160 String configuration::find_parameter(const String section, const String parameter)
162 xmlNodePtr root_node;
163 xmlNodePtr section_node;
164 xmlNodePtr param_node;
166 String param_value("");
168 // First, we try to find the parameter in the system-wide config
172 root_node = xmlDocGetRootElement(system);
173 section_node = xmlFindTag(root_node->children, section);
174 if (section_node != NULL)
176 param_node = xmlFindTag(section_node->children, parameter);
177 if (param_node != NULL)
179 param_node = param_node->children;
181 if (param_node != NULL)
183 param_value = (char *)param_node->content;
188 // If the parameter is also defined in the user config, it will override
189 // the system-wide value.
193 root_node = xmlDocGetRootElement(user);
194 section_node = xmlFindTag(root_node->children, section);
195 if (section_node != NULL)
197 param_node = xmlFindTag(section_node->children, parameter);
198 if (param_node != NULL)
200 param_node = param_node->children;
202 if (param_node != NULL)
204 param_value = (char *)param_node->content;