TreeParser.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Eric Sauser, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 // TreeParser.cpp: implementation of the sXMLParser class.
00020 //
00022 
00023 #include "TreeParser.h"
00024 
00025 //string        Tree::CurrentPath;
00026 
00027 Tree::Tree()
00028 {
00029         Clear();
00030 }
00031 
00032 Tree::Tree(string newName)
00033 {
00034         Clear();
00035         m_Name.assign(newName);
00036 }
00037 Tree::Tree(string newName, string newData)
00038 {
00039         Clear();
00040         m_Name.assign(newName);
00041         m_Data.assign(newData);
00042 }
00043 /*
00044 Tree::Tree(string newName, string newData, Tree_List & newSubTree)
00045 {
00046         Clear();
00047         m_Name.assign(newName);
00048         m_Data.assign(newData);
00049         for(int i=0;i<newSubTree.size();i++)
00050                 m_SubTree.push_back(newSubTree[i]);
00051 }
00052 */
00053 Tree::~Tree()
00054 {
00055         Clear();
00056 }
00057 
00058 pTree Tree::Clone(){
00059   pTree clone = new Tree(m_Name);
00060   clone->m_Data = m_Data;
00061   for(unsigned int i=0;i<m_SubTree.size();i++)
00062                 clone->m_SubTree.push_back(m_SubTree[i]->Clone());
00063   return clone;
00064 }
00065 
00066 string Tree::GetName(){
00067   return m_Name;
00068 }
00069 string Tree::GetData(){
00070   return m_Data;
00071 }
00072 void Tree::SetName(string newName){
00073   m_Name = newName;
00074 }
00075 void Tree::SetData(string newData){
00076   m_Data = newData;
00077 }
00078 
00079 pTree Tree::Find(string name){
00080   return Find(name,"");
00081 }
00082 
00083 pTree Tree::Find(string name, string data){
00084   unsigned int pointPos = name.find(".");
00085   string cTag;
00086   string eTag;
00087 
00088   if(pointPos != string::npos){
00089     cTag  = name.substr(0,pointPos);
00090     eTag  = name.substr(pointPos+1,name.size()-pointPos-1);
00091   }else{
00092     cTag  = name;
00093   }
00094 
00095   //  cout <<name<<" = "<< cTag<<"."<<eTag<<endl;
00096 
00097   for(unsigned int i=0;i<m_SubTree.size();i++){
00098     if(cTag.compare(m_SubTree[i]->m_Name)==0){
00099       if(eTag.size()==0){
00100         if(data.size()==0){
00101           return m_SubTree[i];
00102         }else{
00103           if(data.compare(m_SubTree[i]->m_Data)==0)
00104             return m_SubTree[i];
00105         }
00106       }else{
00107         return m_SubTree[i]->Find(eTag,data);
00108       }
00109     }
00110   }
00111   return NULL;
00112 }
00113 
00114 
00115 string Tree::FindData(string name){
00116   pTree tree = Find(name);
00117   if(tree!=NULL)
00118     return tree->m_Data;
00119   return "";
00120 }
00121 
00122 void Tree::Clear()
00123 {
00124   for(unsigned int i=0;i<m_SubTree.size();i++)
00125                 delete m_SubTree[i];
00126         m_SubTree.clear();
00127         m_Data.assign("");
00128         m_Name.assign("");
00129 }
00130 
00131 int Tree::AddSubTree(pTree newSubTree)
00132 {
00133         if(newSubTree!=NULL)
00134                 m_SubTree.push_back(newSubTree);
00135 
00136         return m_SubTree.size();
00137 }
00138 
00139 int Tree::DelSubTree(pTree oldSubTree)
00140 {
00141   if(oldSubTree!=NULL){
00142     for(unsigned int i=0;i<m_SubTree.size();i++)
00143       if(m_SubTree[i] == oldSubTree){
00144         m_SubTree.erase(m_SubTree.begin()+i);
00145         delete oldSubTree;        
00146         break;
00147       }else{
00148         m_SubTree[i]->DelSubTree(oldSubTree);
00149       }
00150   }
00151 
00152         return m_SubTree.size();
00153 }
00154 
00155 void Tree::Print(unsigned int indent)
00156 {
00157   unsigned int i;
00158   char *c = new char[indent+1];
00159   
00160   for(i=0;i<indent;i++) c[i]=' ';
00161   c[i]=0;
00162   
00163   cout << c << m_Name << "\t\t" << m_Data << endl;
00164   
00165   for(i=0;i<m_SubTree.size();i++)
00166     m_SubTree[i]->Print(indent+2);
00167 
00168   delete c;
00169 }
00170 
00171 int Tree::GetNextTag(ifstream & file, string & str)
00172 {
00173         string s;
00174 
00175         if (file.eof()) return TREE_EOF;
00176 
00177         file >> s;
00178         int length = s.length();
00179         if ((s[0]=='<') && (s[length-1]=='>')){
00180                 if(s[1]!='/'){
00181                         str.assign(s,1,length-2);
00182                         return TREE_START_TAG;
00183                 }else{
00184                         str.assign(s,2,length-3);
00185                         return TREE_STOP_TAG;
00186                 }
00187         }
00188         else{
00189                 str = s;
00190                 return TREE_DATA;
00191         }
00192 }
00193 
00194 
00195 
00196 int Tree::LoadFromFile(string filename)
00197 {
00198         ifstream  file;
00199         string    s;
00200   int       result = TREE_ERROR;
00201 
00202   /*
00203         string   ContextCurrentPath = CurrentPath;
00204 
00205         for(int i=filename.size()-1;i>=0;i--)
00206                 if((filename[i]=='/') || (filename[i]=='\\'))
00207                         break;
00208         if(i>=0) CurrentPath = filename.substr(0,i+1);
00209   */
00210 
00211         file.open(filename.c_str());
00212   if(file.is_open()){
00213     int res = GetNextTag(file,s);
00214 
00215           if((res==TREE_START_TAG)){
00216                 m_Name = s;
00217                   result = LoadFromStream(file);
00218           }
00219   
00220                 file.close();
00221   }else{
00222     printf("Error: File not found\n");
00223   }
00224   
00225   return result;
00226   /*
00227   else{
00228                 CurrentPath = ContextCurrentPath;
00229                 return false;
00230         }
00231         CurrentPath = ContextCurrentPath;
00232   */
00233         //return true;
00234 }
00235 
00236 
00237 int     Tree::LoadFromStream(ifstream & file)
00238 {
00239   //    char c=' ';
00240         string s;
00241         m_Data ="";
00242 
00243         while(!file.eof()){
00244 
00245                 int res = GetNextTag(file,s);
00246                 switch(res){
00247 
00248                 case TREE_DATA:
00249                         m_Data.append(s);
00250                         m_Data.append(" ");
00251                         break;
00252 
00253                 case TREE_START_TAG:
00254                         {
00255                           //                      printf("Start Tag found: %s\n",s.c_str());
00256                                 pTree newField = new Tree(s);
00257         int flag = newField->LoadFromStream(file);
00258         if(flag == TREE_OK){
00259                                         AddSubTree(newField);
00260         }else{
00261           delete newField;
00262                                         return TREE_ERROR;
00263         }
00264                         }
00265                         break;
00266 
00267                 case TREE_STOP_TAG:
00268                         if(m_Name.compare(s)==0){
00269                                 if(m_Data.size()>0){
00270                                         m_Data.assign(m_Data,0,m_Data.size()-1);
00271                                         //                                      printf("Block found: Start Tag: %s, Data %s\n",m_Name.c_str(),m_Data.c_str());
00272 
00273                                 
00274           /*
00275           if(name.compare("Include")==0){
00276                                                 string newFile;
00277                                                 newFile.assign(CurrentPath);
00278                                                 newFile.append(data);
00279                                                 this->LoadFromXMLFile(newFile);
00280                                         }
00281           */
00282                                 }
00283                                 return TREE_OK;
00284                         }
00285                         else{
00286                           printf("Stop Tag incorrect: %s instead of %s having data %s.\n",s.c_str(),m_Name.c_str(),m_Data.c_str());
00287                                 return TREE_ERROR;
00288                         }
00289 
00290                 case TREE_EOF:
00291                   {
00292                     printf("End of File reached: Missing Stop Tag: %s.\n",m_Name.c_str());
00293                         return TREE_ERROR;
00294                   }
00295 
00296                 }
00297         }       
00298         return TREE_ERROR;
00299 }
00300 
00301 
00302 
00303 int Tree::SaveToFile(string filename)
00304 {
00305         ofstream file;
00306         string s;
00307   int result = TREE_ERROR;
00308 
00309         file.open(filename.c_str());
00310   if(file.is_open()){
00311           result = SaveToStream(file);
00312           file.close();
00313   }
00314         return result;
00315 }
00316 
00317 int Tree::SaveToStream(ofstream & file, unsigned int indent)
00318 {
00319         unsigned int i;
00320         char *ind = new char[indent+1];
00321         for(i=0;i<indent;i++) ind[i]=' ';
00322         ind[i]=0;
00323 
00324         file << ind<<"<" << m_Name << "> " << m_Data << endl;
00325         for(i=0;i<m_SubTree.size();i++)
00326                 m_SubTree[i]->SaveToStream(file,indent+2);
00327 
00328         file << ind<<"</" << m_Name << ">"<<endl;
00329         delete ind;
00330         return TREE_OK;
00331 }
00332 
00333 
00334 double  Tree::ToDouble(){
00335         istringstream ss(m_Data);
00336         double tmp = 0.0;
00337         ss >> tmp;
00338         return tmp;
00339 }
00340 float   Tree::ToFloat(){
00341         istringstream ss(m_Data);
00342         float tmp = 0.0f;
00343         ss >> tmp;
00344         return tmp;
00345 }
00346 int             Tree::ToInt(){
00347         istringstream ss(m_Data);
00348         int tmp = 0;
00349         ss >> tmp;
00350         return tmp;
00351 }
00352 bool    Tree::ToBool(){
00353         bool tmp = false;
00354         tmp = (m_Data.compare("true")==0?true:false);
00355         return tmp;
00356 }
00357 
00358 Tree_List *Tree::GetSubTrees(){
00359   return &m_SubTree;
00360 }
00361 
00362 /*
00363 
00364 vector<TreeVisitorRegistryItem*>        TreeVisitorRegistry::Items;
00365 
00366 
00367 TreeVisitorRegistryItem* TreeVisitorRegistry::GetItem(string name){
00368         for(unsigned int i=0;i<Items.size();i++)
00369                 if(Items[i]->Name.compare(name) == 0)
00370                         return Items[i];
00371         return NULL;
00372 }
00373 
00374 int TreeVisitorRegistry::Register(TreeVisitor*(*NewFunc)(), string name){
00375         if(GetItem(name) == NULL){
00376                 TreeVisitorRegistryItem *tmp = new TreeVisitorRegistryItem();
00377                 tmp->Name    = name;
00378                 tmp->NewFunc = NewFunc; 
00379                 Items.push_back(tmp);
00380                 return true;
00381         }
00382         return false;
00383 }
00384 
00385 void TreeVisitorRegistry::Clear()
00386 {
00387         for(unsigned int i=0;i,Items.size();i++)
00388                 delete Items[i];
00389         Items.clear();
00390 }
00391 
00392 TreeVisitor * TreeVisitorRegistry::GetVisitor(string name){
00393         TreeVisitorRegistryItem *tmp;
00394         if((tmp=GetItem(name)) != NULL)
00395                 return tmp->NewFunc();
00396         return NULL;
00397 }
00398 
00399 TreeVisitor * TreeVisitorRegistry::ApplyTree(Tree *tree)
00400 {       
00401         TreeVisitorRegistryItem *tmp;
00402         if((tmp=GetItem(tree->name)) != NULL)
00403                 return (tmp->NewFunc())->Apply(tree);
00404         
00405         return false;           
00406 }
00407 
00408 TreeVisitor * TreeVisitorRegistry::ApplyFile(string filename)
00409 {
00410         Tree                    *myTree         = new Tree();
00411         TreeVisitor     *myObject       = NULL;
00412 
00413         myTree->LoadFromXMLFile(filename);
00414         myObject = TreeVisitorRegistry::ApplyTree(myTree);
00415         
00416         delete myTree;
00417         return myObject;
00418 }
00419 
00420 
00421 ObjectContainer::ObjectContainer()
00422 :ObjectRegistry(){
00423 }
00424 
00425 ObjectContainer::~ObjectContainer(){}
00426 
00427 
00428 TreeVisitor *ObjectContainer::Apply(Tree * tree){
00429         int i;
00430         for(i=0;i<tree->subTree.size();i++)
00431                 Register(TreeVisitorRegistry::ApplyTree(tree->subTree[i]),tree->subTree[i]->data);
00432         return this;
00433 }
00434 */
00435 
00436 /*
00437 void* Tree::NewSubTree(string val)
00438 {
00439         return new Tree(val);
00440 }
00441 */
00442 
00443 
00444 /*
00445 int Tree::ResetLookUp()
00446 {
00447         lookupPos=0;
00448         for(int i=0;i<subTree.size();i++)
00449                 subTree[i]->ResetLookUp();
00450         return true;
00451 }
00452 
00453 pTree   Tree::LookUp(string val,int depth)
00454 {
00455         pTree   tmp = NULL;
00456         if(val.compare(name)==0){
00457                 tmp=this;
00458         }
00459         else if(depth!=0){
00460                 for(int i=lookupPos;i<subTree.size();i++)
00461                         if((tmp=subTree[i]->LookUp(val,depth-1))!=NULL){
00462                                 lookupPos = i+1;
00463                                 return tmp;
00464                         }
00465                 for(i=0;i<lookupPos;i++)
00466                         if((tmp=subTree[i]->LookUp(val,depth-1))!=NULL){
00467                                 lookupPos = i+1;
00468                                 return tmp;
00469                         }
00470                 if(lookupPos>=subTree.size()) 
00471                         lookupPos = 0;
00472         }
00473         return tmp;                     
00474 }
00475 
00476 pTree   Tree::LookUpData(string val,int depth)
00477 {
00478         pTree   tmp = NULL;
00479 
00480         if(val.compare(data)==0)
00481                 tmp=this;
00482         else if(depth!=0){
00483                 for(int i=lookupPos;i<subTree.size();i++)
00484                         if((tmp=subTree[i]->LookUpData(val,depth-1))!=NULL){
00485                                 lookupPos = i+1;
00486                                 return tmp;             
00487                         }
00488                 for(i=0;i<lookupPos;i++)
00489                         if((tmp=subTree[i]->LookUpData(val,depth-1))!=NULL){
00490                                 lookupPos = i+1;
00491                                 return tmp;             
00492                         }
00493                 if(lookupPos>=subTree.size()) 
00494                         lookupPos = 0;
00495         }
00496         return tmp;                     
00497 }
00498 
00499 pTree   Tree::LookUpData(string val,string type,int depth)
00500 {
00501         pTree   tmp = NULL;
00502 
00503         if((val.compare(data)==0)&&(type.compare(name)==0))
00504                 tmp=this;
00505         else if(depth!=0){
00506                 for(int i=lookupPos;i<subTree.size();i++)
00507                         if((tmp=subTree[i]->LookUpData(val,type,depth-1))!=NULL){
00508                                 lookupPos = i+1;
00509                                 return tmp;             
00510                         }
00511                 for(i=0;i<lookupPos;i++)
00512                         if((tmp=subTree[i]->LookUpData(val,type,depth-1))!=NULL){
00513                                 lookupPos = i+1;
00514                                 return tmp;             
00515                         }
00516                 if(lookupPos>=subTree.size()) 
00517                         lookupPos = 0;
00518         }
00519         return tmp;                     
00520 }
00521 */
00522 
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:26 2010 for Body_Schema_Learning by  doxygen 1.6.1