Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GalaxyXML::SGalaxy Class Reference

#include <galaxy_xml.h>

Inheritance diagram for GalaxyXML::SGalaxy:
GalaxyXML::Galaxy

Public Member Functions

 SGalaxy ()
 
 SGalaxy (const char *configfile)
 
 SGalaxy (const SGalaxy &g)
 
void writeGalaxy (VSFileSystem::VSFile &f) const
 
void writeSector (VSFileSystem::VSFile &f, int tabs, const string &sectorType, SGalaxy *planet_types) const
 
void processGalaxy (const string &sys)
 
void processSystem (const string &sys, const QVector &suggested_coordinates)
 
 ~SGalaxy ()
 
const string & getVariable (const std::vector< string > &section, const string &name, const string &default_value) const
 
const string & getRandSystem (const string &section, const string &default_value) const
 
const string & getVariable (const string &section, const string &name, const string &defaultvalue) const
 
const string & getVariable (const string &section, const string &subsection, const string &name, const string &defaultvalue) const
 
bool setVariable (const string &section, const string &name, const string &value)
 
bool setVariable (const string &section, const string &subsection, const string &name, const string &value)
 
void addSection (const std::vector< string > &section)
 
void setVariable (const std::vector< string > &section, const string &name, const string &value)
 
SubHeirarchygetHeirarchy ()
 
const std::string & operator[] (const std::string &s) const
 

Protected Member Functions

SGalaxyoperator= (const SGalaxy &a)
 

Protected Attributes

class SubHeirarchysubheirarchy
 
StringMap data
 

Friends

class Galaxy
 

Detailed Description

Definition at line 17 of file galaxy_xml.h.

Constructor & Destructor Documentation

GalaxyXML::SGalaxy::SGalaxy ( )
inline

Definition at line 25 of file galaxy_xml.h.

References subheirarchy.

25  : SGalaxy()
26  {
27  subheirarchy = NULL;
28  }
SGalaxy::SGalaxy ( const char *  configfile)

Definition at line 262 of file galaxy_xml.cpp.

References GalaxyXML::beginElement(), GalaxyXML::endElement(), f, GalaxyXML::XML::g, VSFileSystem::Ok, SERVER, subheirarchy, VSFileSystem::UniverseFile, and x.

263 {
264  using namespace VSFileSystem;
265  subheirarchy = NULL;
266  VSFile f;
267  VSError err = f.OpenReadOnly( configfile, UniverseFile );
268  if (err <= Ok) {
270  x.g = this;
271 
272  XML_Parser parser = XML_ParserCreate( NULL );
273  XML_SetUserData( parser, &x );
274  XML_SetElementHandler( parser, &GalaxyXML::beginElement, &GalaxyXML::endElement );
275  XML_Parse( parser, ( f.ReadFull() ).c_str(), f.Size(), 1 );
276  f.Close();
277 
278  XML_ParserFree( parser );
279  } else if (SERVER) {
280  std::cerr<<"!!! ERROR : couldn't find galaxy file : "<<configfile<<std::endl;
281  exit( 1 );
282  }
283 }
SGalaxy::SGalaxy ( const SGalaxy g)

Definition at line 184 of file galaxy_xml.cpp.

References subheirarchy.

184  : data( g.data )
185 {
186  if (g.subheirarchy)
188  else
189  subheirarchy = NULL;
190 }
SGalaxy::~SGalaxy ( )

Definition at line 164 of file galaxy_xml.cpp.

165 {
166  if (subheirarchy)
167  delete subheirarchy;
168  subheirarchy = NULL;
169 }

Member Function Documentation

void SGalaxy::addSection ( const std::vector< string > &  section)

Definition at line 315 of file galaxy_xml.cpp.

References getHeirarchy(), and i.

Referenced by GalaxyXML::beginElement().

316 {
317  SubHeirarchy *temp = &getHeirarchy();
318  for (unsigned int i = 0; i < section.size(); ++i)
319  temp = &( (*temp)[section[i]].getHeirarchy() );
320 }
SubHeirarchy & SGalaxy::getHeirarchy ( )

Definition at line 285 of file galaxy_xml.cpp.

References subheirarchy.

Referenced by addSection(), StarIter::Done(), NearestSystem(), NumStarsInGalaxy(), setVariable(), and StarIter::StarIter().

286 {
287  if (!subheirarchy)
289  return *subheirarchy;
290 }
const string & SGalaxy::getRandSystem ( const string &  section,
const string &  default_value 
) const

Definition at line 343 of file galaxy_xml.cpp.

References i, size, and subheirarchy.

344 {
345  if (subheirarchy) {
346  const SGalaxy &sector = (*subheirarchy)[sect];
347  if (sector.subheirarchy) {
348  unsigned int size = sector.subheirarchy->size();
349  if (size > 0) {
350  int which = rand()%size;
351  SubHeirarchy::const_iterator i =
352  sector.subheirarchy->begin();
353  while (which > 0)
354  --which, ++i;
355  return (*i).first;
356  }
357  }
358  }
359  return def;
360 }
const string & SGalaxy::getVariable ( const std::vector< string > &  section,
const string &  name,
const string &  default_value 
) const

Definition at line 294 of file galaxy_xml.cpp.

References data, g, i, and subheirarchy.

Referenced by getVarEitherSectionOrSub(), MakeStarSystem(), and processSystem().

296 {
297  const SGalaxy *g = this;
298  for (unsigned int i = 0; i < section.size(); ++i) {
299  if (g->subheirarchy) {
300  SubHeirarchy::const_iterator sub = subheirarchy->find( section[i] );
301  if ( sub != subheirarchy->end() )
302  g = &(*sub).second;
303  else return default_value;
304  } else {return default_value; }}
305  StringMap::const_iterator dat = data.find( name );
306  if ( dat != data.end() )
307  return (*dat).second;
308  return default_value;
309 }
const string & SGalaxy::getVariable ( const string &  section,
const string &  name,
const string &  defaultvalue 
) const

Definition at line 394 of file galaxy_xml.cpp.

References data, g, i, j, and subheirarchy.

395 {
397  SubHeirarchy::const_iterator i;
398  if (s) {
399  i = s->find( section );
400  if ( i != s->end() ) {
401  const SGalaxy *g = &(*i).second;
402  StringMap::const_iterator j = g->data.find( name );
403  if ( j != g->data.end() )
404  return (*j).second;
405  }
406  }
407  return defaultvalue;
408 }
const string & SGalaxy::getVariable ( const string &  section,
const string &  subsection,
const string &  name,
const string &  defaultvalue 
) const

Definition at line 361 of file galaxy_xml.cpp.

References blah, data, g, i, j, processGalaxy(), subheirarchy, and writeGalaxy().

365 {
366 #ifdef WRITEGALAXYCOORDS
367  static bool blah = false;
368  if (!blah) {
369  processGalaxy( "Sol/Sol" );
370  writeGalaxy( "/tmp/outputgalaxy" );
371  blah = true;
372  }
373 #endif
375  SubHeirarchy::const_iterator i;
376  if (s) {
377  i = s->find( section );
378  if ( i != s->end() ) {
379  s = (*i).second.subheirarchy;
380  if (s) {
381  i = s->find( subsection );
382  if ( i != s->end() ) {
383  const SGalaxy *g = &(*i).second;
384  StringMap::const_iterator j = g->data.find( name );
385  if ( j != g->data.end() )
386  return (*j).second;
387  }
388  }
389  }
390  }
391  return defaultvalue;
392 }
SGalaxy & SGalaxy::operator= ( const SGalaxy a)
protected

Definition at line 170 of file galaxy_xml.cpp.

References data, and subheirarchy.

171 {
172  if (g.subheirarchy) {
173  SubHeirarchy *temp = new SubHeirarchy( *g.subheirarchy );
174  if (subheirarchy)
175  delete subheirarchy;
176  subheirarchy = temp;
177  } else if (subheirarchy) {
178  delete subheirarchy;
179  subheirarchy = NULL;
180  }
181  data = g.data;
182  return *this;
183 }
const std::string& GalaxyXML::SGalaxy::operator[] ( const std::string &  s) const
inline

Definition at line 48 of file galaxy_xml.h.

References data.

49  {
50  static std::string empty_string;
51  StringMap::const_iterator it = data.find( s );
52  if ( it != data.end() )
53  return it->second;
54 
55  else
56  return empty_string;
57  }
void SGalaxy::processGalaxy ( const string &  sys)

Definition at line 201 of file galaxy_xml.cpp.

References NavigationSystem::SystemIterator::done(), NavigationSystem::SystemIterator::Position(), and processSystem().

Referenced by getVariable().

202 {
203 #ifdef WRITEGALAXYCOORDS
204  NavigationSystem::SystemIterator si( sys, 256000 );
205  while ( !si.done() ) {
206  string sys = *si;
207  processSystem( sys, si.Position() );
208  ++si;
209  }
210 #endif
211 }
void SGalaxy::processSystem ( const string &  sys,
const QVector suggested_coordinates 
)

Definition at line 191 of file galaxy_xml.cpp.

References getStarSystemName(), getStarSystemSector(), getVariable(), and setVariable().

Referenced by processGalaxy().

192 {
193  string sector = getStarSystemSector( sys );
194  string sys2 = getStarSystemName( sys );
195  char coord[65536];
196  sprintf( coord, "%lf %lf %lf", coords.i, coords.j, coords.k );
197  string ret = getVariable( sector, sys2, "" );
198  if (ret.length() == 0)
199  setVariable( sector, sys2, "xyz", coord );
200 }
bool SGalaxy::setVariable ( const string &  section,
const string &  name,
const string &  value 
)

Definition at line 331 of file galaxy_xml.cpp.

References getHeirarchy().

Referenced by GalaxyXML::beginElement(), and processSystem().

332 {
333  getHeirarchy()[section].data[name] = value;
334  return true;
335 }
bool SGalaxy::setVariable ( const string &  section,
const string &  subsection,
const string &  name,
const string &  value 
)

Definition at line 337 of file galaxy_xml.cpp.

References getHeirarchy().

338 {
339  getHeirarchy()[section].getHeirarchy()[subsection].data[name] = value;
340  return true;
341 }
void SGalaxy::setVariable ( const std::vector< string > &  section,
const string &  name,
const string &  value 
)

Definition at line 321 of file galaxy_xml.cpp.

References data, g, getHeirarchy(), and i.

322 {
323  SGalaxy *g = this;
324  for (unsigned int i = 0; i < section.size(); ++i)
325  g = &g->getHeirarchy()[section[i]];
326  g->data[name] = value;
327 }
void SGalaxy::writeGalaxy ( VSFileSystem::VSFile f) const

Definition at line 241 of file galaxy_xml.cpp.

References VSFileSystem::VSFile::Fprintf(), and writeSector().

Referenced by getVariable().

242 {
243  f.Fprintf( "<galaxy>\n<systems>\n" );
244  writeSector( f, 1, "sector", NULL );
245  f.Fprintf( "</systems>\n" );
246  f.Fprintf( "</galaxy>\n" );
247 }
void SGalaxy::writeSector ( VSFileSystem::VSFile f,
int  tabs,
const string &  sectorType,
SGalaxy planet_types 
) const

Definition at line 217 of file galaxy_xml.cpp.

References data, dotabs(), VSFileSystem::VSFile::Fprintf(), and subheirarchy.

Referenced by writeGalaxy(), and GalaxyXML::Galaxy::writeGalaxy().

218 {
219  StringMap::const_iterator dat;
220  for (dat = data.begin(); dat != data.end(); ++dat)
221  if ( (*dat).first != "jumps" ) {
222  dotabs( f, tabs );
223  f.Fprintf( "<var name=\"%s\" value=\"%s\"/>\n", (*dat).first.c_str(), (*dat).second.c_str() );
224  }
225  dat = data.find( std::string( "jumps" ) );
226  if ( dat != data.end() ) {
227  dotabs( f, tabs );
228  f.Fprintf( "<var name=\"jumps\" value=\"%s\"/>\n", (*dat).second.c_str() );
229  }
230  if (subheirarchy) {
231  for (SubHeirarchy::const_iterator it = subheirarchy->begin(); it != subheirarchy->end(); ++it)
232  if (&(*it).second != planet_types) {
233  dotabs( f, tabs );
234  f.Fprintf( "<%s name=\"%s\">\n", sectorType.c_str(), (*it).first.c_str() );
235  (*it).second.writeSector( f, tabs+1, sectorType, planet_types );
236  dotabs( f, tabs );
237  f.Fprintf( "</%s>\n", sectorType.c_str() );
238  }
239  }
240 }

Friends And Related Function Documentation

friend class Galaxy
friend

Definition at line 20 of file galaxy_xml.h.

Member Data Documentation

StringMap GalaxyXML::SGalaxy::data
protected
class SubHeirarchy* GalaxyXML::SGalaxy::subheirarchy
protected

The documentation for this class was generated from the following files: