diff options
author | John Wiegley <johnw@newartisans.com> | 2009-10-26 19:09:24 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-10-26 19:09:24 -0400 |
commit | 7fae606d622d8cd463ce5f81f5d7872a4fdebc60 (patch) | |
tree | be5550e492b20c135143c821fbc18cbd63c27060 /src/utils.cc | |
parent | 92d1bbbe75df71731d458f5b48bcb0f145428920 (diff) | |
parent | 3fdd75fb5b2614f7dab29fd5ad5c9efed6e80b79 (diff) | |
download | fork-ledger-7fae606d622d8cd463ce5f81f5d7872a4fdebc60.tar.gz fork-ledger-7fae606d622d8cd463ce5f81f5d7872a4fdebc60.tar.bz2 fork-ledger-7fae606d622d8cd463ce5f81f5d7872a4fdebc60.zip |
Merge branch 'next'
Diffstat (limited to 'src/utils.cc')
-rw-r--r-- | src/utils.cc | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/utils.cc b/src/utils.cc index 0afea4c2..85a7aa46 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -442,6 +442,59 @@ string::~string() throw() { ledger::string empty_string(""); +ledger::strings_list split_arguments(const char * line) +{ + using namespace ledger; + + strings_list args; + + char buf[4096]; + char * q = buf; + char in_quoted_string = '\0'; + + for (const char * p = line; *p; p++) { + if (! in_quoted_string && std::isspace(*p)) { + if (q != buf) { + *q = '\0'; + args.push_back(buf); + q = buf; + } + } + else if (in_quoted_string != '\'' && *p == '\\') { + p++; + if (! *p) + throw_(std::logic_error, _("Invalid use of backslash")); + *q++ = *p; + } + else if (in_quoted_string != '"' && *p == '\'') { + if (in_quoted_string == '\'') + in_quoted_string = '\0'; + else + in_quoted_string = '\''; + } + else if (in_quoted_string != '\'' && *p == '"') { + if (in_quoted_string == '"') + in_quoted_string = '\0'; + else + in_quoted_string = '"'; + } + else { + *q++ = *p; + } + } + + if (in_quoted_string) + throw_(std::logic_error, + _("Unterminated string, expected '%1'") << in_quoted_string); + + if (q != buf) { + *q = '\0'; + args.push_back(buf); + } + + return args; +} + /********************************************************************** * * Logging |