StringOperations.h
Go to the documentation of this file.
00001 /*
00002  * StringOperations.h
00003  *
00004  *  Created on: Oct 4, 2012
00005  *      Author: dan
00006  */
00007 
00008 #ifndef ROBOT_TASK_STRINGOPERATIONS_H_
00009 #define ROBOT_TASK_STRINGOPERATIONS_H_
00010 
00011 #include <sstream>
00012 #include <vector>
00013 #include <map>
00014 #include <boost/algorithm/string.hpp>
00015 
00016 namespace robot_task_strings{
00017 
00018 using namespace std;
00019 
00020 inline bool parse(std::string line, string& name, string& params, string& sufix, string parentses="()"){
00021         if(parentses.size()>2 || parentses.size()<1) return false;
00022         char op,cp;
00023         if(parentses.size()==1) op=cp=parentses[0];
00024         if(parentses.size()==2){ op=parentses[0]; cp=parentses[1]; }
00025         int state = 1, pcount=0;
00026         stringstream sname,sparams, ssufix;
00027         for(size_t i=0;i<line.size();i++){
00028                 if(line[i]==op){
00029                         pcount+=1;
00030                         if(parentses.size()==1 && state==2){ state=3; continue; }
00031                         if(state==1 && pcount==1){ state=2; continue; }
00032                 }
00033                 if(line[i]==cp){
00034                         pcount-=1;
00035                         if(state<3 && pcount<0) return false;
00036                         if(state==2 && pcount==0){ state=3; continue; }
00037                 }
00038                 if(state==1) sname<<line[i]; else
00039                 if(state==2) sparams<<line[i]; else
00040                 if(state==3) ssufix<<line[i];
00041         }
00042         if(state==2) return false;
00043         name = sname.str(); params = sparams.str(); sufix = ssufix.str();
00044         return true;
00045 }
00046 
00047 inline int split(std::string line, std::vector<string>& list, string del="()"){
00048         struct _c{ bool contains(string& p, char c){ for(size_t i=0;i<p.size();i++) if(p[i]==c) return true; return false; } };
00049         stringstream word;
00050         for(size_t i=0;i<line.size();i++){
00051                 if(_c().contains(del,line[i])){
00052                         list.push_back(word.str()); word.str("");
00053                         continue;
00054                 }
00055                 word<<line[i];
00056         }
00057         list.push_back(word.str());
00058         return list.size();
00059 }
00060 inline std::vector<string> split(std::string line, string del="()"){
00061         std::vector<string> v;
00062         split(line, v, del);
00063         return v;
00064 }
00065 
00066 inline string join(const std::vector<string>& list, string del="()"){
00067         if(list.size()<1) return "";
00068         stringstream line;
00069         line << list[0];
00070         for(size_t i=1;i<list.size();i++){
00071                 line<<del[(i-1)%del.size()]<<list[i];
00072         }
00073         return line.str();
00074 }
00075 
00076 inline string trim(const string s){
00077         string t = s;
00078         boost::trim(t);
00079         return t;
00080 }
00081 inline string toLower(const string s){
00082         string t = s;
00083         boost::to_upper(t);
00084         return t;
00085 }
00086 inline string toUpper(const string s){
00087         string t = s;
00088         boost::to_upper(t);
00089         return t;
00090 }
00091 
00092 inline bool startWith(const std::string& line, const std::string& t){
00093         if(t.size()>line.size()) return false;
00094         if(t.size()==line.size()) return t==line;
00095         for(size_t i=0;i<t.size();i++){
00096                 if(line[i]!=t[i]) return false;
00097         }
00098         return true;
00099 }
00100 inline bool endWith(const std::string& line, const std::string& t){
00101         if(t.size()>line.size()) return false;
00102         if(t.size()==line.size()) return t==line;
00103         for(int i=t.size()-1;i>=0;i--){
00104                 if(line[i]!=t[i]) return false;
00105         }
00106         return true;
00107 }
00108 typedef std::map<std::string,std::string> Arguments;
00109 struct Function{
00110 private:
00111         bool undef;
00112 public:
00113         string name;
00114         string suffix;
00115         map<string,string> values;
00116         Function():undef(true),name(""){}
00117         string str(){
00118                 if(undef) return "undefined";
00119                 stringstream out;
00120                 out<<name<<"(";
00121                 string d = "";
00122                 for(map<string,string>::iterator i=values.begin();i!=values.end();i++){
00123                         out<<d<<i->first<<"="<<i->second; d=",";
00124                 }
00125                 out<<")"<<suffix;
00126                 return out.str();
00127         }
00128         bool isUndefined()const{ return undef; }
00129         void setDefined(){ undef = false; }
00130 };
00131 inline void trimAll(std::vector<string>& v){
00132         for(size_t i=0;i<v.size();i++) v[i]=trim(v[i]);
00133 }
00134 static Function parse(std::string line){
00135         string name,params,suf;
00136         if(!parse(line,name,params,suf,"()")) return Function();
00137         if(trim(name)=="") return Function();
00138         Function f; f.name=trim(name); f.suffix = trim(suf);
00139         f.setDefined();
00140         vector<string> vars = split(params,",");
00141         for(size_t i=0;i<vars.size();i++){
00142                 if(trim(vars[i])=="") continue;
00143                 vector<string> pair;
00144                 int c = split(vars[i], pair, "=");
00145                 trimAll(pair);
00146                 if(c==1){ stringstream s; s<<"#"<<i; f.values[s.str()] = pair[0]; }
00147                 else if(c==2){ f.values[pair[0]]=pair[1]; }
00148                 else continue;
00149         }
00150         return f;
00151 }
00152 static map<string,string> parseFunctionArgumens(std::string line){
00153         stringstream sline; sline<<"_("<<line<<")";
00154         Function f = parse(sline.str());
00155         return f.values;
00156 }
00157 
00158 static Arguments parseArguments(std::string line){
00159         std::map<std::string,std::string> args;
00160         vector<string> vars = split(line,",");
00161         for(size_t i=0;i<vars.size();i++){
00162                 if(trim(vars[i])=="") continue;
00163                 vector<string> pair;
00164                 int c = split(vars[i], pair, "=");
00165                 trimAll(pair);
00166                 if(c==1){ stringstream s; s<<"#"<<i; args[s.str()] = pair[0]; }
00167                 else if(c==2){ args[pair[0]]=pair[1]; }
00168                 else continue;
00169         }
00170         return args;
00171 }
00172 
00173 }
00174 
00175 #endif /* STRINGOPERATIONS_H_ */


robot_task
Author(s):
autogenerated on Wed Aug 26 2015 11:16:50