***********************
** FILE NAME : configuration.cpp
** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation
-** VERSION NUMBER : $Revision: 1.2 $
+** VERSION NUMBER : $Revision: 1.6 $
**
** DESCRIPTION : Implementation of configuration class
**
********************************
** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
** CREATION DATE : Jul 23, 2002
-** LAST UPDATE : Jul 24, 2002
+** LAST UPDATE : Apr 13, 2007
** MODIFICATIONS :
**************************************************************************/
/*****************************
$Log: configuration.cpp,v $
- Revision 1.2 2002-09-28 06:48:46 arjen
+ Revision 1.6 2007-05-04 14:01:22 arjen
+ Added new arguments to configuration::find_parameter() to allow for a list of
+ sections and a list of parameters in sections.
+ The new arguments are section index and parameter index (default = 0).
+
+ Revision 1.5 2003/03/29 07:19:37 arjen
+ Bugfix: Do not read the config file in the home
+ directory if the HOME environment variable doesn't exist
+
+ Revision 1.4 2002/11/23 12:48:18 arjen
+ Check if a configuration file exists before feeding it to the XML parser.
+
+ Revision 1.3 2002/11/02 14:35:15 arjen
+ Uses the XML2 library
+ BugFix: segfault when finding a parameters that is not in the user-sepcific
+ config tree.
+
+ Revision 1.2 2002/09/28 06:48:46 arjen
Bugfix: In configuration::find_parameter(), check if the parameter node exists
before retrieving its contents.
*****************************/
-static const char *RCSID = "$Id: configuration.cpp,v 1.2 2002-09-28 06:48:46 arjen Exp $";
+static const char *RCSID = "$Id: configuration.cpp,v 1.6 2007-05-04 14:01:22 arjen Exp $";
+#include <fcntl.h>
+#include <unistd.h>
#include "configuration.h"
+
/*=========================================================================
** NAME : configuration::read
** SYNOPSIS :
** VARS CHANGED :
** FUNCTIONS USED :
** SEE ALSO :
-** LAST MODIFIED : Jul 24, 2002
+** LAST MODIFIED : Nov 22, 2002
**=========================================================================
*/
// Try to read the config files.
filename = "/etc/" + name + ".conf";
- system = xmlParseFile(filename);
+ // Check if we can read the file.
- if (system == NULL)
+ int fd = open(filename, O_RDONLY);
+ if (fd < 0)
{
filename = "/usr/local/etc/" + name + ".conf";
+ fd = open(filename, O_RDONLY);
+ }
+
+ if (fd > 0)
+ {
+ close(fd);
system = xmlParseFile(filename);
}
- filename = getenv("HOME");
- filename += "/.";
- filename += name + ".conf";
- user = xmlParseFile(filename);
+ if (getenv("HOME") != NULL)
+ {
+ filename = getenv("HOME");
+ filename += "/.";
+ filename += name + ".conf";
+ fd = open(filename, O_RDONLY);
+ if (fd > 0)
+ {
+ close(fd);
+ user = xmlParseFile(filename);
+ }
+ }
// Check the root element, which must be the application name
** VARS CHANGED :
** FUNCTIONS USED :
** SEE ALSO :
-** LAST MODIFIED : Jul 24, 2002
+** LAST MODIFIED : Apr 13, 2007
**=========================================================================
*/
-xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
+xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag, int n)
{
xmlNodePtr element = NULL;
{
if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
{
- element = node;
+ if (n == 0)
+ {
+ element = node;
+ }
+ n--;
}
node = node->next;
}
** VARS CHANGED :
** FUNCTIONS USED :
** SEE ALSO :
-** LAST MODIFIED : Jul 24, 2002
+** LAST MODIFIED : Apr 13, 2007
**=========================================================================
*/
-String configuration::find_parameter(const String section, const String parameter)
+String configuration::find_parameter(const String section, const String parameter, int sec_n, int param_n)
{
xmlNodePtr root_node;
xmlNodePtr section_node;
if (system)
{
root_node = xmlDocGetRootElement(system);
- section_node = xmlFindTag(root_node->childs, section);
+ section_node = xmlFindTag(root_node->children, section, sec_n);
if (section_node != NULL)
{
- param_node = xmlFindTag(section_node->childs, parameter);
+ param_node = xmlFindTag(section_node->children, parameter, param_n);
if (param_node != NULL)
{
- param_node = param_node->childs;
+ param_node = param_node->children;
}
if (param_node != NULL)
{
if (user)
{
root_node = xmlDocGetRootElement(user);
- section_node = xmlFindTag(root_node->childs, section);
+ section_node = xmlFindTag(root_node->children, section, sec_n);
if (section_node != NULL)
{
- param_node = xmlFindTag(section_node->childs, parameter);
- param_node = param_node->childs;
+ param_node = xmlFindTag(section_node->children, parameter, param_n);
+ if (param_node != NULL)
+ {
+ param_node = param_node->children;
+ }
if (param_node != NULL)
{
param_value = (char *)param_node->content;