00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00140 #include "../include/IPSA/viewer/instructions/Instruction.h"
00141
00142 #include <algorithm>
00143 #include <iostream>
00144 #include <sstream>
00145 #include <iterator>
00146
00151 Instruction::Instruction(const std::string& code)
00152 : I_CODE(code)
00153 {
00154 if (I_CODE.empty())
00155 std::cout << "Instruction with no help string!!!!" << std::endl;
00156 std::string::size_type j = code.find_first_not_of(" ");
00157 while (j != code.npos)
00158 {
00159 std::string::size_type k = code.find(" ",j);
00160 if (k == code.npos)
00161 {
00162 instrdata.push_back(code.substr(j,code.size()-j));
00163 j = code.npos;
00164 }
00165 else
00166 {
00167 instrdata.push_back(code.substr(j,k-j));
00168 j = code.find_first_not_of(" ",k);
00169 }
00170 }
00171 }
00172
00173
00183 bool Instruction::findAndExecute(std::vector<std::string>& instr, std::ostringstream& answer, unsigned int& numberOfReturnValues)
00184 {
00185 if (instr.empty() || instr[0] != instrdata[0])
00186 return false;
00187
00188 bool result = false;
00189
00190 std::cout << "cmd: ";
00191 std::copy(instr.begin(), instr.end(), std::ostream_iterator<std::string>(std::cout, " "));
00192 std::cout << "... ";
00193
00194
00195 if(instr.size() == instrdata.size() )
00196 {
00197 tExecResultType res = vExecute(instr,answer, numberOfReturnValues);
00198
00199 switch (res)
00200 {
00201 case eUNKNOWN_ERROR: printf("unknown error occured!\n"); break;
00202 case eJOINT_NOT_FOUND: printf("joint not found!\n"); break;
00203 case eBODY_NOT_FOUND: printf("body not found!\n"); break;
00204 case eJOINT_TYPE_INCOMPATIBLE: printf("joint type incompatible!\n"); break;
00205 case eNODE_IS_NOT_A_BODY: printf("node is not a body!\n"); break;
00206 case eOK: printf("done.\n"); result = true; break;
00207 default: printf("unknown result type!\n"); break;
00208 }
00209 }
00210 else
00211 {
00212 answer << std::endl;
00213 printf("ERROR,got %d instead of %d arguments:\n \t[ ", (int)instr.size(),
00214 (int)instrdata.size());
00215 std::copy(instrdata.begin(), instrdata.end(), std::ostream_iterator<std::string>(std::cout, " "));
00216 printf("]\n");
00217 }
00218 return result;
00219 }
00220
00221
00225 std::string Instruction::getCmdString()
00226 {
00227 return instrdata.at(0);
00228 }
00229
00230
00235 std::string Instruction::getCmdHelpString()
00236 {
00237 return I_CODE;
00238 }
00239
00240
00244 unsigned int Instruction::getCommandParameterCount()
00245 {
00246
00247 return (instrdata.size() - 1);
00248 }