- Timestamp:
- Aug 27, 2010, 7:29:49 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/command/Executor.cc
r7228 r7230 50 50 } 51 51 52 MultiType Executor::parse(const std::string& params, int* error, const std::string& delimiter, bool bPrintError) const52 MultiType Executor::parse(const std::string& arguments, int* error, const std::string& delimiter, bool bPrintError) const 53 53 { 54 if (error)55 *error = CommandExecutor::Success;54 return this->parse(SubString(arguments, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'), error, delimiter, bPrintError); 55 } 56 56 57 MultiType Executor::parse(const SubString& arguments, int* error, const std::string& delimiter, bool bPrintError) const 58 { 59 MultiType param[MAX_FUNCTOR_ARGUMENTS]; 60 unsigned int paramCount = this->evaluateParams(arguments, param, error, delimiter); 61 62 if (error && *error) 63 { 64 if (bPrintError) 65 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << arguments.join() << ")." << std::endl; 66 return MT_Type::Null; 67 } 68 69 switch (paramCount) 70 { 71 case 0: return (*this->functor_)(); 72 case 1: return (*this->functor_)(param[0]); 73 case 2: return (*this->functor_)(param[0], param[1]); 74 case 3: return (*this->functor_)(param[0], param[1], param[2]); 75 case 4: return (*this->functor_)(param[0], param[1], param[2], param[3]); 76 case 5: 77 default: return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); 78 } 79 } 80 81 int Executor::evaluateParams(const SubString& arguments, MultiType param[MAX_FUNCTOR_ARGUMENTS], int* error, const std::string& delimiter) const 82 { 57 83 unsigned int paramCount = this->functor_->getParamCount(); 84 unsigned int argumentCount = arguments.size(); 58 85 59 if (paramCount == 0) 86 // if there are not enough params given, check if there are default values 87 for (unsigned int i = argumentCount; i < paramCount; i++) 60 88 { 61 COUT(5) << "Calling Executor " << this->name_ << " through parser without parameters." << std::endl; 62 return (*this->functor_)(); 63 } 64 else if (paramCount == 1) 65 { 66 const std::string& temp = getStripped(params); 67 if (!temp.empty()) 89 if (this->defaultValue_[i].null()) 68 90 { 69 COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using whole string: " << params << std::endl;70 return (*this->functor_)(params);71 }72 else if (!this->defaultValue_[0].null())73 {74 COUT(5) << "Calling Executor " << this->name_ << " through parser with one parameter, using default value: " << this->defaultValue_[0] << std::endl;75 return (*this->functor_)(this->defaultValue_[0]);76 }77 else78 {79 if (bPrintError)80 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << temp << ")." << std::endl;81 91 if (error) 82 92 *error = CommandExecutor::Incomplete; 83 return MT_Type::Null; 84 } 85 } 86 else 87 { 88 SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); 89 90 for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) 91 { 92 if (this->defaultValue_[i].null()) 93 { 94 if (bPrintError) 95 COUT(2) << "Warning: Can't call executor " << this->name_ << " through parser: Not enough parameters or default values given (input: " << params << ")." << std::endl; 96 if (error) 97 *error = CommandExecutor::Incomplete; 98 return MT_Type::Null; 99 } 100 } 101 102 MultiType param[MAX_FUNCTOR_ARGUMENTS]; 103 COUT(5) << "Calling Executor " << this->name_ << " through parser with " << paramCount << " parameters, using " << tokens.size() << " tokens ("; 104 for (unsigned int i = 0; i < tokens.size() && i < MAX_FUNCTOR_ARGUMENTS; i++) 105 { 106 param[i] = tokens[i]; 107 if (i != 0) 108 { 109 COUT(5) << ", "; 110 } 111 COUT(5) << tokens[i]; 112 } 113 COUT(5) << ") and " << std::max((int)paramCount - (int)tokens.size(), 0) << " default values ("; 114 for (unsigned int i = tokens.size(); i < paramCount; i++) 115 { 116 param[i] = this->defaultValue_[i]; 117 if (i != 0) 118 { 119 COUT(5) << ", "; 120 } 121 COUT(5) << this->defaultValue_[i]; 122 } 123 COUT(5) << ")." << std::endl; 124 125 if ((tokens.size() > paramCount) && (this->functor_->getTypenameParam(paramCount - 1) == "string")) 126 param[paramCount - 1] = tokens.subSet(paramCount - 1).join(); 127 128 switch(paramCount) 129 { 130 case 2: 131 return (*this->functor_)(param[0], param[1]); 132 case 3: 133 return (*this->functor_)(param[0], param[1], param[2]); 134 case 4: 135 return (*this->functor_)(param[0], param[1], param[2], param[3]); 136 case 5: 137 return (*this->functor_)(param[0], param[1], param[2], param[3], param[4]); 93 return 0; 138 94 } 139 95 } 140 96 141 return MT_Type::Null; 142 } 97 // assign all given arguments to the multitypes 98 for (unsigned int i = 0; i < std::min(argumentCount, MAX_FUNCTOR_ARGUMENTS); i++) 99 param[i] = arguments[i]; 143 100 144 bool Executor::evaluate(const std::string& params, MultiType param[5], const std::string& delimiter) const145 {146 unsigned int paramCount = this->functor_->getParamCount();101 // fill the remaining multitypes with default values 102 for (unsigned int i = argumentCount; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 103 param[i] = this->defaultValue_[i]; 147 104 148 if (paramCount == 1) 149 { 150 // only one param: check if there are params given, otherwise try to use default values 151 if (!getStripped(params).empty()) 152 { 153 param[0] = params; 154 this->functor_->evaluateParam(0, param[0]); 155 return true; 156 } 157 else if (!this->defaultValue_[0].null()) 158 { 159 param[0] = this->defaultValue_[0]; 160 this->functor_->evaluateParam(0, param[0]); 161 return true; 162 } 163 return false; 164 } 165 else 166 { 167 // more than one param 168 SubString tokens(params, delimiter, SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); 105 // assign the remaining arguments all to the last parameter if it is a string 106 if ((paramCount <= MAX_FUNCTOR_ARGUMENTS) &&(argumentCount > paramCount) && (paramCount == 1 || this->functor_->getTypenameParam(paramCount - 1) == "string")) 107 param[paramCount - 1] = arguments.subSet(paramCount - 1).join(delimiter); 169 108 170 // if there are not enough params given, check if there are default values 171 for (unsigned int i = tokens.size(); i < this->functor_->getParamCount(); i++) 172 if (this->defaultValue_[i].null()) 173 return false; 109 // evaluate the param types through the functor 110 for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 111 this->functor_->evaluateParam(i, param[i]); 174 112 175 // assign all given arguments to the multitypes 176 for (unsigned int i = 0; i < std::min(tokens.size(), MAX_FUNCTOR_ARGUMENTS); i++) 177 param[i] = tokens[i]; 178 179 // fill the remaining multitypes with default values 180 for (unsigned int i = tokens.size(); i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 181 param[i] = this->defaultValue_[i]; 182 183 // evaluate the param types through the functor 184 for (unsigned int i = 0; i < std::min(paramCount, MAX_FUNCTOR_ARGUMENTS); i++) 185 this->functor_->evaluateParam(i, param[i]); 186 187 return true; 188 } 113 if (error) 114 *error = CommandExecutor::Success; 115 return paramCount; 189 116 } 190 117
Note: See TracChangeset
for help on using the changeset viewer.