diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-05-12T22·13+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2010-05-12T22·13+0000 |
commit | aa45027818af8976dc73e6a299d5d918e5c51df1 (patch) | |
tree | 6086eb0642bc90477397169d0ee3993c1789200e /src/libutil/util.cc | |
parent | a0e3b84fac56cad6377ecd1462058a6b29bb1ea8 (diff) | |
parent | 8032f26ca0bd2233de066ce5786ff976bbd641ae (diff) |
* Sync with the trunk.
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r-- | src/libutil/util.cc | 88 |
1 files changed, 41 insertions, 47 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 1c1c9a9e5b74..e0c41a58d8ee 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -950,53 +950,6 @@ void _interrupted() ////////////////////////////////////////////////////////////////////// -string packStrings(const Strings & strings) -{ - string d; - for (Strings::const_iterator i = strings.begin(); - i != strings.end(); ++i) - { - unsigned int len = i->size(); - d += len & 0xff; - d += (len >> 8) & 0xff; - d += (len >> 16) & 0xff; - d += (len >> 24) & 0xff; - d += *i; - } - return d; -} - - -Strings unpackStrings(const string & s) -{ - Strings strings; - - string::const_iterator i = s.begin(); - - while (i != s.end()) { - - if (i + 4 > s.end()) - throw Error(format("short db entry: `%1%'") % s); - - unsigned int len; - len = (unsigned char) *i++; - len |= ((unsigned char) *i++) << 8; - len |= ((unsigned char) *i++) << 16; - len |= ((unsigned char) *i++) << 24; - - if (len == 0xffffffff) return strings; /* explicit end-of-list */ - - if (i + len > s.end()) - throw Error(format("short db entry: `%1%'") % s); - - strings.push_back(string(i, i + len)); - i += len; - } - - return strings; -} - - Strings tokenizeString(const string & s, const string & separators) { Strings result; @@ -1052,6 +1005,47 @@ bool hasSuffix(const string & s, const string & suffix) } +void expect(std::istream & str, const string & s) +{ + char s2[s.size()]; + str.read(s2, s.size()); + if (string(s2, s.size()) != s) + throw Error(format("expected string `%1%'") % s); +} + + +string parseString(std::istream & str) +{ + string res; + expect(str, "\""); + int c; + while ((c = str.get()) != '"') + if (c == '\\') { + c = str.get(); + if (c == 'n') res += '\n'; + else if (c == 'r') res += '\r'; + else if (c == 't') res += '\t'; + else res += c; + } + else res += c; + return res; +} + + +bool endOfList(std::istream & str) +{ + if (str.peek() == ',') { + str.get(); + return false; + } + if (str.peek() == ']') { + str.get(); + return true; + } + return false; +} + + void ignoreException() { try { |