diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-17 22:19:51 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-17 22:19:51 +0200 |
commit | fd04142b2accb08ab3c79bfd5c5bffd25b3f5784 (patch) | |
tree | bf67976abdb9c92cc26233091f24e3b3de5ca5b1 /src | |
parent | a4db4f377b6d0d3c91aba8bc4a2392845c2ef18e (diff) |
Storage: newline support in records
Diffstat (limited to 'src')
-rw-r--r-- | src/storage.cpp | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/src/storage.cpp b/src/storage.cpp index 6a0669c..16662df 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -642,21 +642,31 @@ optional<RecordT<S>> RecordT<S>::decode(const S & st, auto items = make_shared<vector<Item>>(); while (begin != end) { - const auto newline = std::find(begin, end, '\n'); - if (newline == end) + const auto colon = std::find(begin, end, ':'); + if (colon == end) throw runtime_error("invalid record"); + const string name(begin, colon); - const auto colon = std::find(begin, newline, ':'); - if (colon == newline) + const auto space = std::find(colon + 1, end, ' '); + if (space == end) throw runtime_error("invalid record"); + const string type(colon + 1, space); - const auto space = std::find(colon, newline, ' '); - if (space == newline) - throw runtime_error("invalid record"); - - const auto name = string(begin, colon); - const auto type = string(colon + 1, space); - const auto value = string(space + 1, newline); + begin = space + 1; + string value; + for (bool cont = true; cont; ) { + auto newline = std::find(begin, end, '\n'); + if (newline == end) + throw runtime_error("invalid record"); + + if (newline + 1 != end && *(newline + 1) == '\t') + newline++; + else + cont = false; + + value.append(begin, newline); + begin = newline + 1; + } if (type == "i") items->emplace_back(name, std::stoi(value)); @@ -680,8 +690,6 @@ optional<RecordT<S>> RecordT<S>::decode(const S & st, } else items->emplace_back(name, typename Item::UnknownType { type, value }); - - begin = newline + 1; } return RecordT<S>(items); @@ -765,8 +773,17 @@ vector<uint8_t> RecordT<S>::encodeInner() const copy(type.begin(), type.end(), inserter); inserter = ' '; - copy(value.begin(), value.end(), inserter); - inserter = '\n'; + + auto i = value.begin(); + while (true) { + auto j = std::find(i, value.end(), '\n'); + copy(i, j, inserter); + inserter = '\n'; + if (j == value.end()) + break; + inserter = '\t'; + i = j + 1; + } } return res; } |