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
xml_serializer.cpp
Go to the documentation of this file.
1 #include "xml_serializer.h"
2 #include "cmd/unit_generic.h"
3 #include "cmd/images.h"
4 #include "vsfilesystem.h"
5 #include "configxml.h"
6 #include "vs_globals.h"
7 #include "vegastrike.h"
8 #include "networking/const.h"
9 
11 using namespace XMLSupport;
12 using namespace VSFileSystem;
13 
14 std::string intStarHandler( const XMLType &input, void *mythis )
15 {
16  return XMLSupport::tostring( *input.w.i );
17 }
18 std::string uintStarHandler( const XMLType &input, void *mythis )
19 {
20  return XMLSupport::tostring( *input.w.ui );
21 }
22 std::string floatStarHandler( const XMLType &input, void *mythis )
23 {
24  return XMLSupport::tostring( *input.w.f );
25 }
26 std::string fabsFloatStarHandler( const XMLType &input, void *mythis )
27 {
28  return XMLSupport::tostring( (float) fabs( *input.w.f ) );
29 }
30 std::string absIntStarHandler( const XMLType &input, void *mythis )
31 {
32  return XMLSupport::tostring( (int) abs( *input.w.i ) );
33 }
34 std::string scaledFloatStarHandler( const XMLType &input, void *mythis )
35 {
36  return XMLSupport::tostring( (float) ( (*input.w.f)/XMLSupport::parse_float( input.str ) ) );
37 }
38 
39 std::string angleStarHandler( const XMLType &input, void *mythis )
40 {
41  return XMLSupport::tostring( ( float( (*input.w.f)*180/3.1415926536 ) ) );
42 }
43 std::string doubleStarHandler( const XMLType &input, void *mythis )
44 {
45  return XMLSupport::tostring( (float) (*input.w.d) );
46 }
47 std::string boolStarHandler( const XMLType &input, void *mythis )
48 {
49  if (*input.w.b)
50  return "1";
51  return "0";
52 }
53 std::string charStarHandler( const XMLType &input, void *mythis )
54 {
55  return XMLSupport::tostring( *input.w.c );
56 }
57 std::string ucharStarHandler( const XMLType &input, void *mythis )
58 {
59  return XMLSupport::tostring( *input.w.uc );
60 }
61 std::string negationCharStarHandler( const XMLType &input, void *mythis )
62 {
63  return XMLSupport::tostring( -(*input.w.c) );
64 }
65 std::string negationIntStarHandler( const XMLType &input, void *mythis )
66 {
67  return XMLSupport::tostring( -(*input.w.i) );
68 }
69 std::string negationFloatStarHandler( const XMLType &input, void *mythis )
70 {
71  return XMLSupport::tostring( -(*input.w.f) );
72 }
73 std::string stringStarHandler( const XMLType &input, void *mythis )
74 {
75  if (!input.w.p)
76  return string( "" );
77  return *( (string*) (input.w.p) );
78 }
79 std::string stringHandler( const XMLType &input, void *mythis )
80 {
81  return input.str;
82 }
83 std::string intHandler( const XMLType &input, void *mythis )
84 {
85  return XMLSupport::tostring( input.w.hardint );
86 }
87 std::string floatHandler( const XMLType &input, void *mythis )
88 {
89  return XMLSupport::tostring( input.w.hardfloat );
90 }
91 std::string lessNeg1Handler( const XMLType &input, void *mythis )
92 {
93  return XMLSupport::tostring( ( (*input.w.c) < -1 ) ? 1 : 0 );
94 }
95 
96 std::string cloakHandler( const XMLType &input, void *mythis )
97 {
98  return XMLSupport::tostring( ( (*input.w.i) == -1 ) ? 1 : 0 ); //short fix
99 }
100 
101 std::string intToFloatHandler( const XMLType &input, void *mythis )
102 {
103  return XMLSupport::tostring( (float) ( ( (float) (*input.w.i) )/( (float) (2147483647) ) ) );
104 }
105 
106 void XMLElement::Write( VSFileSystem::VSFile &f, void *mythis )
107 {
108  f.Fprintf( " %s=\"%s\"", elem.c_str(), ( (*handler)(value, mythis) ).c_str() );
109 }
110 static void Tab( VSFileSystem::VSFile &f )
111 {
112  f.Fprintf( "\t" );
113 }
114 static void Tab( VSFileSystem::VSFile &f, int level )
115 {
116  for (int i = 0; i < level; i++)
117  Tab( f );
118 }
119 void XMLnode::Write( VSFileSystem::VSFile &f, void *mythis, int level )
120 {
121  Tab( f, level );
122  f.Fprintf( "<%s", val.c_str() );
123  for (unsigned int i = 0; i < elements.size(); i++)
124  elements[i].Write( f, mythis );
125  if ( subnodes.empty() ) {
126  f.Fprintf( "/>\n" );
127  } else {
128  f.Fprintf( ">\n" );
129  for (unsigned int i = 0; i < subnodes.size(); i++)
130  subnodes[i].Write( f, mythis, level+1 );
131  Tab( f, level );
132  f.Fprintf( "</%s>\n", val.c_str() );
133  }
134 }
135 void XMLSerializer::Write( const char *modificationname )
136 {
137  if (modificationname)
138  if (strlen( modificationname ) != 0)
139  savedir = modificationname;
141  VSFile f;
142  VSError err = f.OpenCreateWrite( savedir+"/"+this->filename, UnitFile );
143  if (err > Ok) {
144  fprintf( stderr, "!!! ERROR : Writing saved unit file : %s\n", f.GetFullPath().c_str() );
145  return;
146  }
147  for (unsigned int i = 0; i < topnode.subnodes.size(); i++)
148  topnode.subnodes[i].Write( f, mythis, 0 );
149  f.Close();
150 }
151 
152 static string TabString( int level )
153 {
154  string ret = "";
155  for (int i = 0; i < level; i++)
156  ret += '\t';
157  return ret;
158 }
160 {
161  string ret = "";
162  for (unsigned int i = 0; i < topnode.subnodes.size(); i++)
163  ret += topnode.subnodes[i].WriteString( mythis, 0 );
164  return ret;
165 }
166 string XMLnode::WriteString( void *mythis, int level )
167 {
168  string ret;
169  char buffer[MAXBUFFER];
170  memset( buffer, 0, MAXBUFFER );
171  ret = TabString( level );
172  ret = ret+"<"+val;
173  ret += string( buffer );
174  for (unsigned int i = 0; i < elements.size(); i++)
175  ret += elements[i].WriteString( mythis );
176  if ( subnodes.empty() ) {
177  ret += "/>\n";
178  } else {
179  ret += ">\n";
180  for (unsigned int i = 0; i < subnodes.size(); i++)
181  ret += subnodes[i].WriteString( mythis, level+1 );
182  ret += TabString( level );
183  ret = ret+"</"+val+">\n";
184  }
185  return ret;
186 }
187 string XMLElement::WriteString( void *mythis )
188 {
189  string ret( " "+elem+"=\""+( (*handler)(value, mythis) )+"\"" );
190  return ret;
191 }
192 
193 XMLSerializer::XMLSerializer( const char *filename, const char *modificationname, void *mythis ) : savedir( modificationname )
194  , mythis( mythis )
195 {
196  curnode = &topnode;
197  //In network mode we don't care about saving filename, we want always to save with modification
198  //name since we only work with savegames
199  if (Network != NULL)
200  this->filename = string( modificationname );
201  else
202  this->filename = string( filename );
203 }
204 void XMLSerializer::AddTag( const std::string &tag )
205 {
206  curnode->subnodes.push_back( XMLnode( tag, curnode ) );
207  curnode = &curnode->subnodes.back();
208 }
209 
210 void XMLSerializer::AddElement( const std::string &element, XMLHandler *handler, const XMLType &input )
211 {
212  curnode->elements.push_back( XMLElement( element, input, handler ) );
213 }
214 
215 void XMLSerializer::EndTag( const std::string endname )
216 {
217  if (curnode)
218  if (endname == curnode->val)
219  curnode = curnode->up;
220 }
221