00001 #include "debug_tools.h"
00002 #include "ModulesParser.h"
00003 #include <sstream>
00004 #include <string.h>
00005 #include <algorithm>
00006 #include <functional>
00007 #include <stdio.h>
00008 #include <iostream>
00009 #include <TString.h>
00010 #include <ctype.h>
00011 using std::cout;
00012 using std::endl;
00013
00014 int modules::parser::TokeniseByWhiteSpace(const std::string& input, std::vector<std::string>& output){
00015 std::stringstream ss(input);
00016 std::string val;
00017 while(ss>>val) output.push_back(val);
00018 return output.size();
00019 }
00020
00021 int modules::parser::TokeniseByDelimiter(const std::string& input, std::vector<std::string>& output, const char* delim){
00022 char line[modules::parser::max_line];
00023 strcpy(line,input.c_str());
00024 char* word = strtok(line,delim);
00025 while(word != NULL){
00026
00027 output.push_back(word);
00028 word = strtok(NULL,delim);
00029 }
00030 return output.size();
00031 }
00032
00033 std::string modules::parser::GetOneWord(const std::string& in, size_t start, size_t stop){
00034 std::stringstream ss(in.substr(start,stop-start));
00035 std::string word;
00036 ss>>word;
00037 return word;
00038 }
00039
00040
00041 modules::parser::Constructor_t modules::parser::ParseConstructor(const std::string& input, char open, char close)
00042 throw(modules::parser::errors::unmatched_parenthesis){
00043 size_t end_br=std::string::npos;
00044 size_t start_br=input.find(open);
00045 Constructor_t retVal;
00046
00047 retVal.before=input.substr(0,start_br);
00048
00049 TrimWhiteSpaceBeforeAfter(retVal.before);
00050
00051 if(start_br!=std::string::npos){
00052 end_br=input.rfind(close);
00053 if(end_br==std::string::npos){
00054
00055 throw modules::parser::errors::unmatched_parenthesis(open,close);
00056 }
00057 retVal.inside=input.substr(start_br+1,end_br-start_br-1);
00058 TrimWhiteSpaceBeforeAfter(retVal.inside);
00059 }
00060 return retVal;
00061 }
00062
00063 size_t modules::parser::RemoveWhitespace(std::string& input){
00064 return RemoveWhitespace(input, input.begin(),input.end());
00065 }
00066
00067 size_t modules::parser::RemoveWhitespace(std::string& input, std::string::iterator start,std::string::iterator end){
00068 std::string::iterator new_end=std::remove_if(start,end,modules::parser::IsWhitespace);
00069 input.erase(new_end,end);
00070 return input.size();
00071 }
00072
00073 const std::string& modules::parser::ToCppValid(const std::string& input){
00074 return ReplaceAll(input," :{}#*()-=+$%^&!.,/?","_" );
00075 }
00076
00077 void modules::parser::ToCppValid(std::string& input){
00078 ReplaceAll(input," :{}#*()-=+$%^&!.,/?","_" );
00079 }
00080
00081 const std::string& modules::parser::ReplaceAll(const std::string& input, const std::string& search,const std::string& replace ){
00082 static std::string output;
00083 output.clear();
00084 for(std::string::const_iterator i_char=input.begin(); i_char!=input.end();++i_char){
00085 if(std::find(search.begin(),search.end(), *i_char)!=search.end()){
00086 output+=replace;
00087 } else{
00088 output+=*i_char;
00089 }
00090 }
00091 return output;
00092 }
00093
00094 void modules::parser::ReplaceAll(std::string& input, const std::string& search, const std::string& replace ){
00095 input=ReplaceAll(const_cast<const std::string&>(input),search,replace);
00096 }
00097
00098 void modules::parser::ReplaceWords(std::string& input, const std::string& search, const std::string& replace){
00099 size_t match_start;
00100 for( match_start=input.find(search);
00101 match_start!=std::string::npos;
00102 match_start=input.find(search)){
00103 input=input.substr(0,match_start) + replace + input.substr(match_start+search.length());
00104 }
00105 }
00106
00107 void modules::parser::TrimWhiteSpaceBeforeAfter(std::string& line){
00108 size_t not_white=line.find_first_not_of(" \t");
00109 if(not_white!=std::string::npos)
00110 RemoveWhitespace(line, line.begin(),line.begin()+not_white+1);
00111
00112 not_white=line.find_last_not_of(" \t");
00113 if(not_white!=std::string::npos)
00114 RemoveWhitespace(line, line.begin()+not_white+1,line.end());
00115 }
00116
00117 bool modules::parser::IsWhitespace(char in){
00118 bool retval=false;
00119 switch(in){
00120 case ' ': case '\t':
00121 retval=true;
00122 break;
00123 default:
00124 retval=false;
00125 }
00126 return retval;
00127 }
00128
00129 bool modules::parser::IsDigit(char in){
00130 bool retval=false;
00131 switch(in){
00132 case '0': case '1': case '2': case '3': case '4':
00133 case '5': case '6': case '7': case '8': case '9':
00134 retval=true;
00135 }
00136 return retval;
00137 }
00138
00139 bool modules::parser::IsDecimal(char in){
00140 return in=='.';
00141 }
00142
00143 bool modules::parser::IsFloatChar(char in){
00144 return IsDecimal(in) || IsDigit(in);
00145 }
00146
00147 bool modules::parser::IsNotFloatChar(char in){
00148 return !IsFloatChar(in);
00149 }
00150
00151 bool modules::parser::IsNumber(const std::string& input){
00152 return std::find_if(input.begin(),input.end(),IsNotFloatChar) == input.end();
00153 }
00154
00155 int modules::parser::GetNumber(const std::string& input){
00156 int val;
00157 sscanf ( input.c_str(), "%d", &val );
00158 return val;
00159 }
00160
00161 double modules::parser::GetDouble(const std::string& input, size_t start, size_t stop){
00162 TString tstr=input.substr(start,stop-start);
00163 double value=tstr.Atof();
00164 return value;
00165 }
00166
00167 bool modules::parser::iequals(const char a, const char b){
00168 return ( a=='_' && b=='-' ) ||
00169 ( a=='-' && b=='_' ) ||
00170 ( tolower(a) == tolower(b));
00171 }
00172
00173 bool modules::parser::iequals(const std::string& a, const std::string& b){
00174 unsigned int sz = a.size();
00175 if (b.size() != sz) return false;
00176 for (unsigned int i = 0; i < sz; ++i){
00177 if (! iequals(a[i],b[i])) return false;
00178 }
00179 return true;
00180 }
00181
00182 bool modules::parser::IsTrue(const std::string& val){
00183 return (val=="true")|| (val=="TRUE")|| (val=="YES")|| (val=="yes")|| (val=="on")|| (val=="ON")|| (val=="1");
00184 }