From 050d9b942d3a3182865074f4f26f12af2a17fa3f Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sun, 6 May 2018 22:33:45 +0200 Subject: Check if substring is empty in split(), minor fix in command parsing --- main.cpp | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 4fca3db..de37652 100644 --- a/main.cpp +++ b/main.cpp @@ -32,16 +32,24 @@ extern "C" { std::vector split(const std::string& str, const char sep) { std::vector 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); + } } } } -- cgit v1.2.1