diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-09-09 04:40:34 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-09-09 04:40:34 +0200 |
commit | 8dd9bc2815347435c8f92bb329a0209b50660618 (patch) | |
tree | f444a693e05a35f1aa0471ed823f6cd6b60e7d4f /src/Common | |
parent | d8dda96481a9614b5d7550643f794a9831c6b474 (diff) | |
download | mad-8dd9bc2815347435c8f92bb329a0209b50660618.tar mad-8dd9bc2815347435c8f92bb329a0209b50660618.zip |
Kommandos in einzelne Argumente aufteilen
Diffstat (limited to 'src/Common')
-rw-r--r-- | src/Common/Util.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/Common/Util.h b/src/Common/Util.h index 04b666c..71d7b69 100644 --- a/src/Common/Util.h +++ b/src/Common/Util.h @@ -22,6 +22,7 @@ #include <string> #include <locale> +#include <vector> namespace Mad { namespace Common { @@ -54,6 +55,64 @@ class Util { return str.substr(beg, end); } + + static std::vector<std::string> split(const std::string &str) { + std::vector<std::string> ret; + std::string temp; + bool quoteSingle = false, quoteDouble = false, escape = false; + + size_t beg = 0; + + for(size_t cur = 0; cur < str.length(); ++cur) { + if(!escape) { + if(str[cur] == ' ' && !quoteSingle && !quoteDouble) { + if(cur == beg && temp.empty()) { + ++beg; + } + else { + temp += str.substr(beg, cur-beg); + ret.push_back(temp); + temp.clear(); + beg = cur+1; + } + + continue; + } + + if(str[cur] == '"' && !quoteSingle) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteDouble = !quoteDouble; + continue; + } + if(str[cur] == '\'' && !quoteDouble) { + temp += str.substr(beg, cur-beg); + beg = cur+1; + + quoteSingle = !quoteSingle; + continue; + } + + if(str[cur] == '\\') { + escape = true; + continue; + } + } + + if(escape && ((!quoteSingle && !quoteDouble) || (quoteSingle && str[cur] == '\'') || (quoteDouble && (str[cur] == '"' || str[cur] == '\\')))) { + temp += str.substr(beg, cur-beg-1); + beg = cur; + } + + escape = false; + } + + temp += str.substr(beg, std::string::npos); + ret.push_back(temp); + + return ret; + } }; } |