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.5 $
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.5 2003-03-29 07:19:37 arjen
28 Bugfix: Do not read the config file in the home
29 directory if the HOME environment variable doesn't exist
31 Revision 1.4 2002/11/23 12:48:18 arjen
32 Check if a configuration file exists before feeding it to the XML parser.
34 Revision 1.3 2002/11/02 14:35:15 arjen
36 BugFix: segfault when finding a parameters that is not in the user-sepcific
39 Revision 1.2 2002/09/28 06:48:46 arjen
40 Bugfix: In configuration::find_parameter(), check if the parameter node exists
41 before retrieving its contents.
43 Revision 1.1 2002/07/25 08:01:26 arjen
44 First checkin, AXE release 0.2
46 *****************************/
48 static const char *RCSID = "$Id: configuration.cpp,v 1.5 2003-03-29 07:19:37 arjen Exp $";
52 #include "configuration.h"
55 /*=========================================================================
56 ** NAME : configuration::read
59 ** RETURN VALUE : true if any config files were found
67 ** LAST MODIFIED : Nov 22, 2002
68 **=========================================================================
71 bool configuration::read(const String name)
77 // Try to read the config files.
79 filename = "/etc/" + name + ".conf";
80 // Check if we can read the file.
82 int fd = open(filename, O_RDONLY);
85 filename = "/usr/local/etc/" + name + ".conf";
86 fd = open(filename, O_RDONLY);
92 system = xmlParseFile(filename);
95 if (getenv("HOME") != NULL)
97 filename = getenv("HOME");
99 filename += name + ".conf";
100 fd = open(filename, O_RDONLY);
104 user = xmlParseFile(filename);
108 // Check the root element, which must be the application name
114 root = xmlDocGetRootElement(system);
115 if (app_name != (const char *)root->name)
117 std::cerr << "Wrong config file.\n";
125 root = xmlDocGetRootElement(user);
126 if (app_name != (const char *)root->name)
128 std::cerr << "Wrong config file.\n";
134 return system != NULL || user != NULL;
137 /*=========================================================================
138 ** NAME : configuration::xmlFindTag
141 ** RETURN VALUE : Pointer to the XML element or NULL
149 ** LAST MODIFIED : Jul 24, 2002
150 **=========================================================================
153 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
155 xmlNodePtr element = NULL;
157 while (element == NULL && node != NULL)
159 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
169 /*=========================================================================
170 ** NAME : configuration::find_parameter
173 ** RETURN VALUE : The content of the parameter or an empty string
181 ** LAST MODIFIED : Nov 02, 2002
182 **=========================================================================
185 String configuration::find_parameter(const String section, const String parameter)
187 xmlNodePtr root_node;
188 xmlNodePtr section_node;
189 xmlNodePtr param_node;
191 String param_value("");
193 // First, we try to find the parameter in the system-wide config
197 root_node = xmlDocGetRootElement(system);
198 section_node = xmlFindTag(root_node->children, section);
199 if (section_node != NULL)
201 param_node = xmlFindTag(section_node->children, parameter);
202 if (param_node != NULL)
204 param_node = param_node->children;
206 if (param_node != NULL)
208 param_value = (char *)param_node->content;
213 // If the parameter is also defined in the user config, it will override
214 // the system-wide value.
218 root_node = xmlDocGetRootElement(user);
219 section_node = xmlFindTag(root_node->children, section);
220 if (section_node != NULL)
222 param_node = xmlFindTag(section_node->children, parameter);
223 if (param_node != NULL)
225 param_node = param_node->children;
227 if (param_node != NULL)
229 param_value = (char *)param_node->content;