vegastrike  0.5.1.r1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
configxml.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * xml Configuration written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 
26 #include <expat.h>
27 #include "xml_support.h"
28 #include <assert.h>
29 #include "configxml.h"
30 #include "easydom.h"
31 
32 using std::cout;
33 using std::cerr;
34 using std::endl;
35 
36 /* *********************************************************** */
37 
38 VegaConfig::VegaConfig( const char *configfile )
39 {
41  configNode *top = (configNode*) domf->LoadXML( configfile );
42  if (top == NULL) {
43  cout<<"Panic exit - no configuration"<<endl;
44  exit( 0 );
45  }
46  variables = NULL;
47  colors = NULL;
48  checkConfig( top );
49 }
50 
52 {
53  if (variables != NULL)
54  delete variables;
55  if (colors != NULL)
56  delete colors;
57  if (bindings != NULL)
58  delete bindings;
59 }
60 
61 /* *********************************************************** */
62 
64 {
65  if (node->Name() != "vegaconfig") {
66  cout<<"this is no Vegastrike config file"<<endl;
67  return false;
68  }
69  vector< easyDomNode* >::const_iterator siter;
70  for (siter = node->subnodes.begin(); siter != node->subnodes.end(); siter++) {
71  configNode *cnode = (configNode*) (*siter);
72  if (cnode->Name() == "variables")
73  doVariables( cnode );
74  else if (cnode->Name() == "colors")
75  doColors( cnode );
76  else if (cnode->Name() == "bindings")
77  bindings = cnode; //delay the bindings until keyboard/joystick is initialized
78  else
79  cout<<"Unknown tag: "<<cnode->Name()<<endl;
80  }
81  return true;
82 }
83 
84 /* *********************************************************** */
85 
87 {
88  if (variables != NULL) {
89  cout<<"only one variable section allowed"<<endl;
90  return;
91  }
92  variables = node;
93 
94  vector< easyDomNode* >::const_iterator siter;
95  for (siter = node->subnodes.begin(); siter != node->subnodes.end(); siter++) {
96  configNode *cnode = (configNode*) (*siter);
97  checkSection( cnode, SECTION_VAR );
98  }
99 }
100 
101 /* *********************************************************** */
102 
103 void VegaConfig::doSection( string prefix, configNode *node, enum section_t section_type )
104 {
105  string section = node->attr_value( "name" );
106  if ( section.empty() )
107  cout<<"no name given for section"<<endl;
108  vector< easyDomNode* >::const_iterator siter;
109  for (siter = node->subnodes.begin(); siter != node->subnodes.end(); siter++) {
110  configNode *cnode = (configNode*) (*siter);
111  if (section_type == SECTION_COLOR) {
112  checkColor( prefix, cnode );
113  } else if (section_type == SECTION_VAR) {
114  if (cnode->Name() == "var")
115  doVar( prefix, cnode );
116  else if (cnode->Name() == "section")
117  doSection( prefix+cnode->attr_value( "name" )+"/", cnode, section_type );
118  else
119  cout<<"neither a variable nor a section"<<endl;
120  }
121  }
122 }
123 
124 /* *********************************************************** */
125 
126 void VegaConfig::checkSection( configNode *node, enum section_t section_type )
127 {
128  if (node->Name() != "section") {
129  cout<<"not a section"<<endl;
130  node->printNode( cout, 0, 1 );
131 
132  return;
133  }
134  doSection( node->attr_value( "name" )+"/", node, section_type );
135 }
136 
137 /* *********************************************************** */
138 
139 void VegaConfig::doVar( string prefix, configNode *node )
140 {
141  string name = node->attr_value( "name" );
142  string value = node->attr_value( "value" );
143  string hashname = prefix+name;
144  map_variables[hashname] = value;
145  if ( name.empty() )
146  cout<<"no name given for variable "<<name<<" "<<value<<" "<<endl;
147 }
148 
149 /* *********************************************************** */
150 
152 {
153  if (node->Name() != "var") {
154  cout<<"not a variable"<<endl;
155  return;
156  }
157  doVar( "", node );
158 }
159 
160 /* *********************************************************** */
161 
162 bool VegaConfig::checkColor( string prefix, configNode *node )
163 {
164  if (node->Name() != "color") {
165  cout<<"no color definition"<<endl;
166  return false;
167  }
168  if ( node->attr_value( "name" ).empty() ) {
169  cout<<"no color name given"<<endl;
170  return false;
171  }
172  string name = node->attr_value( "name" );
173  string hashname = prefix+name;
174 
175  vColor *color;
176  if ( node->attr_value( "ref" ).empty() ) {
177  string r = node->attr_value( "r" );
178  string g = node->attr_value( "g" );
179  string b = node->attr_value( "b" );
180  string a = node->attr_value( "a" );
181  if ( r.empty() || g.empty() || b.empty() || a.empty() ) {
182  cout<<"neither name nor r,g,b given for color "<<node->Name()<<endl;
183  return false;
184  }
185  float rf = atof( r.c_str() );
186  float gf = atof( g.c_str() );
187  float bf = atof( b.c_str() );
188  float af = atof( a.c_str() );
189 
190  vColor &vc = map_colors[hashname];
191  vc.name.erase();
192  vc.r = rf;
193  vc.g = gf;
194  vc.b = bf;
195  vc.a = af;
196 
197  color = new vColor;
198 
199  color->r = rf;
200  color->g = gf;
201  color->b = bf;
202  color->a = af;
203  } else {
204  float refcol[4];
205 
206  string ref_section = node->attr_value( "section" );
207  string ref_name = node->attr_value( "ref" );
208  if ( ref_section.empty() ) {
209  cout<<"you have to give a referenced section when referencing colors"<<endl;
210  ref_section = "default";
211  }
212  getColor( ref_section, ref_name, refcol );
213 
214  vColor &vc = map_colors[hashname];
215  vc.name = ref_section+"/"+ref_name;
216  vc.r = refcol[0];
217  vc.g = refcol[1];
218  vc.b = refcol[2];
219  vc.a = refcol[3];
220 
221  color = new vColor;
222 
223  color->r = refcol[0];
224  color->g = refcol[1];
225  color->b = refcol[2];
226  color->a = refcol[3];
227  }
228  color->name = node->attr_value( "name" );
229 
230  node->color = color;
231 
232  return true;
233 }
234 
235 /* *********************************************************** */
236 
238 {
239  if (colors != NULL) {
240  cout<<"only one variable section allowed"<<endl;
241  return;
242  }
243  colors = node;
244 
245  vector< easyDomNode* >::const_iterator siter;
246  for (siter = node->subnodes.begin(); siter != node->subnodes.end(); siter++) {
247  configNode *cnode = (configNode*) (*siter);
248  checkSection( cnode, SECTION_COLOR );
249  }
250 }
251 
252 /* *********************************************************** */
253 
254 string VegaConfig::getVariable( string section, string subsection, string name, string defaultvalue )
255 {
256  string hashname = section+"/"+subsection+"/"+name;
257  std::map< string, string >::iterator it;
258  if ( ( it = map_variables.find( hashname ) ) != map_variables.end() )
259  return (*it).second;
260 
261  else
262  return defaultvalue;
263 }
264 
265 /* *********************************************************** */
266 
267 string VegaConfig::getVariable( string section, string name, string defaultval )
268 {
269  string hashname = section+"/"+name;
270  std::map< string, string >::iterator it;
271  if ( ( it = map_variables.find( hashname ) ) != map_variables.end() )
272  return (*it).second;
273 
274  else
275  return defaultval;
276 }
277 
278 /* *********************************************************** */
279 
280 string VegaConfig::getVariable( configNode *section, string name, string defaultval )
281 {
282  vector< easyDomNode* >::const_iterator siter;
283  for (siter = section->subnodes.begin(); siter != section->subnodes.end(); siter++) {
284  configNode *cnode = (configNode*) (*siter);
285  if ( (cnode)->attr_value( "name" ) == name )
286  return (cnode)->attr_value( "value" );
287  }
288  static bool foundshouldwarn = false;
289  static bool shouldwarn = true;
290  if (!foundshouldwarn) {
291  if (name != "debug_config") {
292  shouldwarn = XMLSupport::parse_bool( getVariable( "general", "debug_config", "true" ) );
293  foundshouldwarn = true;
294  }
295  }
296  if (shouldwarn)
297  cout<<"WARNING: no var named "<<name<<" in section "<<section->attr_value( "name" )<<" using default: "<<defaultval
298  <<endl;
299  return defaultval;
300 }
301 
302 /* *********************************************************** */
303 
304 void VegaConfig::gethColor( string section, string name, float color[4], int hexcolor )
305 {
306  color[3] = ( (float) (hexcolor&0xff) )/256.0;
307  color[2] = ( (float) ( (hexcolor&0xff00)>>8 ) )/256.0;
308  color[1] = ( (float) ( (hexcolor&0xff0000)>>16 ) )/256.0;
309  color[0] = ( (float) ( (hexcolor&0xff000000)>>24 ) )/256.0;
310 
311  getColor( section, name, color, true );
312 }
313 
314 /* *********************************************************** */
315 
316 void VegaConfig::getColor( string section, string name, float color[4], bool have_color )
317 {
318  string hashname = section+"/"+name;
319  std::map< string, vColor >::iterator it;
320  if ( ( it = map_colors.find( hashname ) ) != map_colors.end() ) {
321  color[0] = (*it).second.r;
322  color[1] = (*it).second.g;
323  color[2] = (*it).second.b;
324  color[3] = (*it).second.a;
325  } else if (!have_color) {
326  color[0] = color[1] = color[2] = color[3] = 1.0f;
327  }
328 }
329 
330 /* *********************************************************** */
331 
332 void VegaConfig::getColor( configNode *node, string name, float color[4], bool have_color )
333 {
334  vector< easyDomNode* >::const_iterator siter;
335  for (siter = node->subnodes.begin(); siter != node->subnodes.end(); siter++) {
336  configNode *cnode = (configNode*) (*siter);
337  if ( (cnode)->attr_value( "name" ) == name ) {
338  color[0] = (cnode)->color->r;
339  color[1] = (cnode)->color->g;
340  color[2] = (cnode)->color->b;
341  color[3] = (cnode)->color->a;
342  return;
343  }
344  }
345  if (have_color == false) {
346  color[0] = 1.0;
347  color[1] = 1.0;
348  color[2] = 1.0;
349  color[3] = 1.0;
350 
351  cout<<"WARNING: color "<<name<<" not defined, using default (white)"<<endl;
352  } else {
353  cout<<"WARNING: color "<<name<<" not defined, using default (hexcolor)"<<endl;
354  }
355 }
356 
357 /* *********************************************************** */
358 
359 configNode* VegaConfig::findEntry( string name, configNode *startnode )
360 {
361  return findSection( name, startnode );
362 }
363 
364 /* *********************************************************** */
365 
366 configNode* VegaConfig::findSection( string section, configNode *startnode )
367 {
368  vector< easyDomNode* >::const_iterator siter;
369  for (siter = startnode->subnodes.begin(); siter != startnode->subnodes.end(); siter++) {
370  configNode *cnode = (configNode*) (*siter);
371  string scan_name = (cnode)->attr_value( "name" );
372  if (scan_name == section)
373  return cnode;
374  }
375  cout<<"WARNING: no section/variable/color named "<<section<<endl;
376 
377  return NULL;
378 }
379 
380 /* *********************************************************** */
381 
382 void VegaConfig::setVariable( configNode *entry, string value )
383 {
384  entry->set_attribute( "value", value );
385 }
386 
387 /* *********************************************************** */
388 
389 bool VegaConfig::setVariable( string section, string name, string value )
390 {
391  configNode *sectionnode = findSection( section, variables );
392  if (sectionnode != NULL) {
393  configNode *varnode = findEntry( name, sectionnode );
394  if (varnode != NULL)
395  //now set the thing
396  setVariable( varnode, value );
397  }
398  string hashname = section+"/"+name;
399  map_variables[hashname] = value;
400  return true;
401 }
402 
403 bool VegaConfig::setVariable( string section, string subsection, string name, string value )
404 {
405  configNode *sectionnode = findSection( section, variables );
406  if (sectionnode != NULL) {
407  configNode *subnode = findSection( name, sectionnode );
408  if (subnode != NULL) {
409  configNode *varnode = findEntry( name, subnode );
410  if (varnode != NULL)
411  //now set the thing
412  setVariable( varnode, value );
413  }
414  }
415  string hashname = section+"/"+subsection+"/"+name;
416  map_variables[hashname] = value;
417  return true;
418 }
419