Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Console.cpp
00001 #include "Console.h" 00002 #include "Log.h" 00003 00004 extern Log _; 00005 00006 Console::Console(uint32_t timeout) 00007 : timeout(timeout) 00008 { 00009 this->reader = Reader::getInstance(); 00010 } 00011 00012 Command* Console::parse(string command) const 00013 { 00014 // trim 00015 { 00016 size_t first = command.find_first_not_of(' '); 00017 size_t last = command.find_last_not_of(' '); 00018 if (first != string::npos && last != string::npos) { 00019 command = command.substr(first, last - first + 1); 00020 } 00021 } 00022 00023 // remove backspaces 00024 { 00025 const char backspaces[] = {0x8, 0x7F}; 00026 for (uint8_t i = 0; i < sizeof(backspaces) / sizeof(backspaces[0]); i++) { 00027 size_t index = command.find_first_of(backspaces[i]); 00028 while (index != string::npos) { 00029 command.erase(index, 1); 00030 if (index != 0) { 00031 command.erase(index - 1, 1); 00032 } 00033 index = command.find_first_of(backspaces[i]); 00034 } 00035 } 00036 } 00037 00038 // remove control characters 00039 { 00040 size_t i = command.length(); 00041 if (i != 0) { 00042 do { 00043 i--; 00044 if (iscntrl(command[i])) { 00045 command.erase(i, 1); 00046 } 00047 } while (i != 0); 00048 } 00049 } 00050 00051 // remove extra whitespaces 00052 { 00053 size_t length, newLength = command.length(); 00054 do { 00055 length = newLength; 00056 size_t found = command.find(" "); 00057 if (found != string::npos) { 00058 command.replace(found, 2, " "); 00059 } 00060 newLength = command.length(); 00061 } while (length != newLength); 00062 } 00063 00064 // count whitespaces 00065 size_t length = command.length(); 00066 uint8_t spacesNum = 0; 00067 for (uint16_t i = 0; i < length; i++) { 00068 if (command[i] == ' ') { 00069 spacesNum++; 00070 } 00071 } 00072 00073 // break into pieces 00074 uint8_t argc = spacesNum + 1; 00075 string *argv = new string[argc]; 00076 size_t pos = 0; 00077 for (uint8_t i = 0; i < spacesNum; i++) { 00078 size_t len = command.find_first_of(' ', pos) - pos; 00079 argv[i] = command.substr(pos, len); 00080 pos += len + 1; 00081 } 00082 argv[spacesNum] = command.substr(pos); 00083 00084 // to lower case 00085 transform(argv[0].begin(), argv[0].end(), argv[0].begin(), ::tolower); 00086 00087 // make command 00088 if (argv[0].empty()) { 00089 delete[] argv; 00090 return NULL; 00091 } else if (argv[0] == "prueba") { 00092 return new PruebaCommand(argv, argc); 00093 } else { 00094 return new InvalidCommand(argv, argc); 00095 } 00096 } 00097 00098 void Console::start() 00099 { 00100 bool end = false; 00101 uint8_t intentos = 0; 00102 while (!end) { 00103 const string cmd = this->reader->readLine(this->timeout); 00104 if (cmd.length() != 0) { 00105 if (this->isFinishCommand(cmd)) { 00106 end = true; 00107 } else { 00108 Command* command = this->parse(cmd); 00109 if (command != NULL) { 00110 if (!this->execute(command)) { 00111 _.print("Error: ").print(command->getError()).ln(); 00112 } 00113 delete command; 00114 } 00115 } 00116 } else { 00117 if(intentos > 5) { 00118 end = true; 00119 } else { 00120 intentos++; 00121 } 00122 } 00123 } 00124 } 00125 00126 bool Console::execute(Command* command) 00127 { 00128 return command->execute(); 00129 } 00130 00131 bool Console::isFinishCommand(const string &command) const 00132 { 00133 return command == "finish" || command == "end" || command == "exit"; 00134 }
Generated on Fri Aug 19 2022 07:33:34 by
1.7.2