diff options
author | Nao Pross <naopross@thearcway.org> | 2018-05-06 22:33:45 +0200 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-05-06 22:33:45 +0200 |
commit | 050d9b942d3a3182865074f4f26f12af2a17fa3f (patch) | |
tree | 97fd8b4560b61ba81803090a29ade62ef23fb402 | |
parent | Fix uart device static_assert, add backspace support (diff) | |
download | SAMLiquidSmoke-050d9b942d3a3182865074f4f26f12af2a17fa3f.tar.gz SAMLiquidSmoke-050d9b942d3a3182865074f4f26f12af2a17fa3f.zip |
Check if substring is empty in split(), minor fix in command parsing
-rw-r--r-- | main.cpp | 44 |
1 files changed, 35 insertions, 9 deletions
@@ -32,16 +32,24 @@ extern "C" { std::vector<std::string> split(const std::string& str, const char sep) { std::vector<std::string> v; + std::string sub; size_t last = 0; size_t next = 0; while ((next = str.find(sep, last)) != std::string::npos) { - v.push_back(str.substr(last, next-last)); + sub = str.substr(last, next-last); + if (!sub.empty()) { + v.push_back(sub); + } + last = next + 1; } - - v.push_back(str.substr(last)); + + sub = str.substr(last); + if (!sub.empty()) { + v.push_back(sub); + } return v; } @@ -153,15 +161,33 @@ int main(int argc, char** argv) if (command[0] == "set") { if (command[1] == "led") { if (command[2] == "red") { - led1.set((command[3] == "on")); + if (command[3] == "on") { + led1.set(1); + } else if (command[3] == "off") { + led1.set(0); + } } else if (command[2] == "green") { - led2.set((command[3] == "on")); + if (command[3] == "on") { + led2.set(1); + } else if (command[3] == "off") { + led2.set(0); + } } else if (command[2] == "yellow") { - led3.set((command[3] == "on")); + if (command[3] == "on") { + led3.set(1); + } else if (command[3] == "off") { + led3.set(0); + } } else if (command[2] == "all") { - led1.set((command[3] == "on")); - led2.set((command[3] == "on")); - led3.set((command[3] == "on")); + if (command[3] == "on") { + led1.set(1); + led2.set(1); + led3.set(1); + } else if (command[3] == "off") { + led1.set(0); + led2.set(0); + led3.set(0); + } } } } |