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.6 $
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 : Apr 13, 2007
23 **************************************************************************/
25 /*****************************
26 $Log: configuration.cpp,v $
27 Revision 1.6 2007-05-04 14:01:22 arjen
28 Added new arguments to configuration::find_parameter() to allow for a list of
29 sections and a list of parameters in sections.
30 The new arguments are section index and parameter index (default = 0).
32 Revision 1.5 2003/03/29 07:19:37 arjen
33 Bugfix: Do not read the config file in the home
34 directory if the HOME environment variable doesn't exist
36 Revision 1.4 2002/11/23 12:48:18 arjen
37 Check if a configuration file exists before feeding it to the XML parser.
39 Revision 1.3 2002/11/02 14:35:15 arjen
41 BugFix: segfault when finding a parameters that is not in the user-sepcific
44 Revision 1.2 2002/09/28 06:48:46 arjen
45 Bugfix: In configuration::find_parameter(), check if the parameter node exists
46 before retrieving its contents.
48 Revision 1.1 2002/07/25 08:01:26 arjen
49 First checkin, AXE release 0.2
51 *****************************/
53 static const char *RCSID = "$Id: configuration.cpp,v 1.6 2007-05-04 14:01:22 arjen Exp $";
57 #include "configuration.h"
60 /*=========================================================================
61 ** NAME : configuration::read
64 ** RETURN VALUE : true if any config files were found
72 ** LAST MODIFIED : Nov 22, 2002
73 **=========================================================================
76 bool configuration::read(const String name)
82 // Try to read the config files.
84 filename = "/etc/" + name + ".conf";
85 // Check if we can read the file.
87 int fd = open(filename, O_RDONLY);
90 filename = "/usr/local/etc/" + name + ".conf";
91 fd = open(filename, O_RDONLY);
97 system = xmlParseFile(filename);
100 if (getenv("HOME") != NULL)
102 filename = getenv("HOME");
104 filename += name + ".conf";
105 fd = open(filename, O_RDONLY);
109 user = xmlParseFile(filename);
113 // Check the root element, which must be the application name
119 root = xmlDocGetRootElement(system);
120 if (app_name != (const char *)root->name)
122 std::cerr << "Wrong config file.\n";
130 root = xmlDocGetRootElement(user);
131 if (app_name != (const char *)root->name)
133 std::cerr << "Wrong config file.\n";
139 return system != NULL || user != NULL;
142 /*=========================================================================
143 ** NAME : configuration::xmlFindTag
146 ** RETURN VALUE : Pointer to the XML element or NULL
154 ** LAST MODIFIED : Apr 13, 2007
155 **=========================================================================
158 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag, int n)
160 xmlNodePtr element = NULL;
162 while (element == NULL && node != NULL)
164 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
178 /*=========================================================================
179 ** NAME : configuration::find_parameter
182 ** RETURN VALUE : The content of the parameter or an empty string
190 ** LAST MODIFIED : Apr 13, 2007
191 **=========================================================================
194 String configuration::find_parameter(const String section, const String parameter, int sec_n, int param_n)
196 xmlNodePtr root_node;
197 xmlNodePtr section_node;
198 xmlNodePtr param_node;
200 String param_value("");
202 // First, we try to find the parameter in the system-wide config
206 root_node = xmlDocGetRootElement(system);
207 section_node = xmlFindTag(root_node->children, section, sec_n);
208 if (section_node != NULL)
210 param_node = xmlFindTag(section_node->children, parameter, param_n);
211 if (param_node != NULL)
213 param_node = param_node->children;
215 if (param_node != NULL)
217 param_value = (char *)param_node->content;
222 // If the parameter is also defined in the user config, it will override
223 // the system-wide value.
227 root_node = xmlDocGetRootElement(user);
228 section_node = xmlFindTag(root_node->children, section, sec_n);
229 if (section_node != NULL)
231 param_node = xmlFindTag(section_node->children, parameter, param_n);
232 if (param_node != NULL)
234 param_node = param_node->children;
236 if (param_node != NULL)
238 param_value = (char *)param_node->content;