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.4 $
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 22, 2002
23 **************************************************************************/
25 /*****************************
26 $Log: configuration.cpp,v $
27 Revision 1.4 2002-11-23 12:48:18 arjen
28 Check if a configuration file exists before feeding it to the XML parser.
30 Revision 1.3 2002/11/02 14:35:15 arjen
32 BugFix: segfault when finding a parameters that is not in the user-sepcific
35 Revision 1.2 2002/09/28 06:48:46 arjen
36 Bugfix: In configuration::find_parameter(), check if the parameter node exists
37 before retrieving its contents.
39 Revision 1.1 2002/07/25 08:01:26 arjen
40 First checkin, AXE release 0.2
42 *****************************/
44 static const char *RCSID = "$Id: configuration.cpp,v 1.4 2002-11-23 12:48:18 arjen Exp $";
48 #include "configuration.h"
50 /*=========================================================================
51 ** NAME : configuration::read
54 ** RETURN VALUE : true if any config files were found
62 ** LAST MODIFIED : Nov 22, 2002
63 **=========================================================================
66 bool configuration::read(const String name)
72 // Try to read the config files.
74 filename = "/etc/" + name + ".conf";
75 // Check if we can read the file.
77 int fd = open(filename, O_RDONLY);
80 filename = "/usr/local/etc/" + name + ".conf";
81 fd = open(filename, O_RDONLY);
87 system = xmlParseFile(filename);
90 filename = getenv("HOME");
92 filename += name + ".conf";
93 fd = open(filename, O_RDONLY);
97 user = xmlParseFile(filename);
100 // Check the root element, which must be the application name
106 root = xmlDocGetRootElement(system);
107 if (app_name != (const char *)root->name)
109 std::cerr << "Wrong config file.\n";
117 root = xmlDocGetRootElement(user);
118 if (app_name != (const char *)root->name)
120 std::cerr << "Wrong config file.\n";
126 return system != NULL || user != NULL;
129 /*=========================================================================
130 ** NAME : configuration::xmlFindTag
133 ** RETURN VALUE : Pointer to the XML element or NULL
141 ** LAST MODIFIED : Jul 24, 2002
142 **=========================================================================
145 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
147 xmlNodePtr element = NULL;
149 while (element == NULL && node != NULL)
151 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
161 /*=========================================================================
162 ** NAME : configuration::find_parameter
165 ** RETURN VALUE : The content of the parameter or an empty string
173 ** LAST MODIFIED : Nov 02, 2002
174 **=========================================================================
177 String configuration::find_parameter(const String section, const String parameter)
179 xmlNodePtr root_node;
180 xmlNodePtr section_node;
181 xmlNodePtr param_node;
183 String param_value("");
185 // First, we try to find the parameter in the system-wide config
189 root_node = xmlDocGetRootElement(system);
190 section_node = xmlFindTag(root_node->children, section);
191 if (section_node != NULL)
193 param_node = xmlFindTag(section_node->children, parameter);
194 if (param_node != NULL)
196 param_node = param_node->children;
198 if (param_node != NULL)
200 param_value = (char *)param_node->content;
205 // If the parameter is also defined in the user config, it will override
206 // the system-wide value.
210 root_node = xmlDocGetRootElement(user);
211 section_node = xmlFindTag(root_node->children, section);
212 if (section_node != NULL)
214 param_node = xmlFindTag(section_node->children, parameter);
215 if (param_node != NULL)
217 param_node = param_node->children;
219 if (param_node != NULL)
221 param_value = (char *)param_node->content;