NIREP

NIREPFunctionParseEngine.cxx

Go to the documentation of this file.
00001 #include "NIREPFunctionParseEngine.h"
00002 #include "stdio.h"
00003 
00004 NIREPFunctionParseEngine::NIREPFunctionParseEngine(void)
00005 {
00006 }
00007 
00008 NIREPFunctionParseEngine::~NIREPFunctionParseEngine(void)
00009 {
00010 }
00011 
00012 std::string NIREPFunctionParseEngine::functionHandler(std::string str, std::string::size_type loc)//"function("
00013 {
00014   std::string capture = str.substr(0, loc);
00015   str = str.substr(loc+1);
00016 
00017   if(capture.empty()) return str;
00018 
00019   node = new Node;
00020   lst.push_back(node);
00021 
00022   current->child.push_back(node);
00023   
00024   node->root = current;
00025   node->function = capture;
00026 
00027   current = node;
00028 
00029   return str;
00030 }
00031 std::string NIREPFunctionParseEngine::operatorHandler(std::string str, std::string::size_type loc)//"T1:A-1"
00032 {
00033   std::string capture = str.substr(0, loc);
00034   str = str.substr(loc+1);
00035 
00036   Node * temp = current; 
00037 
00038   current = current->root;
00039   if(capture.empty()) return str;
00040 
00041   node = new Node;
00042   lst.push_back(node);
00043 
00044   temp->child.push_back(node);
00045   
00046   node->root = temp;
00047   node->function = capture;
00048 
00049   temp = node;
00050 
00051   current = temp;
00052   current = current->root;
00053   
00054   return str;
00055 }
00056 std::string NIREPFunctionParseEngine::endHandler(std::string str, std::string::size_type loc)//")"
00057 {
00058   std::string capture = str.substr(0, loc);
00059   str = str.substr(loc+1);
00060 
00061   Node * temp = current; 
00062   current = current->root;
00063 
00064   if(capture.empty()) return str;
00065 
00066 
00067   node = new Node;
00068   lst.push_back(node);
00069 
00070   temp->child.push_back(node);
00071   
00072   node->root = temp;
00073   node->function = capture;
00074 
00075   temp = node;
00076 
00077   current = temp;
00078   current = current->root;
00079 
00080   return str;
00081 }
00082 
00083 std::string NIREPFunctionParseEngine::removeSpaceTabNewLineReturn(std::string str)
00084 {
00085   size_t loc;
00086   while((loc = str.find(' ')) != -1)
00087     str = str.erase(loc,1);
00088   while((loc = str.find('\r')) != -1)
00089     str = str.erase(loc,1);
00090   while((loc = str.find('\t')) != -1)
00091     str = str.erase(loc,1);
00092   return str;
00093 }
00094 
00095 bool NIREPFunctionParseEngine::CheckMismatchParenthesis(std::string str)
00096 {
00097   const char * ptr = str.c_str();
00098   int x = 0, y = 0;
00099   for(unsigned int i = 0; i < str.length(); i++)
00100   {
00101     if( ptr[i] == '(' )
00102       x++;
00103     else if(ptr[i] == ')')
00104       y++;
00105     if(x-y < 0) 
00106       return false;
00107   }
00108   if(x != y) return false;
00109   return true;
00110 }
00111 bool NIREPFunctionParseEngine::CheckUndefinedFunction()
00112 {
00113   bool undef;
00114   std::vector<std::string> temp_list;
00115   for(unsigned int i = 1; i < lst.size(); i ++)
00116   {
00117      if(lst[i]->function.find(":") == -1)
00118      {
00119       temp_list.push_back(lst[i]->function);
00120      }
00121   }
00122   for(unsigned int i = 0; i < temp_list.size(); i++)
00123   {
00124     undef = true;
00125     for(unsigned int j = 0; j < func_list.size(); j++)
00126     {
00127       if(!func_list[j].func.compare(temp_list[i]))
00128       {
00129         undef = false;
00130       }
00131     }
00132     if(undef)
00133     {
00134       return true;
00135     }
00136   }
00137   return false;
00138 }
00139 
00140 void NIREPFunctionParseEngine::EvaluateString(std::string str)
00141 {
00142   std::string::size_type loc[3];
00143   while(!str.empty())
00144   {
00145     if( (loc[0] = str.find("(")) < (loc[1] =  str.find(",")))
00146       if( loc[0] < (loc[2] = str.find(")")) ) 
00147         str = functionHandler(str, loc[0]);
00148       else 
00149         str = endHandler(str, loc[2]);
00150     else if( loc[1] < (loc[2] = str.find(")"))) 
00151       str = operatorHandler(str, loc[1]);
00152     else str = endHandler(str, loc[2]);
00153   }
00154 }
00155 
00156 std::string NIREPFunctionParseEngine::DispatchCallBack(std::string func, std::vector<std::string> para)
00157 {
00158   std::vector<std::string> para_list;
00159   for(unsigned int i = 0; i < para.size(); i++)
00160   {
00161     if(para[i].find(":") == 0)
00162       para_list.push_back(cache[para[i]]);//lookpup in temp cache
00163     else
00164       para_list.push_back(para[i]);//lookup from database
00165   }
00166   for(unsigned int i = 0; i < func_list.size(); i++)
00167   {
00168     if(!func_list[i].func.compare(func))
00169     {
00170       return (*func_list[i].ptr)(para_list); //dispatchCallBack
00171     }
00172   }
00173   char buffer[128];
00174   sprintf(buffer,"undefined function %s error", func.c_str());
00175   return std::string((char *)buffer);
00176 }
00177 
00178 std::string NIREPFunctionParseEngine::CallBackHandler()
00179 {
00180  bool flag; //iteration flag, should we continue looking for calls
00181  std::string ret;
00182  do
00183   {
00184     flag = false;
00185     for(unsigned int i = 1; i < lst.size(); i++)
00186     {
00187       size_t cnt;
00188       if( (cnt = lst[i]->child.size()) >= 1)
00189       {
00190         flag = true;
00191         int tmp = cnt;
00192         for(int j = 0;j < cnt;j++)
00193         {
00194           if(lst[i]->child[j]->function.find(":") != -1)
00195           {
00196             tmp--;
00197           }
00198         }
00199         if(!tmp)
00200         {
00201           std::string _operator = lst[i]->function;
00202           std::vector<std::string> _operands;
00203           for(int j = 0; j < cnt; j++)//loop through children
00204           {
00205             _operands.push_back(lst[i]->child[j]->function);  
00206           }
00207    
00208           char buffer[32];
00209           sprintf((char *)buffer,"%d", tablecnt++);
00210 
00211           lst[i]->function = std::string(":").append(buffer);//add temp entry, so algorithm skips
00212           lst[i]->child.clear();//remove
00213           //lookup function level
00214           ret = cache[lst[i]->function] = DispatchCallBack(_operator, _operands);;
00215         }
00216       }
00217     }
00218   }while(flag);
00219 
00220   return ret;
00221 }
00222 
00223 void NIREPFunctionParseEngine::AddCallback(std::string fn, std::string (*func)(std::vector<std::string>))
00224 {
00225   func_info info;
00226   info.func = fn;
00227   info.ptr = func;
00228   func_list.push_back(info);
00229 }
00230 
00231 void NIREPFunctionParseEngine::SetString(std::string str)
00232 {
00233   this->str = str;
00234 }
00235 std::string NIREPFunctionParseEngine::Evaluate()
00236 {
00237   tablecnt = 0;
00238 
00239   str = removeSpaceTabNewLineReturn(str);
00240   if( !CheckMismatchParenthesis(str) ){
00241     return std::string("Error: Mismatch Parenthesis");//return mismatch error
00242   }
00243 
00244   //create root node
00245   node = new Node;
00246   lst.push_back(node);
00247 
00248   root = node;
00249   node->root = NULL;
00250   current = node;
00251 
00252   EvaluateString(str);
00253   if( CheckUndefinedFunction()) {
00254     return std::string("Error: Undefined Function");//return undefined function
00255   }
00256   std::string ret = CallBackHandler();
00257 
00258   Delete();
00259   return ret;
00260 }
00261 
00262 void NIREPFunctionParseEngine::Delete()
00263 {
00264   for( unsigned int i = 0; i < lst.size(); i++)
00265     delete lst[i];
00266 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines