void CommandLineEngine::set(Print * printer, Tokens & tk, FunctionFlags ff) { if( ff == FF_HelpText ) { if( printer ) { printer->println("Function Name: Set"); printer->println("Description: Use it to set a macro. When using set do not include the $ sign."); printer->println("To use a macro type the $macro name. For instance the following commands."); printer->println("set redpin D7"); printer->println("pin $redpin high"); printer->println("The set function syntax is as follows:"); printer->println("set [macroname] [value]"); } return; } String arg, val; if( !tk.next(arg) || !tk.next(val) ) quickprint("Invalid syntax. Type help set") UserMacros::iterator it = findMacro(arg); if( it == mUserMacros.end() ) { UserMacroDef um = { arg, val}; mUserMacros.push_back( um ); } else{ it->value = val; } }
std::vector<bool> parseBools(Tokens& tokens) { std::vector<bool> r; if (tokens.peek() == "[") { tokens.next(); while (tokens.peek() != "]") r.push_back(parseBool(tokens)); tokens.next(); } else { r.push_back(parseBool(tokens)); } return r; }
std::vector<int64_t> parseInts(Tokens& tokens) { std::vector<int64_t> r; if (tokens.peek() == "[") { tokens.next(); while (tokens.peek() != "]") r.push_back(parseInt(tokens)); tokens.next(); } else { r.push_back(parseInt(tokens)); } return r; }
std::vector<std::string> parseStrings(Tokens& tokens) { std::vector<std::string> r; if (tokens.peek() == "[") { tokens.next(); while (tokens.peek() != "]") r.push_back(parseString(tokens)); tokens.next(); } else { r.push_back(parseString(tokens)); } return r; }
void parse(Tokens& tokens) { while (!tokens.done()) { auto token = tokens.next(); auto c = Commands.find(token); if (c != Commands.end()) c->second(tokens); else { std::cout << "Unable to find PBRT command: " << token << std::endl; } } }
void CommandLineEngine::help(Print * printer, Tokens & tk, FunctionFlags ff) { if (!printer) return; UserFunctions::iterator it; String arg; if( tk.next(arg) ) { if( arg.equalsIgnoreCase("help") ) { printer->println("Type help without any arguments to learn more."); return; } else if( arg.equalsIgnoreCase("set") ) { set(printer, tk, FF_HelpText); return; } it = findFunction(arg); if( it == mUserFunctions.end() ) { printer->print("Unable to locate the function: "); printer->println(arg); return; } // Call the function and tell it to display its help text it->func(printer, tk, FF_HelpText); return; } else { printer->println("Function Name: help"); printer->println("Type help <function name> to learn more about that function."); printer->println("The following functions are defined:"); printer->println("help"); printer->println("set"); // List all the functions for( it = mUserFunctions.begin(); it != mUserFunctions.end(); ++it) { printer->println(it->name); } } }
bool CommandLineEngine::execute( String cmdLine, Print * printer ) { if( cmdLine.length() <= 0 ) return false; Tokens tk; if( Tokens::Tokenize(tk,cmdLine).isEmpty() ) return false; // Replace the macros now // doMacros uses external iterator so the internal iterator of tk is untouched doMacros( tk ); // Now find the function and call it // But first check if it is one of the built in functions String func; if (!tk.next(func)) return false; // Remove trailing weird space issues func.trim(); if( func.equalsIgnoreCase("set") ) { // Built in function set( printer, tk, FF_Normal ); } else if ( func.equalsIgnoreCase("help") ) { // Built in function help( printer, tk, FF_Normal ); } else { bool foundfunction = false; // Now Search for the function and call the user function if // we can find it for( UserFunctions::iterator it = mUserFunctions.begin(); it != mUserFunctions.end(); ++it ) { if( func.equalsIgnoreCase( it->name) ) { foundfunction = true; it->func( printer ? printer : &nullprinter, tk, FF_Normal); break; } } if( !foundfunction ) { if(printer) { printer->print("Unable to find the specified command: "); printer->println( func ); } return false; } } return true; }
int64_t parseInt(Tokens& tokens) { return stoll(tokens.next()); }
float parseReal(Tokens& tokens) { return stof(tokens.next()); }
std::string parseString(Tokens& tokens) { auto t = tokens.next(); // strip off quotes and return return t.substr(1, t.length() - 2); }