symboltable.cpp

Go to the documentation of this file.
00001 
00003 /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005    Copyright (C) 2006  Bernd Opitz
00006    Copyright (C) 2007  Thomas Moor
00007    Exclusive copyright is granted to Klaus Schmidt
00008 
00009    This library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Lesser General Public
00011    License as published by the Free Software Foundation; either
00012    version 2.1 of the License, or (at your option) any later version.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Lesser General Public License for more details.
00018 
00019    You should have received a copy of the GNU Lesser General Public
00020    License along with this library; if not, write to the Free Software
00021    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00022 
00023 
00024 #include "symboltable.h"
00025 #include <iostream>
00026 
00027 namespace faudes {
00028 
00029 // static symbol table (used for events, should be in vGenerator)
00030 SymbolTable SymbolTable::msEventSymbolTable;
00031 
00032 // constructor
00033 SymbolTable:: SymbolTable(void) :
00034   mMyName("SymbolTable"),
00035   mMaxIndex(std::numeric_limits<Idx>::max()), 
00036   mNextIndex(1) {
00037 }
00038 
00039 // Name()
00040 std::string SymbolTable::Name(void) const {
00041   return mMyName;
00042 }
00043     
00044 //Name(name)
00045 void SymbolTable::Name(const std::string& rName) {
00046   mMyName = rName;
00047 }
00048 
00049 // Clear()
00050 void SymbolTable::Clear(void) {   
00051   mMaxIndex=std::numeric_limits<Idx>::max();
00052   mNextIndex=1;
00053   mIndexMap.clear();
00054   mNameMap.clear();
00055 }
00056 
00057 // Size()
00058 Idx SymbolTable::Size(void) {   
00059   return mIndexMap.size();
00060 }
00061 
00062 // MaxIndex()
00063 Idx SymbolTable::MaxIndex(void) const {
00064   return mMaxIndex;
00065 }
00066     
00067 // MaxIndex(index)
00068 void SymbolTable::MaxIndex(Idx index) {
00069   if(index <= std::numeric_limits<Idx>::max()) {
00070     mMaxIndex = index;
00071     return;
00072   }
00073   std::stringstream errstr;
00074   errstr << "symboltable overflow";
00075   throw Exception("SymbolTable::MaxIndex(inde))", errstr.str(), 40);
00076 }
00077     
00078 // LastIndex()
00079 Idx SymbolTable::LastIndex(void) const {
00080   return mNextIndex - 1;
00081 }
00082     
00083 // ValidSymbol(rName)
00084 bool SymbolTable::ValidSymbol(const std::string& rName)  {
00085   if(rName=="") return false;
00086   for(unsigned int cp=0;cp<rName.size();cp++) {
00087     char ch = rName[cp];
00088     if(iscntrl(ch)) return false;
00089     if(isspace(ch)) return false;
00090     if(ch=='"')    return false;
00091     if(isdigit(ch)) continue;
00092     if(isalpha(ch)) continue;
00093     if(isprint(ch)) continue;
00094     return false;
00095   }
00096   return true;
00097 }
00098 
00099 // UniqueSymbol(rName)
00100 std::string SymbolTable::UniqueSymbol(const std::string& rName) const {
00101   if(!Exists(rName)) return (rName);
00102   long int count=0;
00103   std::string name=rName;
00104   std::string bname=rName;
00105   // if the name ends with extension "_123", remove the extension
00106   unsigned int up = bname.find_last_of("_");
00107   if(up != std::string::npos) {
00108     bool ad=true;
00109     for(unsigned int dp=up+1;dp<bname.size();dp++)
00110       if(!isdigit(bname[dp])) ad=false;
00111     if(ad) 
00112       bname.erase(up);
00113   }
00114   // try append extension "_123" until name is unique
00115   do {
00116     count++;
00117     name=bname + "_" + ToStringInteger(count);
00118   } while(Exists(name));
00119   return name;
00120 }
00121 
00122 
00123 // InsEntry(index, rName)
00124 Idx SymbolTable::InsEntry(Idx index, const std::string& rName) {
00125   if( ! (index <= mMaxIndex)) {
00126     std::stringstream errstr;
00127     errstr << "symboltable overflow";
00128     throw Exception("SymbolTable::InsEntry(index,name))", errstr.str(), 40);
00129   }
00130   Idx nidx=Index(rName);
00131   if((nidx!=0) && (nidx!=index)) {
00132     std::stringstream errstr;
00133     errstr << "Name " << rName << " allready exists in EventSymbolTable";
00134     throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 41);
00135   }
00136   std::string idxname=Symbol(index);
00137   if((idxname != "") && (idxname != rName)) {
00138     std::stringstream errstr;
00139     errstr << "Index " << index << " allready exists in EventSymbolTable";
00140     throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 42);
00141   }
00142   if(!ValidSymbol(rName)) {
00143     std::stringstream errstr;
00144     errstr << "Name " << rName << " is not a valid symbol";
00145     throw Exception("SymbolTable::InsEntry(index,name)", errstr.str(), 43);
00146   }
00147 
00148   mIndexMap[rName] = index;
00149   mNameMap[index] = rName;
00150 
00151   if(mNextIndex<=index) mNextIndex=index+1; 
00152   return index;
00153 }
00154 
00155 // InsEntry(rName)
00156 Idx SymbolTable::InsEntry(const std::string& rName) {
00157   Idx index=Index(rName);
00158   if( index != 0) return index;
00159   return InsEntry(mNextIndex,rName);
00160 }
00161 
00162 
00163 // SetEntry(index, rName)
00164 void SymbolTable::SetEntry(Idx index, const std::string& rName) {
00165   Idx nidx=Index(rName);
00166   if((nidx!=0) && (nidx!=index)) {
00167     std::stringstream errstr;
00168     errstr << "Name " << rName << " allready exists";
00169     throw Exception("SymbolTable::SetEntry(index,name)", errstr.str(), 41);
00170   }
00171   if(!ValidSymbol(rName)) {
00172     std::stringstream errstr;
00173     errstr << "Name " << rName << " is not a valid symbol";
00174     throw Exception("SymbolTable::SetEntry(index,name)", errstr.str(), 43);
00175   }
00176   mIndexMap[rName] = index;
00177   mNameMap[index] = rName;
00178 }
00179 
00180 // SetDefaultSymbol(index)
00181 void SymbolTable::SetDefaultSymbol(Idx index) {
00182   ClrEntry(index); 
00183   std::ostringstream o;
00184   std::string dname;
00185   o << index;
00186   dname = UniqueSymbol(ToStringInteger(index));
00187   InsEntry(index,dname);
00188 }
00189 
00190 
00191 // ClrEntry(index)
00192 void SymbolTable::ClrEntry(Idx index) {
00193   std::map<Idx,std::string>::iterator it = mNameMap.find(index);
00194   if (it == mNameMap.end()) return;
00195   mIndexMap.erase(it->second);
00196   mNameMap.erase(it);
00197 }
00198 
00199 // ClrEntry(rName)
00200 void SymbolTable::ClrEntry(const std::string& rName) {
00201   std::map<std::string,Idx>::iterator it = mIndexMap.find(rName);
00202   if (it == mIndexMap.end()) return;
00203   mNameMap.erase(it->second);
00204   mIndexMap.erase(it);
00205 }
00206 
00207 // Index(rName)
00208 Idx SymbolTable::Index(const std::string& rName) const {
00209   std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName);
00210   if (it == mIndexMap.end()) 
00211     return 0;
00212   else 
00213     return it->second;
00214 }
00215 
00216 // Symbol(index)
00217 std::string SymbolTable::Symbol(Idx index) const {
00218   std::map<Idx,std::string>::const_iterator it = mNameMap.find(index);
00219   if (it == mNameMap.end()) 
00220     return "";
00221   else 
00222     return it->second;
00223 }
00224 
00225 
00226 
00227 // Exists(index)
00228 bool SymbolTable::Exists(Idx index) const {
00229   std::map<Idx,std::string>::const_iterator it = mNameMap.find(index);
00230   return (it != mNameMap.end());
00231 }
00232 
00233 // Exists(rName)
00234 bool SymbolTable::Exists(const std::string& rName) const {
00235   std::map<std::string,Idx>::const_iterator it = mIndexMap.find(rName);
00236   return ( it != mIndexMap.end());
00237 }
00238 
00239 
00240 // GlobalEventSymbolTablep 
00241 SymbolTable* SymbolTable::GlobalEventSymbolTablep(void) {
00242   return &msEventSymbolTable; 
00243 }
00244 
00245 
00246 
00247 
00248 } // namespace

Generated on Fri May 9 11:26:47 2008 for libFAUDES 2.09b by  doxygen 1.4.4