From 57b45f9c73bd6fe53506f001accf5c9bf1165f0e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sat, 9 Apr 2016 09:44:46 -0400 Subject: [PATCH] helper: Added jsonFromIntervals --- src/helper.cpp | 33 +++++++++++++++++++++++++++++++++ src/timew.h | 1 + 2 files changed, 34 insertions(+) diff --git a/src/helper.cpp b/src/helper.cpp index a0965637..79f42520 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -215,3 +215,36 @@ bool intervalMatchesFilter (const Interval& interval, const Filter& filter) } //////////////////////////////////////////////////////////////////////////////// +// Compose a JSON document of intervals. In the trivial case: +// [ +// ] +// +// In the non-trivial case: +// [ +// {...}, +// {...} +// ] +// +std::string jsonFromIntervals (const std::vector & intervals) +{ + std::stringstream out; + + out << "[\n"; + int counter = 0; + for (auto& interval : intervals) + { + if (counter) + out << ",\n"; + + out << interval.json (); + ++counter; + } + + if (counter) + out << "\n"; + + out << "]\n"; + return out.str (); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/timew.h b/src/timew.h index 516dbfcf..99be7c1e 100644 --- a/src/timew.h +++ b/src/timew.h @@ -51,6 +51,7 @@ Filter createFilterFromCLI (const CLI&); Timeline createTimelineFromData (const Rules&, Database&, const Filter&); Interval getLatestInterval (Timeline&, Database&, const Filter&); bool intervalMatchesFilter (const Interval&, const Filter&); +std::string jsonFromIntervals (const std::vector &); // utiƀ.cpp std::string osName ();