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
communication_xml.cpp
Go to the documentation of this file.
1 #include <vector>
2 #include <string>
3 #include <expat.h>
4 #include "vegastrike.h"
5 #include "communication.h"
6 #include <assert.h>
7 #include "vsfilesystem.h"
8 static int unitlevel;
9 using namespace XMLSupport;
13 namespace CommXML
14 {
15 enum Names
16 {
26 };
27 
29  EnumMap::Pair( "UNKNOWN", UNKNOWN ),
30  EnumMap::Pair( "Node", NODE ),
31  EnumMap::Pair( "Edge", EDGE ),
32  EnumMap::Pair( "Sound", SOUND )
33 };
35  EnumMap::Pair( "UNKNOWN", UNKNOWN ),
36  EnumMap::Pair( "Text", NAME ),
37  EnumMap::Pair( "Index", INDEX ),
38  EnumMap::Pair( "Relationship", VALUE ),
39  EnumMap::Pair( "file", FILENAME ),
40  EnumMap::Pair( "sex", SEXE )
41 };
42 
45 }
46 
47 void FSM::beginElement( void *userData, const XML_Char *names, const XML_Char **atts )
48 {
49  ( (FSM*) userData )->beginElement( names, AttributeList( atts ) );
50 }
51 
52 void FSM::beginElement( const string &name, const AttributeList attributes )
53 {
54  using namespace CommXML;
55  AttributeList::const_iterator iter;
56  Names elem = (Names) element_map.lookup( name );
57  string nam;
58  string filename;
59  float val = 0.0f; //FIXME "= 0.0f" added by chuck_starchaser without knowing what value to use
60  unsigned char sexe = 0; //FIXME "= 0" added by chuck_starchaser without knowing what value to use
61  switch (elem)
62  {
63  case SOUND:
64  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
65  switch ( attribute_map.lookup( (*iter).name ) )
66  {
67  case SEXE:
68  sexe = XMLSupport::parse_int( (*iter).value );
69  break;
70  case FILENAME:
71  filename = (*iter).value;
72  break;
73  }
74  }
75  if (!filename.empty())
76  nodes.back().AddSound( filename, sexe ); //FIXME sexe was used uninitialized until I added = 0 --chuck_starchaser
77  break;
78  case UNKNOWN:
79  unitlevel++;
80  return;
81 
82  case NODE:
83  {
84  unitlevel++;
85  vector< string > messages;
86  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
87  if (strtoupper( (*iter).name ) == "RELATIONSHIP") {
88  val = parse_float( (*iter).value );
89  } else {
90  string tmp = strtoupper( (*iter).name );
91  unsigned int num = 0;
92  if (1 == sscanf( tmp.c_str(), "TEXT%d", &num ) || tmp == "TEXT") {
93  while ( !( num < messages.size() ) )
94  messages.push_back( string() );
95  nam = (*iter).value;
96  {
97  for (string::iterator i = nam.begin(); i != nam.end(); i++)
98  if (*i == '\\')
99  *i = '\n';
100  }
101  messages[num] = nam;
102  }
103  }
104  }
105  nodes.push_back( Node( messages, val ) ); //FIXME val was used uninitialized until I added = 0 --chuck_starchaser
106  break;
107  }
108  case EDGE:
109  unitlevel++;
110  for (iter = attributes.begin(); iter != attributes.end(); iter++)
111  if ( ( attribute_map.lookup( (*iter).name ) ) == INDEX )
112  nodes.back().edges.push_back( parse_int( (*iter).value ) );
113  break;
114  default:
115  break;
116  }
117 }
118 void FSM::endElement( void *userData, const XML_Char *name )
119 {
120  using namespace CommXML;
121  Names elem = (Names) element_map.lookup( name );
122  switch (elem)
123  {
124  case UNKNOWN:
125  unitlevel--;
126  break;
127  default:
128  unitlevel--;
129  break;
130  }
131 }
132 
133 void FSM::LoadXML( const char *filename )
134 {
135  using namespace CommXML;
136  using namespace VSFileSystem;
137  unitlevel = 0;
138  VSFile f;
139  VSError err = f.OpenReadOnly( filename, CommFile );
140  if (err > Ok) {
141  fprintf( stderr, "Failed to open Communications file '%s' -- aborting!\n", filename );
142  assert( 0 );
143  return;
144  }
145  XML_Parser parser = XML_ParserCreate( NULL );
146  XML_SetUserData( parser, this );
147  XML_SetElementHandler( parser, &FSM::beginElement, &FSM::endElement );
148 
149  XML_Parse( parser, ( f.ReadFull() ).c_str(), f.Size(), 1 );
150  f.Close();
151  XML_ParserFree( parser );
152 }
153