Main Page   Namespace List   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

ExCXMLParser.cpp

Go to the documentation of this file.
00001 /*
00002  * ExNihilo 3D Engine
00003  * 
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * Please read AUTHORS file !!!
00019  * 
00020  * $Id: ExCXMLParser.cpp,v 1.1 2002/08/01 19:37:51 binny Exp $
00021  *
00022  */
00023  
00024 #include "ExCXMLParser.h"
00025 
00026 #ifdef XML_TEST
00027 
00028 ExCXMLAction::ExCXMLAction () {
00029         action = 0;
00030         command = "";
00031         sub = "";
00032         args = 0;
00033 }
00034 
00035 ExCXMLAction::~ExCXMLAction () {}
00036         
00037 void ExCXMLAction::setAction (int a) { 
00038         action = a;
00039 }
00040 
00041 void ExCXMLAction::setCommand (string c) { 
00042         command = c; 
00043 }
00044 
00045 void ExCXMLAction::setSub (string s) { 
00046         sub = s; 
00047 }
00048 
00049 void ExCXMLAction::setArgs (int a) { 
00050         args = a;
00051 }
00052 
00053 void ExCXMLAction::add (string s) { 
00054         sarg.push_back (s); 
00055 }
00056 
00057 void ExCXMLAction::add (int i) { 
00058         iarg.push_back (i); 
00059 }
00060 
00061 int ExCXMLAction::getAction (void) { 
00062         return action; 
00063 }
00064 
00065 string ExCXMLAction::getCommand (void) { 
00066         return command; 
00067 }
00068 
00069 string ExCXMLAction::getSub (void) { 
00070         return sub;
00071 }
00072 
00073 int ExCXMLAction::getArgs (void) { 
00074         return args;
00075 }
00076 
00077 bool ExCXMLAction::isInt (void) {
00078         if (iarg.size () != 0) 
00079                 return true;
00080         else return false;
00081 }
00082 
00083 bool ExCXMLAction::isString (void) {
00084         if (sarg.size () != 0) 
00085                 return true;
00086         else return false;
00087 }
00088 
00089 vector<int> ExCXMLAction::getInt (void) { 
00090         return iarg; 
00091 }
00092 
00093 vector<string> ExCXMLAction::getString (void) { 
00094         return sarg; 
00095 }
00096 
00097 void ExCXMLAction::reset (void) {
00098         command = "";
00099         sub = "";
00100         iarg.clear ();
00101         sarg.clear ();
00102 }
00103 
00104 ExCXMLFileParse::ExCXMLFileParse () {}
00105 ExCXMLFileParse::~ExCXMLFileParse () {}
00106 
00107 void ExCXMLFileParse::setType (string t) {
00108         type = t;
00109 }
00110 
00111 string ExCXMLFileParse::getType (void) {
00112         return type;
00113 }
00114 
00115 vector<ExCXMLAction> ExCXMLFileParse::getParsing (void) {
00116         return _action;
00117 }
00118 
00119 void ExCXMLFileParse::add (ExCXMLAction f) {
00120         _action.push_back (f);
00121 }
00122 
00123 ExCXMLParser::ExCXMLParser () {}
00124 ExCXMLParser::~ExCXMLParser () {}
00125 
00126 // ok, it was funny 'till here ;)
00127 ExCXMLFileParse ExCXMLParser::parse (ExCFileReader *file) {
00128         if (file->open ()) {
00129                 vector<string> content = file->getContent ();
00130                 vector<string>::iterator i = content.begin ();
00131                 string t;
00132                 
00133                 ExCXMLAction tmp_action;
00134                 
00135                 while (i != content.end ()) {
00136                         t = *i++;
00137                         t = t.substr (t.find ("<") + 1);                        
00138                         
00139                         switch (t[0]) {
00140                                 case '?': // <?xml .... 
00141                                         break;
00142                                 case 'f': {
00143                                         // <file type="type_of_file">                                   
00144                                         string type = t.substr (t.find ("type") + 6);
00145                                         type.erase (type.find ("\""));
00146                                         action_parsing.setType (type);
00147                                         break;
00148                                 }
00149                                 case 'c': {
00150                                         // <command name="name_of_command" sub="foo" action="12345"> action soon deprecated ?!
00151                                         string name = t.substr (t.find ("name") + 6);
00152                                         name.erase (name.find ("\""));
00153                                         tmp_action.setCommand (name);
00154                                         
00155                                         string sub = t.substr (t.find ("sub") + 5);
00156                                         sub.erase (sub.find ("\""));
00157                                         tmp_action.setSub (sub);
00158                                         
00159                                         string action = t.substr (t.find ("action") + 8);
00160                                         action.erase (action.find ("\""));
00161                                         tmp_action.setAction (atoi (action.data ()));
00162                                         break;
00163                                 }
00164                                 case 'a': {
00165                                         string targ = t.substr (t.find ("type") + 6);
00166                                         targ.erase (targ.find ("\""));
00167                                         
00168                                         if (targ != "null") {
00169                                                 string arg = t.substr (t.find (">") + 1);
00170                                                 arg.erase (arg.find ("<"));
00171                                                                                                 
00172                                                 string narg = t.substr (t.find ("number") + 8);
00173                                                 narg.erase (narg.find ("\""));
00174                                                 int n_arg = atoi (narg.data ());
00175                                                 tmp_action.setArgs (n_arg);
00176                                                 
00177                                                 for (int i = 0; i < n_arg; i++) {
00178                                                         if (targ == "int") {
00179                                                                 tmp_action.add (atoi (arg.substr (0, arg.find(" ")).data ()));
00180                                                         } else if (targ == "string") {
00181                                                                 tmp_action.add (arg.substr (0, arg.find(" ")));
00182                                                         } else if (targ == "mix") {
00183                                                         } else {
00184                                                                 cerr << "Problem in parser : " << targ << " : unknown type" << endl;
00185                                                         }       
00186                                                         arg = arg.substr (arg.find(" ") + 1);
00187                                                 }
00188                                         }
00189                                         break;
00190                                 }
00191                                 case '/': {
00192                                         // </command> or </file>
00193                                         switch (t[1]) {
00194                                                         case 'c': // </command>
00195                                                                 action_parsing.add (tmp_action);
00196                                                                 tmp_action.reset ();
00197                                                                 break;
00198                                                         case 'f': // </file>
00199                                                                 file->close ();
00200                                                                 return action_parsing;
00201                                                         default:                                                                
00202                                                                 cerr << "Problem in parser : <" << t << " : unknown tag" << endl;
00203                                                                 break;
00204                                                 }
00205                                         break;
00206                                 }
00207                                 default:
00208                                         cerr << "Problem in parser : <" << t << " : unknown tag" << endl;
00209                                         break;
00210                         }
00211                 }
00212                 file->close ();
00213         }
00214         return action_parsing;
00215 }
00216 
00217 #endif // XML_TEST

Generated on Tue Dec 10 18:20:05 2002 for ExNihilo by doxygen1.3-rc1