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
criteria_xml.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
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 #include "vegastrike.h"
23 #if defined (_WIN32) && !defined (__CYGWIN__) && !defined (__MINGW32__)
24 //For WIN32 debugging.
25 #include <crtdbg.h>
26 #endif
27 
28 #include <assert.h>
29 #include <set>
30 #include <string>
31 #include <expat.h>
32 
33 #include "criteria.h"
34 #include "xml_support.h"
35 #include "vsfilesystem.h"
36 
38 using namespace VSFileSystem;
39 using std::set;
40 using std::string;
41 
42 void CriteriaContains::beginElement( void *userData, const XML_Char *name, const XML_Char **atts )
43 {
44  AttributeList attributes( atts );
45  if (string( name ) == "Planet") {
46  for (AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); iter++)
47  if ( (*iter).name == "file" )
48  static_cast< set< string >* > (userData)->insert( string( (*iter).value ) );
49  }
50 }
51 
52 void CriteriaContains::endElement( void *userData, const XML_Char *name ) {}
53 
54 std::set< std::string >CriteriaContains::getPlanetTypesFromXML( const char *filename ) const
55 {
56  set< string >textures;
57 
58  VSFile f;
59  VSError err;
60  err = f.OpenReadOnly( string( string( filename )+string( ".system" ) ).c_str(), SystemFile );
61  if (err > Ok) {
62  printf( "CriteriaContains: file not found %s\n", filename );
63  return textures;
64  }
65  XML_Parser parser = XML_ParserCreate( NULL );
66  XML_SetUserData( parser, &textures );
67  XML_SetElementHandler( parser, &CriteriaContains::beginElement, &CriteriaContains::endElement );
68  XML_Parse( parser, ( f.ReadFull() ).c_str(), f.Size(), 1 );
69  f.Close();
70  XML_ParserFree( parser );
71 
72  return textures;
73 }
74