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
accountsxml.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  * account_info Managment - Network Client account_info Managment - written by Stephane Vaxelaire <svax@free.fr>
24  */
25 #include <iostream>
26 #include <fstream>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include "const.h"
30 #include "xml_support.h"
31 #include "hashtable.h"
32 #include "accountsxml.h"
33 #include "vsfilesystem.h"
34 
35 #include <expat.h>
36 
37 using std::vector;
41 
42 using namespace VSFileSystem;
43 
44 /*
45  *************************************************************
46  **** Display an Account's info ***
47  *************************************************************
48  */
49 
51 {
52  cout<<"Serial #: "<<serial<<endl;
53  cout<<"Name: "<<callsign<<endl;
54  cout<<"Pass: "<<passwd<<endl;
55  if (this->type == 0)
56  cout<<"Type : UNKNOWN"<<endl;
57  else if (this->type == 1)
58  cout<<"Type : PLAYER"<<endl;
59  else if (this->type == 2)
60  cout<<"Type : ADMIN"<<endl;
61  else
62  cout<<"Type : ERROR"<<endl;
63 }
64 
65 namespace accountXML
66 {
67 /*
68  *************************************************************
69  **** XML Things ***
70  *************************************************************
71  */
72 
73 enum Names
74 {
79  //attributes
85 };
86 
87 const EnumMap::Pair element_names[] = {
88  EnumMap::Pair( "UNKNOWN", UNKNOWN ), //don't add anything until below Admin so it maps to enum ACCOUNT_TYPE
89  EnumMap::Pair( "PLAYER", PLAYER ),
90  EnumMap::Pair( "ADMIN", ADMIN ),
91  EnumMap::Pair( "ACCOUNTS", ACCOUNTS )
92 };
93 
94 const EnumMap::Pair attribute_names[] = {
95  EnumMap::Pair( "UNKNOWN", UNKNOWN ),
96  EnumMap::Pair( "NAME", NAME ),
97  EnumMap::Pair( "PASSWORD", PASSWORD ),
98  EnumMap::Pair( "SERVERIP", SERVERIP ),
99  EnumMap::Pair( "SERVERPORT", SERVERPORT ),
100 };
101 
102 const EnumMap element_map( element_names, 4 );
104 string curname;
105 string file;
106 
108 //Account acct;
109 int level = -1;
110 vsUMap< string, Account* >accounttable;
111 int nbaccounts = 0;
112 
113 void beginElement( void *userData, const XML_Char *name, const XML_Char **atts )
114 {
115  AttributeList attributes( atts );
116  tmpacct = Account();
117  Names elem = (Names) element_map.lookup( string( name ) );
118 
119  AttributeList::const_iterator iter;
120  switch (elem)
121  {
122  case UNKNOWN:
124  break;
125  case ACCOUNTS:
126  assert( level == -1 );
127  level++;
128  break;
129  case PLAYER:
130  case ADMIN:
131  assert( level == 0 );
132  level++;
133  switch (elem)
134  {
135  case PLAYER:
137  break;
138  case ADMIN:
140  break;
141  default:
143  break;
144  }
145  for (iter = attributes.begin(); iter != attributes.end(); iter++) {
146  switch ( attribute_map.lookup( (*iter).name ) )
147  {
148  case UNKNOWN:
149  VSFileSystem::vs_fprintf( stderr, "Unknown Account Element %s\n", (*iter).name.c_str() );
150  break;
151  case NAME:
152  tmpacct.callsign = (*iter).value;
153  break;
154  case PASSWORD:
155  tmpacct.passwd = (*iter).value;
156  break;
157  case SERVERIP:
158  tmpacct.serverip = (*iter).value;
159  break;
160  case SERVERPORT:
161  tmpacct.serverport = (*iter).value;
162  break;
163  default:
164  assert( 0 );
165  break;
166  }
167  }
168  break;
169  case NAME:
170  case PASSWORD:
171  case SERIAL:
172  case SERVERIP:
173  case SERVERPORT:
174  break;
175  }
176 }
177 
178 void endElement( void *userData, const XML_Char *name )
179 {
180  Names elem = (Names) element_map.lookup( name );
181  switch (elem)
182  {
183  case UNKNOWN:
184  break;
185  case ACCOUNTS:
186  assert( level == 0 );
187  level--;
188  break;
189  case PLAYER:
190  case ADMIN:
191  assert( level == 1 );
192  level--;
195  nbaccounts++;
196  }
197  break;
198  default:
199  break;
200  }
201 }
202 
203 //
204 }
205 
206 using namespace accountXML;
207 
208 vector< Account* >getAllAccounts()
209 {
210  vector< Account* >retval;
211  for (vsUMap< string, Account* >::iterator iter = accounttable.begin();
212  iter != accounttable.end();
213  ++iter)
214  if (iter->second)
215  retval.push_back( iter->second );
216  return retval;
217 }
218 
220 {
221  for (vsUMap< string, Account* >::iterator iter = accounttable.begin();
222  iter != accounttable.end();
223  ++iter)
224  if (iter->second)
225  if (iter->second->getSerial() == ser)
226  return iter->second;
227  return NULL;
228 }
229 
231 {
232  for (vsUMap< string, Account* >::iterator iter = accounttable.begin();
233  iter != accounttable.end();
234  ++iter)
235  if (iter->second)
236  if ( iter->second->getSocket().sameAddress( ser ) )
237  return iter->second;
238  return NULL;
239 }
240 
241 void LoadAccounts( const char *filename )
242 {
243  file = filename;
244  VSFile f;
245  VSError err = f.OpenReadOnly( filename, AccountFile );
246  if (err > Ok)
247  return;
248  XML_Parser parser = XML_ParserCreate( NULL );
249  XML_SetElementHandler( parser, &beginElement, &endElement );
250  std::string data( f.ReadFull() );
251  XML_Parse( parser, data.c_str(), f.Size(), 1 );
252  f.Close();
253  XML_ParserFree( parser );
254 }
255 
256 void addAcct( string key, Account *acct )
257 {
258  accounttable[strtoupper( key )] = acct;
259 }
260 
261 Account * getAcctNoReload( const string &key )
262 {
263  return accounttable[strtoupper( key )];
264 }
265 
266 Account * getAcctTemplate( const string &key )
267 {
268  Account *acct = accounttable[strtoupper( key )];
269  if (acct == NULL) {
270  LoadAccounts( file.c_str() );
271  acct = accounttable[strtoupper( key )];
272  }
273  return acct;
274 }
275