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.1 $
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 : Jul 24, 2002
23 **************************************************************************/
25 /*****************************
26 $Log: configuration.cpp,v $
27 Revision 1.1 2002-07-25 08:01:26 arjen
28 First checkin, AXE release 0.2
30 *****************************/
32 static const char *RCSID = "$Id: configuration.cpp,v 1.1 2002-07-25 08:01:26 arjen Exp $";
34 #include "configuration.h"
36 /*=========================================================================
37 ** NAME : configuration::read
40 ** RETURN VALUE : true if any config files were found
48 ** LAST MODIFIED : Jul 24, 2002
49 **=========================================================================
52 bool configuration::read(const String name)
58 // Try to read the config files.
60 filename = "/etc/" + name + ".conf";
61 system = xmlParseFile(filename);
65 filename = "/usr/local/etc/" + name + ".conf";
66 system = xmlParseFile(filename);
69 filename = getenv("HOME");
71 filename += name + ".conf";
72 user = xmlParseFile(filename);
74 // Check the root element, which must be the application name
80 root = xmlDocGetRootElement(system);
81 if (app_name != (char *)root->name)
83 cerr << "Wrong config file.\n";
91 root = xmlDocGetRootElement(user);
92 if (app_name != (char *)root->name)
94 cerr << "Wrong config file.\n";
100 return system != NULL || user != NULL;
103 /*=========================================================================
104 ** NAME : configuration::xmlFindTag
107 ** RETURN VALUE : Pointer to the XML element or NULL
115 ** LAST MODIFIED : Jul 24, 2002
116 **=========================================================================
119 xmlNodePtr configuration::xmlFindTag(xmlNodePtr node, const String tag)
121 xmlNodePtr element = NULL;
123 while (element == NULL && node != NULL)
125 if (node->type == XML_ELEMENT_NODE && tag == (char *)node->name)
135 /*=========================================================================
136 ** NAME : configuration::find_parameter
139 ** RETURN VALUE : The content of the parameter or an empty string
147 ** LAST MODIFIED : Jul 24, 2002
148 **=========================================================================
151 String configuration::find_parameter(const String section, const String parameter)
153 xmlNodePtr root_node;
154 xmlNodePtr section_node;
155 xmlNodePtr param_node;
157 String param_value("");
159 // First, we try to find the parameter in the system-wide config
163 root_node = xmlDocGetRootElement(system);
164 section_node = xmlFindTag(root_node->childs, section);
165 if (section_node != NULL)
167 param_node = xmlFindTag(section_node->childs, parameter);
168 param_node = param_node->childs;
169 if (param_node != NULL)
171 param_value = (char *)param_node->content;
176 // If the parameter is also defined in the user config, it will override
177 // the system-wide value.
181 root_node = xmlDocGetRootElement(user);
182 section_node = xmlFindTag(root_node->childs, section);
183 if (section_node != NULL)
185 param_node = xmlFindTag(section_node->childs, parameter);
186 param_node = param_node->childs;
187 if (param_node != NULL)
189 param_value = (char *)param_node->content;