From 898b5d9e4c43850d6326abefdba0e0bcf6bb1123 Mon Sep 17 00:00:00 2001 From: Jan Holesovsky Date: Mon, 6 Feb 2017 13:36:54 +0100 Subject: [PATCH] Treat the rest of the line as payload, because it may contain the delimiter. Change-Id: I883f41727cf46c4b71484d2b99144ae7e30ef6d3 --- wsd/TraceFile.hpp | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/wsd/TraceFile.hpp b/wsd/TraceFile.hpp index 347e5f6ae..8f308f1eb 100644 --- a/wsd/TraceFile.hpp +++ b/wsd/TraceFile.hpp @@ -378,22 +378,11 @@ private: break; } - const auto v = split(line, line[0]); - if (v.size() == 4) - { - TraceFileRecord rec; - rec.Dir = static_cast(line[0]); - unsigned index = 0; - rec.TimestampNs = std::atoi(v[index++].c_str()); - rec.Pid = std::atoi(v[index++].c_str()); - rec.SessionId = v[index++]; - rec.Payload = v[index++]; + TraceFileRecord rec; + if (extractRecord(line, rec)) _records.push_back(rec); - } else - { fprintf(stderr, "Invalid trace file record, expected 4 tokens. [%s]\n", line.c_str()); - } } if (_records.empty() || @@ -411,20 +400,43 @@ private: _epochStart = _records[0].TimestampNs; } - static std::vector split(const std::string& s, const char delim) + static bool extractRecord(const std::string& s, TraceFileRecord& rec) { - std::stringstream ss(s); - std::string item; - std::vector v; - while (std::getline(ss, item, delim)) + if (s.length() < 1) + return false; + + char delimiter = s[0]; + rec.Dir = static_cast(delimiter); + + size_t pos = 1; + int record = 0; + for (; record < 4 && pos < s.length(); ++record) { - if (!item.empty()) + size_t next = s.find(delimiter, pos); + + switch (record) { - v.push_back(item); + case 0: + rec.TimestampNs = std::atoi(s.substr(pos, next - pos).c_str()); + break; + case 1: + rec.Pid = std::atoi(s.substr(pos, next - pos).c_str()); + break; + case 2: + rec.SessionId = s.substr(pos, next - pos); + break; + case 3: + rec.Payload = s.substr(pos); + return true; } + + if (next == std::string::npos) + break; + + pos = next + 1; } - return v; + return false; } unsigned advance(unsigned index, const TraceFileRecord::Direction dir)