Add annotations to intervals

This commit is contained in:
Thomas Lauf 2018-10-06 18:35:46 +02:00
parent 59f44e7ff2
commit 0077768843
4 changed files with 132 additions and 12 deletions

View file

@ -31,6 +31,8 @@
#include <Lexer.h>
#include <sstream>
#include <JSON.h>
#include "Interval.h"
////////////////////////////////////////////////////////////////////////////////
bool Interval::empty () const
@ -84,6 +86,12 @@ std::string Interval::serialize () const
out << ' ' << quoteIfNeeded (tag);
}
if (! annotation.empty ())
{
out << (_tags.empty () ? " #" : "")
<< " # " << annotation;
}
return out.str ();
}
@ -123,6 +131,16 @@ std::string Interval::json () const
<< ']';
}
if (!annotation.empty ())
{
if (start.toEpoch () || end.toEpoch () || !_tags.empty ())
{
out << ',';
}
out << "\"annotation\":\"" << annotation << "\"";
}
out << "}";
return out.str ();
}
@ -167,5 +185,16 @@ void Interval::setRange (const Datetime& start, const Datetime& end)
this->end = end;
}
////////////////////////////////////////////////////////////////////////////////
void Interval::setAnnotation (const std::string& annotation)
{
this->annotation = annotation;
}
////////////////////////////////////////////////////////////////////////////////
std::string Interval::getAnnotation ()
{
return annotation;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -45,6 +45,9 @@ public:
void setRange (const Range& range);
void setRange (const Datetime& start, const Datetime& end);
void setAnnotation(const std::string& annotation);
std::string getAnnotation();
std::string serialize () const;
std::string json () const;
std::string dump () const;

View file

@ -69,12 +69,30 @@ Interval IntervalFactory::fromSerialization (const std::string& line)
}
// Optional '#' <tag>
if (tokens.size () > 2 + offset &&
tokens[1 + offset] == "#")
if (tokens.size () > 2 + offset && tokens[1 + offset] == "#")
{
// Optional <tag> ...
for (unsigned int i = 2 + offset; i < tokens.size (); ++i)
interval.tag (tokens[i]);
auto index = 2 + offset;
while (index < tokens.size () && tokens[index] != "#")
{
interval.tag (tokens[index]);
index++;
}
// Optional '#' <annotation>
if (index < tokens.size () && tokens[index] == "#")
{
std::string annotation;
// Optional <annotation> ...
for (unsigned int i = index + 1; i < tokens.size (); ++i)
{
annotation += (i > index +1 ? " " : "") + tokens[i];
}
interval.setAnnotation (annotation);
}
}
return interval;
@ -103,6 +121,9 @@ Interval IntervalFactory::fromJson (const std::string& jsonString)
}
}
json::string* annotation = (json::string*) json->_data["annotation"];
interval.annotation = (annotation != nullptr) ? annotation->_data : "";
json::string* start = (json::string*) json->_data["start"];
interval.start = (start != nullptr) ? Datetime(start->_data) : 0;
json::string* end = (json::string*) json->_data["end"];